-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?

0

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

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();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.