...">

Open soft keyboard programmatically

I have activity without child widgets, and the corresponding xml file,

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myLayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:focusable="true" > </LinearLayout> 

and I want the program to open programmatically while the activity starts. And what I have tried so far,

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (inputMethodManager != null) { inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); } 

Give me some tips.

+108
android android-softkeyboard
Apr 08 2018-11-11T00:
source share
21 answers

I used the following lines to display the soft keyboard manually inside the onclick event, and the keyboard is visible.

 InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInputFromWindow( linearLayout.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0); 

But I still can’t open it while the activity opens, just as there is a solution for this?

+142
Apr 11 2018-11-11T00:
source share

In your manifest file, try adding the following to the <activity> that you want to display on the keyboard at startup:

android:windowSoftInputMode="stateVisible"

This will cause the keyboard to become visible when the action begins.

See the documentation for more information .

+114
Oct 13 '11 at 14:15
source share

Please follow the code below. I am sure that your problem will be solved.

 if (imm != null){ imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); } 
+34
Apr 11 2018-11-11T00:
source share

It works

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

or

 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 
+24
Sep 06 '13 at 1:29
source share

All I needed was to expose the keyboard at a very precise moment. It worked for me! Thanks Benitez.

  private Handler mHandler= new Handler(); 

And at the most accurate moment:

  mHandler.post( new Runnable() { public void run() { InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInputFromWindow(yourEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0); yourEditText.requestFocus(); } }); 
+16
Aug 24 '12 at 7:16
source share

I used the following lines to display the soft keyboard manually inside the onclick event.

 public void showKeyboard(final EmojiconEditText ettext){ ettext.requestFocus(); ettext.postDelayed(new Runnable(){ @Override public void run(){ InputMethodManager keyboard=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); keyboard.showSoftInput(ettext,0); } } ,200); } 
+12
Jan 9 '15 at 10:28
source share

Put this in the onResume method:

 findViewById(R.id.root_view_of_your_activity_layout).post( new Runnable() { public void run() { InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInputFromWindow(yourEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0); yourEditText.requestFocus(); } }); 

it needs to be runnable because when the OS starts the onResume method, you cannot be sure that all the views are drawn, so the post method called from the root layout keeps you waiting for each view to be ready.

+9
Jul 19 '12 at 14:57
source share

in the onCreate action method or onActivityCreated fragment

 .... view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { view.removeOnPreDrawListener(this); InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); // !Pay attention to return `true` // Chet Haase told to return true; } }); 
+8
Feb 06 '15 at 11:08
source share

it looks like it works

  protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_patientid); editText = (EditText)findViewById(R.id.selectPatient); //editText.requestFocus(); //works without that } @Override protected void onResume() { findViewById(R.id.selectPatient).postDelayed( new Runnable() { public void run() { editText.requestFocus(); InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.showSoftInput(editText,InputMethodManager.SHOW_IMPLICIT); } },100); super.onResume(); } 

This seems to work better: in the manifest:

 <application> <activity android:name="com.doodkin.myapp.ReportActivity" android:label="@string/title_activity_report" android:screenOrientation="sensor" android:windowSoftInputMode="stateHidden" > // add this or stateVisible </activity> </application> 

it seems that the manifest works in android 4.2.2 but does not work in android 4.0.3

+7
Jul 04 '14 at 23:32
source share

I used this to programmatically display the software keyboard, and it worked for me to prevent the screen from automatically resizing when the keyboard starts.

In the manifest:

 <activity android:name="XXXActivity" android:windowSoftInputMode="adjustPan"> </activity> 

In XXXActvity:

 EditText et = (EditText))findViewById(R.id.edit_text); Timer timer = new Timer(); TimerTask task = new TimerTask() { @Override public void run() { InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInputFromWindow(et.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0); } }; timer.schedule(task, 200); 

I assume that this will save time for finding this problem.

+6
Oct. 14 '12 at 13:30
source share
 InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); 
+3
Jan 29 '16 at 16:07
source share

I used it as singleton like:

 public static void showSoftKeyboard(final Context context, final EditText editText) { try { editText.requestFocus(); editText.postDelayed( new Runnable() { @Override public void run() { InputMethodManager keyboard = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); keyboard.showSoftInput(editText, 0); } } , 200); } catch (NullPointerException npe) { npe.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } 

Use it in your activities, for example:

 showSoftKeyboard(this, yourEditTextToFocus); 
+2
Dec 01 '17 at 7:21
source share

Kotlin

 fun hideKeyboard(activity: Activity) { val view = activity.currentFocus val methodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager assert(view != null) methodManager.hideSoftInputFromWindow(view!!.windowToken, InputMethodManager.HIDE_NOT_ALWAYS) } private fun showKeyboard(activity: Activity) { val view = activity.currentFocus val methodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager assert(view != null) methodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT) } 

Java

 public static void hideKeyboard(Activity activity) { View view = activity.getCurrentFocus(); InputMethodManager methodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); assert methodManager != null && view != null; methodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } private static void showKeyboard(Activity activity) { View view = activity.getCurrentFocus(); InputMethodManager methodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); assert methodManager != null && view != null; methodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); } 
+2
01 Oct '18 at 10:00
source share

Like @ShimonDoodkin's answer, this is what I did in the snippet.

stack overflow

  passwordInput.postDelayed(new ShowKeyboard(), 300); //250 sometimes doesn't run if returning from LockScreen 

Where ShowKeyboard is

 private class ShowKeyboard implements Runnable { @Override public void run() { passwordInput.setFocusableInTouchMode(true); passwordInput.requestFocus(); getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(passwordInput, 0); } } 

After successful input, I also make sure to hide the keyboard

 getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)) .hideSoftInputFromWindow(getView().getWindowToken(), 0); 
+1
Mar 24 '15 at 10:27
source share

This is the required source code:

 public static void openKeypad(final Context context, final View v) { new Handler().postDelayed(new Runnable() { @Override public void run() { InputMethodManager inputManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT); Log.e("openKeypad", "Inside Handler"); } },300);} 

See this link for more details. It helped me. https://github.com/Nikhillosalka/Keyboard/blob/master/README.md

+1
Apr 29 '15 at 13:14
source share
 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 

Use the above code in onResume () to open a soft keyboard

+1
Jun 29 '17 at 11:28
source share

It works:

 private static void showKeyboard(Activity activity) { View view = activity.getCurrentFocus(); InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); } 

And you call this method like this:

 showKeyboard(NameOfActivity.this); 
+1
Feb 24 '19 at 19:48
source share

InputMethodManager.SHOW_FORCED is not a good choice. If you use this option, you must control the hidden state of the keyboard. My suggestion is this:

  public void showSoftKeyboard(View view) { InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE); view.requestFocus(); inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0); } 

In addition, you can focus on the view (usually EditText) by accepting its parameters. This makes it a more useful feature.

for more information about InputMethodManager.SHOW_IMPLICIT and SHOW_FORCED; InputMethodManager

+1
Mar 28 '19 at 15:25
source share

Too many answers, but nothing worked for me except this

 inputMethodManager.showSoftInput(emailET,InputMethodManager.SHOW_FORCED); 

I used showSoftInput with SHOW_FORCED

And my activity

  android:windowSoftInputMode="stateVisible|adjustResize" 

hope this helps someone

0
Mar 05 '17 at 15:43
source share

Publish this method in your core business and use it in other activities such as a talisman

 public void openKeyboard() { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); } } 
0
Jun 17 '18 at 16:29
source share
 public final class AAUtilKeyboard { public static void openKeyboard(final Activity activity, final EditText editText) { final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); } } public static void hideKeyboard(final Activity activity) { final View view = activity.getCurrentFocus(); if (view != null) { final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } } } 
0
Aug 15 '19 at 12:33 on
source share



All Articles