-3

I have an EditText in a .xml file with LinearLayout and want to add it as the setView() parameter on an AlertDialog. Is this possible? Here's what I've tried:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
builder.setView(input);

But the dialog is blank when launched. What am I doing wrong?

Alex Newman
  • 1,369
  • 12
  • 34

2 Answers2

0

Use it like this

AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.your_layout, null);
builder.setView(dialogView);

AlertDialog alertDialog = builder.create();
alertDialog.show();

using dialogView you can get your EditText like this

EditText editText = (EditText) dialogView.findViewById(R.id.your_edit_text);
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
0

It might help

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
   LayoutInflater inflater = getLayoutInflater();
builder.setView(inflater.inflate(R.layout.YourLayout, null));
AlertDialog ad = builder.create();
Animesh Jena
  • 1,541
  • 1
  • 25
  • 44