androiddialogandroid-themeandroid-dialogappcompatactivity

Dialog on AppCompatActivity has no title. Why?


I'm compiling with API 27 and displaying a Dialog on a AppCompatActivity with this theme:

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

And the theme for the full application is this:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

I'm displaying the dialog using this code:

final Dialog dialog = new Dialog(mContext);
dialog.setTitle("Valorar " + APP_TITLE);
dialog.setContentView(sv);        
dialog.show(); 

The problem is that the dialog is shown without title and I don't understand why.

Am I doing something wrong with the Activity theme or the Dialog creation logic?


Solution

  • I recommend using AlertDialog instead of just a plain Dialog. If you use the support library version of AlertDialog, you'll also get material design (instead of Holo) on older API levels.

    This will also work around your activity's theme clobbering the dialog's title.

    For you, the change is simple:

    new AlertDialog.Builder(mContext)
            .setTitle("Valorar " + APP_TITLE)
            .setView(sv)
            .show();