In my application in android are many EditText fields. And I ran into a problem with hint. It is not disappearing when EditText is focused, but it disappears when I start to
You can also custom your XML code by using Selector. Here is an example code.
Create a selector. In file selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="@android:color/transparent" />
<item android:color="@color/gray" />
</selector>
In your view
android:textColorHint="@drawable/selector"
The way I like to do it (now in Kotlin) is to have the hint reappear if the user clicks elsewhere. Initialize the hint-string outside of your class, also importing your synthetic layout there:
import kotlinx.android.synthetic.main.activity_layout.*
private const val MY_HINT= "The Hint Shown"
Then use an OnFocusChange listener:
myEditText.setOnFocusChangeListener() { v, event ->
myEditText.hint = if(myEditText.hasFocus()) "" else MY_HINT
false
}
That way the hint doesn't get lost as the user clicks around. Or, just set it to an empty string if you don't care about that!
This is my solution:
final EditText editText = (EditText) findViewById(R.id.activity_edittext);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
editText .setHint("my little hint");
} else {
editText .setHint("");
}
}
});
My solution is like this. It shows hint when empty and not focused and hides if there is text or if it is touched
fun hideHint() {
tilFloating.isHintAnimationEnabled = false
etFloating.onFocusChangeListener = OnFocusChangeListener { v, hasFocus ->
if ((etFloating.text != null && etFloating.text.toString() != "") || hasFocus)
tilFloating.hint = ""
else
tilFloating.hint = mHint
}
}
Here's my solution, where the hint disappear when user focus editText and appears again when focus changes to other place if the edittext is still empty:
editText.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
editText.setHint("");
return false;
}
});
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
editText.setHint("Hint");
}
}
});
hope this helps someone
findViewById(R.id.mainLayout).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hideKeyboard();
editText.clearFocus();
if (TextUtils.isEmpty(editText.getText().toString())) {
editText.setHint("your hint");
}
}
});
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
editText.setHint("");
return false;
}
});
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
editText.setHint("");
else
editText.setHint("your hint");
}
});