Search code examples
androiddialogandroid-alertdialogandroid-2.2-froyo

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?


Solution

  • 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: