553

I want to automatically show the soft-keyboard when an EditText is focused (if the device does not have a physical keyboard) and I have two problems:

  1. When my Activity is displayed, my EditText is focused but the keyboard is not displayed, I need to click again on it to show the keyboard (it should be displayed when my Activity is displayed).

  2. And when I click done on the keyboard, the keyboard is dissmissed but the EditText stays focused and y don't want (because my edit is done).

To resume, my problem is to have something more like on the iPhone: which keep the keyboard sync with my EditText state (focused / not focused) and of course does not present a soft-keyboard if there is a physical one.

2
  • I just have a basic EditText like: <EditText android:id="@+id/myEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:imeOptions="actionDone" /> And on my activity I have this: EditText editTxt = (EditText) findViewById(R.id.myEditText); editTxt.requestFocus(); Commented Feb 24, 2011 at 14:40
  • 2
    This helped me better than any answer in this post : stackoverflow.com/a/2418314/1491212 Commented Sep 30, 2013 at 18:07

49 Answers 49

1
2
3

I use this extension function for showing the keyboard with Kotlin.

fun EditText.requestFocusWithKeyboard() { post { dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0f, 0f, 0)) dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 0f, 0f, 0)) setSelection(length()) } } 

You can just call it as:

editText.requestFocusWithKeyboard() 

Besides being very convenient:

  1. You don't have to worry about closing it in all the possible cases, such as when you navigate back with InputMethodManager.SHOW_FORCED.
  2. In addition to request focus, it also sets the selection (cursor to the end of text)
Sign up to request clarification or add additional context in comments.

Comments

2

If EditText is inside Recycler or ListView and/or this have disable state use below code.

public static void showKeyboardByFocus(final View view) { view.requestFocus(); InputMethodManager keyboard = SystemMaster.getInputMethodManager(); keyboard.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); Runnable re = new Runnable() { @Override public void run() { view.setEnabled(true); view.requestFocus(); } }; Handler h = new Handler(Looper.getMainLooper()); h.postDelayed(re, 360); } 

2 Comments

This helped me! Curious: where did this 360 constant come from?
360 There is a delay time that I reached with a few tests, Better not to reduce so that the code does not give an error.
2

For kotlin extension use below.

fun EditText.toggle() { requestFocus() val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager imm.showSoftInput(this, 0) } 

Access it by:

editText.toggle() 

2 Comments

works for me works with Context instead of Activity, probably because I am in a Fragment. But the keyboard appearance is a bit messy: instead of floating above the view, it pushes up the bottom of the view, hiding pat of the view with the edittext in it, which I solved giving some elevation to the lattest.
Issue with keyboard pushing bottom of the view is solved here : [stackoverflow.com/a/4208067/23159772]
1

I discovered a strange behaviour, since in one of my apps, the soft keyboard was automatically showing on entering the activity (there is an editText.requestFocus() in onCreate).

On digging further, I discovered that this was because there is a ScrollView around the layout. If I remove the ScrollView, the behaviour is as described in the original problem statement: only on clicking the already focused editText does the soft keyboard show up.

If it doesn't work for you, try putting in a ScrollView -- it's harmless anyway.

Comments

1

I had a similar problem using view animations. So I've put an animation listener to make sure I'd wait for the animation to end before trying to request a keyboard access on the shown edittext.

 bottomUp.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { if (textToFocus != null) { // Position cursor at the end of the text textToFocus.setSelection(textToFocus.getText().length()); // Show keyboard InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(textToFocus, InputMethodManager.SHOW_IMPLICIT); } } @Override public void onAnimationRepeat(Animation animation) { } }); 

Comments

1

You can also create a custom extension of the EditText that knows to open the soft keyboard when it receives focus. That's what I've ended up doing. Here's what worked for me:

public class WellBehavedEditText extends EditText { private InputMethodManager inputMethodManager; private boolean showKeyboard = false; public WellBehavedEditText(Context context) { super(context); this.initializeWellBehavedEditText(context); } public WellBehavedEditText(Context context, AttributeSet attributes) { super(context, attributes); this.initializeWellBehavedEditText(context); } public WellBehavedEditText(Context context, AttributeSet attributes, int defStyleAttr) { super(context, attributes, defStyleAttr); this.initializeWellBehavedEditText(context); } public WellBehavedEditText(Context context, AttributeSet attributes, int defStyleAttr, int defStyleRes) { super(context, attributes, defStyleAttr, defStyleRes); this.initializeWellBehavedEditText(context); } private void initializeWellBehavedEditText(Context context) { this.inputMethodManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); final WellBehavedEditText editText = this; this.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if(showKeyboard) { showKeyboard = !(inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_FORCED)); } } }); } @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { if(!focused) this.showKeyboard = false; super.onFocusChanged(focused, direction, previouslyFocusedRect); } @Override public boolean requestFocus(int direction, Rect previouslyFocusedRect) { boolean result = super.requestFocus(direction, previouslyFocusedRect); this.showKeyboard = true; final WellBehavedEditText self = this; this.post(new Runnable() { @Override public void run() { showKeyboard = !(inputMethodManager.showSoftInput(self, InputMethodManager.SHOW_FORCED)); } }); return result; } } 

1 Comment

Oh god at last, the addOnGlobalLayoutListener did the trick! I don't have to resort to ugly input hacks 🙄 Thank you! I get a few warnings in the logs though 🤔
1

call requestFocus() method on editText in onCrete() method of activity() and call clearFocus() method on the same editText when click done in keypad.

Comments

1

This is wild, but actually does work

fun showKeyboard(view: View) { try { InputMethodManager::class.java.getMethod( "showSoftInputUnchecked", Int::class.javaPrimitiveType, ResultReceiver::class.java ).apply { isAccessible = true invoke(view.context.inputMethodManager, 0, null) } } catch (e: Exception) { e.printStackTrace() } } 

Comments

1

I use timer. Keyboard can show when edittext isfocused.

 edittext = (EditText) findViewById(R.id.edittext ); edittext.requestFocus(); edittext.setFocusableInTouchMode(true); if (edittext.requestFocus()) { final Thread timer = new Thread() { public void run() { try{ sleep(500); InputMethodManager imm =(InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE); imm.showSoftInput(edittext, SHOW_IMPLICIT); } catch (Exception e) { e.printStackTrace(); } } }; timer.start(); 

1 Comment

Thanks brother... this worked for me.... i was trying to do this for last 2 days.... Thanks a lot...
1

to show keyboard forcefully use this code:

editText.requestFocus(); InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED); 

Comments

1

After trying every individual answer here the keyboard doesn't show up.. I got hours to get this solved, so hope someone doesn't waste it in the future..

For my case, the issue was not a programming issue at at, I was testing on an emulator with a phone that has a hardware keyboard, so it doesn't show up the software keyboard by default, to solve this, you need to make sure that the show soft input enabled in your emulator by turning off the hardware keyboard or enable the soft keyboard.

In faced this only on API-15 & 16. Here are screen shots how to do that

On Geny Motion emulator:

enter image description here enter image description here

On Android Studio emulator:

enter image description here

Comments

1

view.requestFocus() might not work for diffrent reasons. So the Keyboard will not show.

A view will not actually take focus if it is not focusable (isFocusable returns false), or if it can't be focused due to other conditions (not focusable in touch mode (isFocusableInTouchMode) while the device is in touch mode, not visible, not enabled, or has no size).

I used this solution:

 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); EditText searchView = findViewById(R.id.searchView); new Handler().postDelayed(new Runnable() { @Override public void run() { showSoftKeyboard(searchView); } }, 300); } public void showSoftKeyboard(View view) { if (view.requestFocus()) { InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); } } 

Comments

1

I made this help class. Just pass the context and the View you want to focus and show keyboard and after hide keyboard.

public class FocusKeyboardHelper { private View view; private Context context; private InputMethodManager imm; public FocusKeyboardHelper(Context context, View view){ this.view = view; this.context = context; imm = (InputMethodManager) context.getSystemService(context.INPUT_METHOD_SERVICE); } public void focusAndShowKeyboard(){ view.requestFocus(); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); } public void hideKeyBoard(){ imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } } 

Comments

1

I am agree with raukodraug therefore using in a swithview you must request/clear focus like this:

final ViewSwitcher viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher); final View btn = viewSwitcher.findViewById(R.id.address_btn); final View title = viewSwitcher.findViewById(R.id.address_value); title.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { viewSwitcher.showPrevious(); btn.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(btn, InputMethodManager.SHOW_IMPLICIT); } }); // EditText affiche le titre evenement click btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { btn.clearFocus(); viewSwitcher.showNext(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(btn.getWindowToken(), 0); // Enregistre l'adresse. addAddress(view); } }); 

Comments

1

According to this answer I used the setSoftInputMode method and overrided theese methods in DialogFragment:

@Override public void onCancel(@NonNull DialogInterface dialog) { super.onCancel(dialog); requireDialog().getWindow().setSoftInputMode(InputMethodManager.HIDE_IMPLICIT_ONLY); } @Override public void onStart() { super.onStart(); requireDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); } @Override public void onStop() { super.onStop(); requireDialog().getWindow().setSoftInputMode(InputMethodManager.HIDE_IMPLICIT_ONLY); } 

I also made my own subclass of DialogFragment with theese methods, so when you create another dialog and inherit from this class, you have the soft keyboard showed automatically without any other edits.

Comments

0

Using Xamarin, this works for me inside a Fragment:

using Android.Views.InputMethods; using Android.Content; ... if ( _txtSearch.RequestFocus() ) { var inputManager = (InputMethodManager) Activity.GetSystemService( Context.InputMethodService ); inputManager.ShowSoftInput( _txtSearch, ShowFlags.Implicit ); } 

Comments

0
 void requestFocus(View editText, Activity activity) { try { editText.requestFocus(); InputMethodManager imm = (InputMethodManager) a.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); } catch (Exception e) { e.printStackTrace(); } } 

add this line too do not forget

activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 

Comments

0

Worked for me after adding this lines.

globalSearchBarMainActivity.setEnabled(true); globalSearchBarMainActivity.requestFocus(); 

As my auto complete text view method was hidden i.e. VISIBLE: GONE

So needed to add above two line

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

Comments

0

Yet another answer to this question for after api level 30 is something like this:

 binding.myEditText.windowInsetsController?.show(WindowInsets.Type.ime()) 

PS: Old toggleSoftInput call is doing fine on api 32 but not working on api level 33 at all. At least according to my samsung devices.

And here is the source (kinda) The guy is referring to the stackoverflow question that 11 years old!

Comments

1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.