NullPointerException
NullPointerException

Reputation: 37681

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?

Upvotes: 0

Views: 118

Answers (3)

Nonos
Nonos

Reputation: 2518

I've had the same issue, and you're correct. Alert dialogs don't show a title bar in a NoActionBar activity, I had to specifically define an alertDialogTheme within my NoActionBar theme

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="alertDialogTheme">@style/<your dialog theme></item>
</style>

you could use Theme.AppCompat.Light.Dialog.Alert (or similar) directly above or define your own with it as the parent

Upvotes: 0

Santanu Sur
Santanu Sur

Reputation: 11487

 <item name="windowNoTitle">true</item>

This disables the title, set it to false ..

 <item name="windowNoTitle">false</item>

From your Activity theme

Programatically you can try :-

   final Dialog dialog = new Dialog(mContext);
   Window window = dialog.getWindow();
    if (window != null) {
        window.requestFeature(Window.FEATURE_NO_TITLE);
    }

Upvotes: 0

Ben P.
Ben P.

Reputation: 54214

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

Upvotes: 1

Related Questions