Edittext Requestfocus() Dynamically Not Working
I have multiple Edittexts on action next I have added nextFocus values in the xml. But one edit text is either visible or invisible based on the state of under age checkbox value.
Solution 1:
What was happening is the I was returning false from onEditorAction- Hence the default editor action was also performed back to back and the focus instantly moved away from the email editText. Now I return true and the deafult action is not performed.Its working correctly now.
dobYearEt.setOnEditorActionListener(new EditText.OnEditor ActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.i("focus","on yyyy");
if (actionId == EditorInfo.IME_ACTION_DONE ||
actionId == EditorInfo.IME_ACTION_NEXT) {
Log.i("focus",isUnderAge+"");
if(isUnderAge){
username_reg_ev_dob_row.setFocusableInTouchMode(true);
username_reg_ev_dob_row.requestFocus();
}else{
email_ev.requestFocus();
}
returntrue ; // NOW WORKS// return false;
}
Solution 2:
U need two xml attributes also to achieve this:
android:focusable="true"android:focusableInTouchMode="true"
Add them to the EditText as well as the parent layouts. By default these are false, so the focus is not given to the requested view.
Source: https://developer.android.com/reference/android/view/View.html#attr_android:focusable
After u show the EditText based on the checkbox selection, add the next and previous focus points dynamically in code.
Hope this helps.
Post a Comment for "Edittext Requestfocus() Dynamically Not Working"