How to insert editText number in AlertDialog Android

bircastri picture bircastri · Oct 16, 2013 · Viewed 11.4k times · Source

i have this problem. I have create a small app Android.

I show a AlertDialog.Builder with EditText, so the user must click on the EdiText, select Number 123 then insert a int number.

I would like to show a keyboard with only number. Can we Help me? It's possible to create a AlertDialog with automaticaly focus?

I have write this code. Can we help me?

AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setTitle("Inserisci quantità");
                alert.setMessage("Inserisci una quantità per l'articolo: "+articolo.getNomeArticolo());
                final EditText inputText = new EditText(this);
                alert.setView(inputText);
                alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                                String value = inputText.getText().toString();
                                try{
                                        int quantita = Integer.parseInt(value);
                                        ArticoliOrdine articoloOrdine = new ArticoliOrdine();
                                        articoloOrdine.setIdArticolo(articolo.getCodArticolo());
                                        articoloOrdine.setNomeArticolo(articolo.getNomeArticolo());
                                        articoloOrdine.setQuantia(quantita);
                                        listaArticoli.add(articoloOrdine);

                                        adapter.notifyDataSetChanged();
                                }catch(Exception e){
                                        AlertDialog.Builder alertErrore = new AlertDialog.Builder(getApplicationContext());
                                        alertErrore.setTitle("Errore");
                                        alertErrore.setMessage("Hai inserito una quantità non valida.");
                                        alertErrore.show();

                                }

                        }
                });


                // Showing Alert Message
                alert.show();

Answer

Josh picture Josh · Jun 19, 2014

This code is what you need. Just insert it wherever you need to launch the alert dialog. I haven't figured out how to launch the keyboard automatically , but it shouldn't be difficult.

AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setTitle(multiLangTranslation(R.string.manualshippermessage));
                final EditText input = new EditText(this);
                input.setInputType(InputType.TYPE_CLASS_NUMBER);
                input.setRawInputType(Configuration.KEYBOARD_12KEY);
                alert.setView(input);  
                alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                  //Put actions for OK button here
                  }
                });
                alert.setNegativeButton(multiLangTranslation(R.string.cancel), new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
                      //Put actions for CANCEL button here, or leave in blank
                  }
                });
                alert.show();