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.
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?
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 .
Please follow the code below. I am sure that your problem will be solved.
if (imm != null){ imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); } It works
<activity ... android:windowSoftInputMode="stateVisible" > </activity> or
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 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(); } }); 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); } 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.
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; } }); 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
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.
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); 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); 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); } 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); 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
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); Use the above code in onResume () to open a soft keyboard
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); 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
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
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); } } 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); } } }