Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

force soft keyboard to show when EditText gets focus [duplicate]

I have an EditText that I am passing focus to programmatically. But when I do, I want the keyboard to show up as well (and then go down when that EditText lose focus). Right now, the user has to click on the EditText to get the keyboard to show up -- even thought the EditText already has focus.

like image 563
Cote Mounyo Avatar asked Aug 14 '13 16:08

Cote Mounyo


4 Answers

This is how I show the ketyboard:

EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
like image 84
Waza_Be Avatar answered Nov 02 '22 17:11

Waza_Be


<activity   android:name=".YourActivity"
            android:windowSoftInputMode="stateVisible" />

Add this to manifest file...

like image 38
Looking Forward Avatar answered Nov 02 '22 17:11

Looking Forward


set this for your activity in your manifest to pop keyboard automatically when your screen comes containing EditText box

<activity android:windowSoftInputMode="stateAlwaysVisible" ... />

To hide keyboard on losing focus set a OnFocusChangeListener for the EditText .

In the onCreate()

EditText editText = (EditText) findViewById(R.id.textbox);
OnFocusChangeListener ofcListener = new MyFocusChangeListener();
editText.setOnFocusChangeListener(ofcListener);

Add this class

private class MyFocusChangeListener implements OnFocusChangeListener {

    public void onFocusChange(View v, boolean hasFocus){

        if(v.getId() == R.id.textbox && !hasFocus) {

            InputMethodManager imm =  (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

        }
    }
}
like image 7
jad Avatar answered Nov 02 '22 15:11

jad


To show the keyboard, use the following code.

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);

To hide the keyboard,, use the code below. et is the reference to the EditText

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
like image 6
Rohit Avatar answered Nov 02 '22 15:11

Rohit