Android - Programmatically Hide/Show Soft Keyboard [duplicate] Android - Programmatically Hide/Show Soft Keyboard [duplicate] android android

Android - Programmatically Hide/Show Soft Keyboard [duplicate]


UPDATE 2

@Override    protected void onResume() {        super.onResume();        mUserNameEdit.requestFocus();        mUserNameEdit.postDelayed(new Runnable() {            @Override            public void run() {                // TODO Auto-generated method stub                InputMethodManager keyboard = (InputMethodManager)                getSystemService(Context.INPUT_METHOD_SERVICE);                keyboard.showSoftInput(mUserNameEdit, 0);            }        },200); //use 300 to make it run when coming back from lock screen    }

I tried very hard and found out a solution ... whenever a new activity starts then keyboard cant open but we can use Runnable in onResume and it is working fine so please try this code and check...

UPDATE 1

add this line in your AppLogin.java

mUserNameEdit.requestFocus();

and this line in your AppList.java

listview.requestFocus()'

after this check your application if it is not working then add this line in your AndroidManifest.xml file

<activity android:name=".AppLogin" android:configChanges="keyboardHidden|orientation"></activity><activity android:name=".AppList" android:configChanges="keyboard|orientation"></activity>

ORIGINAL ANSWER

 InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);

for hide keyboard

 imm.hideSoftInputFromWindow(ed.getWindowToken(), 0); 

for show keyboard

 imm.showSoftInput(ed, 0);

for focus on EditText

 ed.requestFocus();

where ed is EditText


Adding this to your code android:focusableInTouchMode="true" will make sure that your keypad doesn't appear on startup for your edittext box. You want to add this line to your linear layout that contains the EditTextBox. You should be able to play with this to solve both your problems. I have tested this. Simple solution.

ie: In your app_list_view.xml file

<LinearLayout     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical"     android:focusableInTouchMode="true">    <EditText         android:id="@+id/filter_edittext"               android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:hint="Search"         android:inputType="text"         android:maxLines="1"/>    <ListView         android:id="@id/android:list"         android:layout_height="fill_parent"        android:layout_weight="1.0"         android:layout_width="fill_parent"         android:focusable="true"         android:descendantFocusability="beforeDescendants"/></LinearLayout> 

------------------ EDIT: To Make keyboard appear on startup -----------------------

This is to make they Keyboard appear on the username edittextbox on startup. All I've done is added an empty Scrollview to the bottom of the .xml file, this puts the first edittext into focus and pops up the keyboard. I admit this is a hack, but I am assuming you just want this to work. I've tested it, and it works fine.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:paddingLeft="20dip"      android:paddingRight="20dip">    <EditText         android:id="@+id/userName"         android:singleLine="true"         android:layout_width="fill_parent"          android:layout_height="wrap_content"         android:hint="Username"          android:imeOptions="actionDone"         android:inputType="text"        android:maxLines="1"       />    <EditText         android:id="@+id/password"         android:password="true"         android:singleLine="true"          android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:hint="Password" />    <ScrollView        android:id="@+id/ScrollView01"          android:layout_height="fill_parent"           android:layout_width="fill_parent">     </ScrollView></LinearLayout>

If you are looking for a more eloquent solution, I've found this question which might help you out, it is not as simple as the solution above but probably a better solution. I haven't tested it but it apparently works. I think it is similar to the solution you've tried which didn't work for you though.

Hope this is what you are looking for.

Cheers!


Try this code.

For showing Softkeyboard:

InputMethodManager imm = (InputMethodManager)                                 getSystemService(Context.INPUT_METHOD_SERVICE);if(imm != null){        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);    }

For Hiding SoftKeyboard -

InputMethodManager imm = (InputMethodManager)                                  getSystemService(Context.INPUT_METHOD_SERVICE);if(imm != null){        imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);    }