I have an EditText
and a Button
. On click of the button i want to open the EditText
keyboard and at the same time request focus on the EditText
, So that the user directly start typing in the keyboard and text appears in the EditText
.
But in my case when i click the button it open the keyboard, but it won't set focus on the EditText
, because of which user has to click the EditText
again to write on it. What is the issue. Any help or suggestion.
Code On click of button
m_SearchEditText.requestFocus();
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(m_SearchEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
In your manifest.xml write:
And call
m_SearchEditText.requestfocus()
inoncreate()
.OR,
Try:
ensure that the edittext is focusable in touch mode. You can do it two way.
In xml:
in Java:
Personally I don't trust the XML definition of this param. I always request focus by these two lines of code:
The keyboard shoul appear on itself without the need to call InputMethodManager.
It works in most of the cases. Once it did not work for me because I have lost the reference to the object due to quite heavy processing - ensure that this is not your issue.
The following works for me and should help:
In my case it worked by adding a handler after you clicked to button and focus set in another view the focus can get back to your needed view.
just put this in your code:
I hope it was helpful
As an extension to this answer (I didn't add it as a comment because of reputation...).
If you want to reduce the delayed time to zero, use handler.post() instead. Full code:
U need two xml attributes also to achieve this:
Add them to the EditText as well as the parent layouts(Any layout inside which these views are). 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.