Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert editText number in AlertDialog Android

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();
like image 925
bircastri Avatar asked Oct 16 '13 13:10

bircastri


People also ask

How to add edittext to alertdialog box in Android?

Step 1: Create a XML file: custom_layout.xml. Add the below code in custom_layout.xml. This code defines the alertdialog box dimensions and add a edittext in it. Step 2: Add a button in activity_main.xml.

How to add custom alert dialog in Java?

Add custom_layout.xml in that activity in which you want to show custom alert dialog here it is added in MainActivity.java. Use Up/Down Arrow keys to increase or decrease volume.

How to implement alert dialog box in Salesforce?

Below is the step by step implementation of the above approach: Step 1: Create a XML file: custom_layout.xml. Add the below code in custom_layout.xml. This code defines the alertdialog box dimensions and add a edittext in it. Step 2: Add a button in activity_main.xml. The button when clicked will show the AlertDialog box.

How to retrieve the edittext with the ID from an activity?

Provide the EditText with the id, by referring to the following code, which has to be invoked inside the activity_main.xml file. The following code needs to be invoked inside the MainActivity.kt file. Which performs the retrieving operation and provides the Toast message the same as the entered data.


2 Answers

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();
like image 126
Josh Avatar answered Oct 10 '22 12:10

Josh


You need to add this line:

inputText.setRawInputType(Configuration.KEYBOARD_12KEY);
like image 29
Mikel Avatar answered Oct 10 '22 11:10

Mikel