Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple EditText objects in AlertDialog

I'm working on a project for college that will let a user place a point on a map and then set the title and description for the overlay object. The problem is, the second EditText box overwrites the first one. Here is my code for the dialog box.

//Make new Dialog
AlertDialog.Builder dialog = new AlertDialog.Builder(mapView.getContext());
dialog.setTitle("Set Target Title & Description");
dialog.setMessage("Title: ");

final EditText titleBox = new EditText(mapView.getContext());
dialog.setView(titleBox);

dialog.setMessage("Description: ");
final EditText descriptionBox = new EditText(mapView.getContext());
dialog.setView(descriptionBox);

Any help would be appreciated!! Thanks!

like image 972
TomSelleck Avatar asked Oct 13 '12 20:10

TomSelleck


3 Answers

A Dialog only contains one root View, that's why setView() overwrites the first EditText. The solution is simple put everything in one ViewGroup, for instance a LinearLayout:

Context context = mapView.getContext();
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);

// Add a TextView here for the "Title" label, as noted in the comments
final EditText titleBox = new EditText(context);
titleBox.setHint("Title");
layout.addView(titleBox); // Notice this is an add method

// Add another TextView here for the "Description" label
final EditText descriptionBox = new EditText(context);
descriptionBox.setHint("Description");
layout.addView(descriptionBox); // Another add method

dialog.setView(layout); // Again this is a set method, not add

(This is a basic example, but it should get you started.)

You should take note of the nomenclature difference between a set and add method. setView() only holds one View, the same is similar for setMessage(). In fact this should be true for every set method, what you're thinking of are add commands. add methods are cumulative, they build a list of everything you push in while set methods are singular, they replace the existing data.

like image 144
Sam Avatar answered Nov 04 '22 16:11

Sam


You can build your layout that contains two EditText, inflate it with a LayoutInflater and use that as the View of your AlertDialog.

LayoutInflater factory = LayoutInflater.from(this);

//text_entry is an Layout XML file containing two text field to display in alert dialog
final View textEntryView = factory.inflate(R.layout.text_entry, null);

final EditText input1 = (EditText) textEntryView.findViewById(R.id.EditText1);
final EditText input2 = (EditText) textEntryView.findViewById(R.id.EditText2);


input1.setText("DefaultValue", TextView.BufferType.EDITABLE);
input2.setText("DefaultValue", TextView.BufferType.EDITABLE);

final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setIcon(R.drawable.icon).setTitle("EntertheText:").setView(textEntryView).setPositiveButton("Save",
  new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog,
     int whichButton) {

    Log.i("AlertDialog","TextEntry 1 Entered "+input1.getText().toString());
    Log.i("AlertDialog","TextEntry 2 Entered "+input2.getText().toString());
    /* User clicked OK so do some stuff */
   }
  }).setNegativeButton("Cancel",
  new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog,
     int whichButton) {
     /*
     * User clicked cancel so do some stuff
     */
   }
  });
alert.show();

enter image description here

you can add your EditText programmatically too like this:

LinearLayout layout = new LinearLayout(mapView.getContext());
layout.setOrientation(LinearLayout.VERTICAL);

final EditText titleBox = new EditText(mapView.getContext());
titleBox.setHint("Title");
layout.addView(titleBox);

final EditText descriptionBox = new EditText(mapView.getContext());
descriptionBox.setHint("Description");
layout.addView(descriptionBox);

dialog.setView(layout);
like image 16
K_Anas Avatar answered Nov 04 '22 17:11

K_Anas


val alert = AlertDialog.Builder(this)
    alert.setTitle("Buy Airtime")
    alert.setMessage("Enter phone details and amount to buy airtime.")

    val layout = LinearLayout(this)
    layout.orientation = LinearLayout.VERTICAL

    val mobileNoET = EditText(this)
    mobileNoET.setSingleLine()
    mobileNoET.hint = "Mobile Number"
    layout.addView(mobileNoET)

    val amountET = EditText(this)
    amountET.setSingleLine()
    amountET.hint = "Amount"
    layout.addView(amountET)

    val networkET = EditText(this)
    networkET.setSingleLine()
    networkET.hint = "Network"
    layout.addView(networkET)

    layout.setPadding(50, 40, 50, 10)

    alert.setView(layout)

    alert.setPositiveButton("Proceed") { _, _ ->
        val mobileNo = mobileNoET.text.toString()
        val amount = amountET.text.toString()
        val network = networkET.text.toString()

        Log.i("xxx",mobileNo )
        Log.i("xxx",amount )
        Log.i("xxx",network )

        Toast.makeText(this, "Saved Sucessfully", Toast.LENGTH_LONG).show()
    }

    alert.setNegativeButton("Cancel") { dialog, _ ->
        dialog.dismiss()
    }

    alert.setCancelable(false)
    alert.show()
like image 1
Team chang Avatar answered Nov 04 '22 16:11

Team chang