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();
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.
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.
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.
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.
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();
You need to add this line:
inputText.setRawInputType(Configuration.KEYBOARD_12KEY);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With