0

Here is my problem. I would like to have an AlertDialog that have a title and a positive button. I want to describe the content of the AlertDialog in a XML file (except for the title/button). I have created a file called dlg_addpwd.xml in my layout resources. Here is the code I'm using:

 AlertDialog alert = new AlertDialog.Builder(this); alert.setTitle("Password access"); alert.setView(findViewById(R.layout.dlg_addpwd)); alert.setPositiveButton("Add", listenAddPwdDlg); alert.show(); 

I guess the line

 alert.setView(findViewById(R.layout.dlg_addpwd)); 

is wrong, isn't it ? So the main idea of my question is: how to define as a view for an AlertDialog a view described in a XML file?

Thanks

Vincent

2 Answers 2

7
 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.paypaldialog, (ViewGroup) findViewById(R.id.yourDialog)); AlertDialog.Builder builder = new AlertDialog.Builder(YourClass.this) .setView(layout); alertDialog = builder.create(); alertDialog.show(); 

that is how I do it.

Sign up to request clarification or add additional context in comments.

Comments

1

See my answer to Customizing the Alert dialog in Android to create a custom Alert Dialog.

The main problem with your code is that you don't want to use AlertDialog.Builder. Instead you want to create a new Dialog, and use setContentView() to render your XML.

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.