Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TextField : set focus + soft input programmatically

In my view, I have a search EditText and I would like to trigger programmatically the behaviour of a click event on the field, i.e give focus to the text field AND display soft keyboard if necessary (if no hard keyboard available).

I tried field.requestFocus(). The field actually gets focus but soft keyboard is not displayed.

I tried field.performClick(). But that only calls the OnClickListener of the field.

Any idea ?

like image 679
sdabet Avatar asked Nov 10 '11 13:11

sdabet


2 Answers

Good sir, try this:

edittext.setFocusableInTouchMode(true); edittext.requestFocus(); 

I'm not sure, but this might be required on some phones (some of the older devices):

final InputMethodManager inputMethodManager = (InputMethodManager) context                 .getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT); 
like image 93
pgsandstrom Avatar answered Sep 20 '22 23:09

pgsandstrom


Here is the code that worked for me.

edittext.post(new Runnable() {     public void run() {         edittext.requestFocusFromTouch();         InputMethodManager lManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);          lManager.showSoftInput(edittext, 0);     } }); 

That's it! Enjoy ;)

like image 37
elisabel Avatar answered Sep 21 '22 23:09

elisabel