11

Thanks in advance for the help.

I would like a keyboard to appear at the end or during the execution of 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 only appears after touching the editText field (ie with my finger). Is there a way that I can make the board automatically appear without using a physical touch?

As an aside I know that how I am specifying the width and height isn't exactly the right way to do things.

HXSP1947
  • 1,311
  • 1
  • 16
  • 39
  • editText.requestFocus(); can you add this code after setTextMethod()? – msevgi Aug 26 '15 at 08:41
  • I've tried that already. editText gets focus but the keyboard still doesn't appear until I touch the screen. – HXSP1947 Aug 26 '15 at 08:42
  • Possible answer : http://stackoverflow.com/a/8991563/3913366 – Shubham A. Aug 26 '15 at 08:52
  • That did it @Shubham!. It wasn't the accepted answer though. It was the second answer down (ie imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); ). If you write up an answer I will accept it. – HXSP1947 Aug 26 '15 at 08:58

5 Answers5

20

Thanks to @Shubham's link I was able to figure it out. The solution was not the answer given in the link, however. It 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:

once using the above solution the keyboard will remain on the screen until a user presses either the back button or the home button (sometimes it takes a few times). To remove the keyboard use this

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.

HXSP1947
  • 1,311
  • 1
  • 16
  • 39
5

Add

    editText.requestFocus();

Try this:

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

ensure that the edittext is focusable in touch mode. You can do it two way.

In xml:

 android:focusableInTouchMode="true"

in Java:

editText.setFocusableInTouchMode(true);
sasikumar
  • 12,540
  • 3
  • 28
  • 48
  • See my comment above. – HXSP1947 Aug 26 '15 at 08:44
  • 1
    Same thing. editText gets focus but the the keyboard doesn't appear until I touch the screen. – HXSP1947 Aug 26 '15 at 08:48
  • The problem isn't really getting it to focus. editText.requestFocus(); seems to work doing that by itself, but the keyboard is staying hidden until I physically touch the screen. – HXSP1947 Aug 26 '15 at 08:50
  • android:windowSoftInputMode="stateAlwaysVisible" add in activity manifest – sasikumar Aug 26 '15 at 08:52
  • I tried that previously but I can't have the keyboard always visible (I'm not really using xml since my code is fairly dynamic). I would assume that it is possible to do the same with code which is what I've been looking at. – HXSP1947 Aug 26 '15 at 08:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87963/discussion-between-sasikumar-and-user2736423). – sasikumar Aug 26 '15 at 08:53
  • I just found a solution thanks to @Shubham. See his link and the second answer down. – HXSP1947 Aug 26 '15 at 08:59
1

Try This

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()

Akshay Sood
  • 152
  • 1
  • 3
  • 15
0

Possible answer link. Many of the above are also sourced from this link.

Try This :

EditText textView = (EditText )findViewById(R.id.myTextViewId);
textView.requestFocus();    
InputMethodManager imm = 
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT);
Community
  • 1
  • 1
Shubham A.
  • 2,446
  • 4
  • 36
  • 68
0

this will show the keypad

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

enter your activity name instead of this if you calling keypad from the listeners eg. button click

Vega
  • 27,856
  • 27
  • 95
  • 103