Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog or Custom Dialog

I'm developing an Android 2.2 application.

I want to show a text with an Ok button but I want to add my custom style.

What is the better choice an AlertDialog with a custom layout or a Dialog?

like image 306
VansFannel Avatar asked Feb 07 '11 13:02

VansFannel


1 Answers

I'd go for AlertDialog:

  • It's easier to implement.
  • The only custom thing you have to do is an XML layout and then inflate it.

AlertDialog dialog = new AlertDialog.Builder(this)
    .setView(getLayoutInflater().inflate(R.layout.custom_dialog, null))
    .create();

In order to listen for UI events:

View view = getLayoutInflater().inflate(R.layout.custom_dialog, null);
Button btn = (Button)view.findViewById(R.id.the_id_of_the_button);
btn.setOnClickListener(blah blah);
AlertDialog dialog = new AlertDialog.Builder(this)
    .setView(view)
    .create();

You can check in android dialog docs:

The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly. Instead, use one of the following subclasses:

like image 51
Cristian Avatar answered Nov 09 '22 22:11

Cristian