Android: make keyboard appear and focus on EditText

Thanks in advance for your help.

I would like the keyboard to appear at the end or while executing the following code.

// edit text EditText editText = new EditText(activity); editText.setBackgroundColor(Color.WHITE); editText.setGravity(Gravity.TOP); editText.setText("Type here..."); RelativeLayout relativeLayoutEditText = new RelativeLayout(activity); RelativeLayout.LayoutParams paramRelativeLayoutEditText = new RelativeLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, 43 * display.getHeight() / 80 ); paramRelativeLayoutEditText.addRule(RelativeLayout.ALIGN_PARENT_TOP); relativeLayoutEditText.addView(editText,paramRelativeLayoutEditText); // add all created views to rootView rootView.addView(relativeLayoutEditText, paramEditText); // open keyboard InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); 

but the keyboard appears only after touching the editText field (i.e. with a finger). Is there a way to make the panel automatically appear without a physical touch?

As an aside, I know that as I indicate width and height, this is not quite the right way to do something.

+6
source share
5 answers

Thanks to the @Shubham link, I was able to figure this out. However, the solution was not the answer referenced in the link. This was the second answer.

 editText.requestFocus(); InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); 

Edit:

after using the above solution, the keyboard will remain on the screen until the user presses the back button or the home button (sometimes this takes several times). To remove the keyboard, use

 imm.toggleSoftInputFromWindow(rootView.getWindowToken(), 0,0); 

in my case, rootView is the rootView of the current activity. I have not tested this to see if this will work on child views.

+7
source

Add

  editText.requestFocus(); 

Try the following:

 EditText editText = (EditText ) findViewById(R.id.myTextViewId); editText.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); 

make sure edittext is configured in touch mode. You can do this in two ways.

In xml:

  android:focusableInTouchMode="true" 

in Java:

 editText.setFocusableInTouchMode(true); 
+3
source

try it

 EditText textView = (EditText ) findViewById(R.id.myTextViewId); textView.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT); 

http://developer.android.com/reference/android/view/View.html#requestFocus ()

0
source

Possible response link . Many of the above sources are also obtained from this link.

Try the following:

 EditText textView = (EditText )findViewById(R.id.myTextViewId); textView.requestFocus(); InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT); 
0
source

it will show the keyboard

 InputMethodManager imm = (InputMethodManager) OrderMainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(txtCustomerId, InputMethodManager.SHOW_IMPLICIT); 

enter the name of your activity instead if you call the keyboard on listeners, for example. press the button

-2
source

All Articles