I am using a TextInputEditText as suggested here to let the user add text up to a specific length.
However, I want that when the TextInputEditText view receives focus, the hint should disappear instead of going above the view, as shown in the aforementioned tutorial. In this answer, the solution described is for a EditText and it just makes the hint go transparent. However, for a TextInputEditText, even if I make the hint transparent on receiving focus, it will still occupy space above the view.
Is there a way to just remove it on receiving focus in the TextInputEditText, statically ? It is easy to do it through OnFocusChangeListener, but I was looking to do it at compile-time.
Try to set focus change listener on EditText and hide hint programmatically when focus is true and show again hint if focus is false and text is empty.
try this from the following post
how to make hint disappear when edittext is touched?
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
myEditText.setHint("");
else
myEditText.setHint("Your hint");
}
});
Try this if you want to disappear on focus:
textInputEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasfocus) {
if (hasfocus) {
textInputLayout.setHint("test");
} else {
textInputLayout.setHint(null);
}
}
});
Try below code if you want to disappear on typing (in case you are looking for something like this):
textInputEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (TextUtils.isEmpty(textInputEditText.getText().toString())) {
textInputLayout.setHint("test");
} else {
textInputLayout.setHint(null);
}
}
#Override
public void afterTextChanged(Editable editable) {
}
});
Simply add:-
try this from the following post how to make hint disappear when edittext is touched?
edittxt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
edittxt.setHint("");
else
edittxt.setHint("hint");
}
});
try to set EditText hint in java and it will automatically hide when you click on the EditText.
edtPassword.setHint(getString(R.string.YOUR_HINT));
For those who have double hint in TextInputLayout, see TextInputLayout and EditText double hint issue. We need to set hint to TextInputLayout, not TextInputEditText.
editText.setOnFocusChangeListener { _, hasFocus ->
inputLayout.hint = if (hasFocus) {
""
} else {
getString(R.string.some_text)
}
}
Related
I want to know is there any chance to get our result in an edittext not in textview without using any button. That is when we place cursor on the edittext field, the result we want to print should be in the field. Is there anyone who could help vth this.?
This is untested but it should work I think
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
editText.setText("whatever")
}
}
});
I have the following code and for some reason , the onFocusChange doesn't get executed? It works for an Edittext thought.
Code:
if (rBar.getOnFocusChangeListener() == null)
rBar.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
// do something
}
});
I have tried the other SO questions related to this like setting: but no success
rBar.setFocusable(true);
rBar.setFocusableInTouchMode(true);
I am working on an Android app and I am trying to implement a feature that I have found online. I am tying to do is when user clicks on edit text, for example, if user clicks on "Username" edit text, the setOnFocusChangeListener will check things and will start animation for label to fadeIn and fadeOut, here my codes
userSignupPasswordET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (userSignupPasswordET.hasFocus()) {
signupPassWDTV.startAnimation(fadeIn);
} else {
if (userSignupPasswordET.getText().toString().length() == 0) {
signupPassWDTV.startAnimation(fadeOut);
}
}
}
});
The feature I am trying to do is here, on youtube. But the problem I am facing is that I have three fields, username, password and email. So if I click on username it is working and fadIn animation for username label works and also if I do not enter anything fadeOut animation works when I move to other edit text and the other edit text's own setOnFocusChangeListener works. But problem is the if I don't enter anything in any of them just keep switching them, keep changing their focus, the other two edit text's setOnFocusChangeListener gets triggered and starts fadeIn animation and than fadeOut animation. So I don't know what to do. Any suggestion? Remember, if I don't enter anything in edit field and just keep changing/toggling them, this problem is getting triggered. If I enter something in one, it changes and label stays visible.
You can add your logic on TextChangedListener()
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
#Override
public void afterTextChanged(Editable et) {
}
});
so that your animation get called only when you enter something on your edittext
I am trying to validate my edittext field after focus is changed but it gives when the time of typing only.i.e, after entering one charecter I am used below code
fname.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s){//some validations}
But my requirement, in my form so many fields are there if we go for validation submition time its a risky one. so please help me.
Add OnFocusChangeListener to your edit text
fname.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// If view having focus.
} else {
// If view not having focus. You can validate here
}
}
});
I have a EditText in my app which is supposed to take only numeric values; this is why I set the inputType property this way:
android:inputType="number|numberSigned|numberDecimal"
It works properly, except when the user presses enter, because it losts focus; is there any property to set to avoid this? Thank you for your answers.
What do you want to do? Keep the focus?
You can implement this method and check the value... if the value is not what you expect, you put back the focus.
public void onFocusChange(View v, boolean hasFocus) {
// ...
}
Regarding your comment:
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus && (v instanceof EditText)) {
((EditText) v).requestFocus();
}