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);
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 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)
}
}
Basically, I am validating a field when the the field loses focus. When an error occurs when we check at the OnFocusChangeListener, I want it to cancel the focus and stay on the same field. Is there a way to cancel the focus on it?
public void onFocusChange(View v, boolean hasFocus)
{
if (!hasFocus)
{
if(checkError(v))
{
// Cancel focus here
}
}
}
I think requestFocus() should do the trick, see this answer for usage: https://stackoverflow.com/a/8077842/1173391
Update:
I think you are missing the check for the view id, because the onFocusChange method is implemented via interface in your class:
try setting this:
if (v.getId() == myEditText.id)
as first check on the focus change method; you have to do it for every edit-field (or use a switch instead of if) ....
OR
attach an OnFocusChangeListener to every single field:
edittext1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
}
});
I have EditView a button and a Spinner in my layout. A picture is given below:
When I focus on the search field (EditText) I'm making the spinner (BrowseBy) disappear. Code is here:
edTxt_SearchField.setOnFocusChangeListener
(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (edTxt_SearchField.hasFocus()) {
spnrBrowseBy.setVisibility(View.GONE);
}
}
});
what happens here is that the softkeyBoard appears. also notice that spinner is gone.
When I press the backKey here, the softkeyboard is hiding. I want the spinner should reappear here also as in 1st image. Or is there any method which can detect the keyBoardHide event?
I have tried onConfiguration change method, has no effect. Note: I can't override the onBackPresskey() method also because my activity is extending the ActivityGroup class.
Did you put the configChages in your manifest so that onConfigurationChage is called?
<activity android:name="TestActivity"
android:configChanges="keyboardHidden">
otherwise it should be just
public void onConfigurationChanged(Configuration newConfig){
spnrBrowseBy.setVisibility(View.VISIBLE);
spnrBrowseBy.requestLayout();
super.onConfigurationChanged(newConfig);
}
By following way you can take actions on Got Focus and Lost Focus of your EditText.
yourEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
Toast.makeText(getApplicationContext(), "Got Focus", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Lost Focus", Toast.LENGTH_LONG).show();
}
});
Hope this helps you.
Thanks.
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
}
}
});