Struggling to Launch Numeric keypad on activity start

2.2k views Asked by At

I have few TextView's in my activity, when activity starts i want to show soft numeric keypad or that activity should always show keypad.

I have tried:

  1. To set android:windowSoftInputMode="stateAlwaysVisible" in manifest for activity but didn't worked.
  2. Added this code in activity onCreate, it shows the alphanumeric keypad but not numeric.

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

Need help on this.

3

There are 3 answers

0
Kanika On

I have tried this for an EditText in a dynamic way...The method would be the same for TextView also..When activity starts,give focus on that textView ,so that it can show u the keypad

    boolean checkFocus=EditText.requestFocus();
    Log.i("CheckFocus", ""+checkFocus);
    if(checkFocus==true)
    {
    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    EditText.setInputType(InputType.TYPE_CLASS_PHONE); [select Input type as according to ur requirement]
    mgr.showSoftInput(EditText, InputMethodManager.SHOW_IMPLICIT);
    }
0
Noby On

Place edit text in your xml file as first child.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             ............
               .........>
 <EditText android:layout_width="0dp"
    android:layout_height="0dp"/>
..............
................
</LinearLayout>
0
Rana Ranvijay Singh On

To pop up a numeric keyboard on start of the activity i used following steps:

Created edit text field in layout as:

<EditText
        ...
        android:inputType="number"    
        ...  />

but since you need the key board to be poped without any edit text so give

 <EditText
        ...
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:inputType="number"    
        ...  />

In function onCreate() show soft keyboard

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

Most important is to give focus to edit text in onResume method.

    @Override
    public void onResume() {
        super.onResume();
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
    }