Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show soft keyboard (numeric) using AlertDialog.Builder

I have an EditText in an AlertDialog, but when it pops up, I have to click on the text box before the keyboard pops up. This EditText is declared in the XML layout as "number", so when the EditText is clicked, a numeric keypad pops up. I want to eliminate this extra tap and have the numeric keypad pop up when the AlertDialog is loaded.

All of the other solutions I found involve using

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

This is NOT an acceptable solution, as this results in the standard keyboard rather than the numeric keyboard popping up. Does anyone have a way to make the numeric keyboard pop up in an AlertDialog, preferably while keeping my layout defined in XML?

AlertDialog dialog = new AlertDialog.Builder(this)
    .setTitle("Mark Runs")
    .setView(markRunsView)
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {                        
        @Override
        public void onClick(DialogInterface dialog, int which) {
            EditText runs = (EditText)markRunsView.findViewById(R.id.runs_marked);
            int numRuns = Integer.parseInt(runs.getText().toString());
         // ...
        })
    .setNegativeButton("Cancel", null)
    .show();

Edit: I want to be perfectly clear that my layout already has:

android:inputType="number"
android:numeric="integer"

I also tried this:

//...
.setNegativeButton("Cancel", null)
.create();
EditText runs = (EditText)markRunsView.findViewById(R.id.runs_marked);
runs.setInputType(InputType.TYPE_CLASS_NUMBER);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();

but that also did not work. With the setSoftInputMode line, I still get the full keyboard when the AlertDialog loads; without it, I still get nothing. In either case, tapping on the text box will bring up the numeric keypad.

Edit Again:

This is the XML for the EditText

<EditText
    android:id="@+id/runs_marked"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="4dip"
    android:inputType="number"
    android:numeric="integer">
    <requestFocus/>
</EditText>
like image 986
Michael Q Avatar asked Oct 17 '25 13:10

Michael Q


2 Answers

Coming a bit late, but today I had this same problem. This is how I solved it:

Call the keyboard when the Dialog opens just like you did:

 dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

Now, into the Dialog I'm assuming you have a EditText field that accepts only numbers. Just request focus on that field and the standard keybord will automatically transform into the numeric keyboard:

final EditText valueView = (EditText) dialogView.findViewById(R.id.editText);
valueView.requestFocus();

Now you just have to remember to dismiss the keyboard once you're done with the Dialog. Just put this in the positive/negative/neutral button click listener:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(valueView.getWindowToken(), 0);
like image 110
Ruocco Avatar answered Oct 19 '25 02:10

Ruocco


Just try to set the InputType by using setInputType().

EditText runs = (EditText)markRunsView.findViewById(R.id.runs_marked);
runs.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
like image 31
Paresh Mayani Avatar answered Oct 19 '25 02:10

Paresh Mayani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!