Skip to content

Instantly share code, notes, and snippets.

@ivanarellano
Last active August 29, 2015 14:19
Show Gist options
  • Save ivanarellano/b2eee986e3fddfe12b6f to your computer and use it in GitHub Desktop.
Save ivanarellano/b2eee986e3fddfe12b6f to your computer and use it in GitHub Desktop.
Simple EditText hint clearing
/*
A custom color selector can simulate this coded function.
This is used to 'clear' the Hint when the user taps an EditText,
thus allowing a centered text cursor to be unobstructed by the Hint.
*/
// custom edittext
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (getText().toString().trim().isEmpty()) {
if (focused) {
setHint(" ");
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);
} else {
setHint(mHint);
}
}
}
// color/edit_text_hint_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/transparent" android:state_selected="true"/>
<item android:color="@android:color/transparent" android:state_focused="true"/>
<item android:color="@color/medium_light_grey"/>
</selector>
// styles.xml
<style name="StandardEditText" parent="p1">
<item name="android:textColorHint">@color/edit_text_hint_selector</item>
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment