Dialog.setTitle not showing a title
Asked Answered
P

4

16

I am trying to add a custom title to my Dialog, however whenever I run my application it doesn't show a title.

My code for creating the dialog is

 final Dialog passwordDialog = new Dialog(this);
 passwordDialog.setContentView(R.layout.admin_password_dialog);
 passwordDialog.setTitle("Enter An Administrative Password");
 passwordDialog.show();

And my layout file is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

<Button
    android:id="@+id/btn_confirmPassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/edit_adminPassword"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:text="@string/confirmPassword"/>

<EditText
    android:id="@+id/edit_adminPassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:ems="10"
    android:inputType="textPassword"/>

And here is what I am getting

Here is what I am getting

Is there something I am missing?

Pet answered 9/2, 2016 at 22:24 Comment(1)
#4853073 might give you a workaroundIngra
B
31

you should define your style like this:

 <style name="Dialog" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">false</item>
    <item name="android:windowIsFloating">true</item>
</style>

and then pass this style to the constructor of the Dialog

final Dialog passwordDialog = new Dialog(this,R.style.Dialog);
Bikini answered 24/12, 2016 at 22:59 Comment(3)
That's called a solution, suggesting to use AlertDialogue instead of dialogue is not the solution, it may be a work around...Uthrop
Why does Android development make me want to bang my head against a desk.Glenoid
@Raafat Alhoumaidy After applying this the dialog background become black with title color as white, Why so??Forestaysail
H
9

Like the other answer, but more concise

final AlertDialog diag = new AlertDialog.Builder(this)
        .setTitle("Enter An Administrative Password")
        .setView(R.layout.admin_password_dialog)
        .create();

diag.show();

Button diagButton = (Button) diag.findViewById(R.id.btn_confirmPassword);
diagButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // handle button click
        EditText input = (EditText) diag.findViewById(R.id.edit_adminPassword);
        String s = input.getText().toString();
    }
});
Harriott answered 9/2, 2016 at 23:8 Comment(10)
@RyanNewman - You could use android.support.v7.app.AlertDialog. If you look at my answer, it is also more inclusive, like how to get the Button and EditText from the dialogHarriott
When I try adding the onClickLIstener I'm getting a null object refernce. Do you know why?Pet
Well, findViewById does return null if it can't find that id. I didn't test this, honestly, but I assumed it would work because of the setViewHarriott
@RyanNewman - Apparently, you have to call show() first, I made the edit... that isn't very intuitive... :/Harriott
Perfect that worked perfectly for my current version of Android. I'm getting a warning on setView that it won't work on older than 5.0 (API 20) but that is something I am currently looking intoPet
Like the other guy said, that method should be supported since API 1. I think Android Studio is confused. Mostly, you can safely ignore the warnings until they are actually a large problem.Harriott
If you are using import android.app.AlertDialog; instead of the support library version I mentioned, I would believe that is where the warning exists.Harriott
When I put it up on a 4.4.4 simulator it crashes which I'm not sure whyPet
I was confused on what you meant by the support library. It works now. Thank you!Pet
In gradle, you can add compile 'com.android.support:appcompat-v7:23.1.1', but as long as it works, that's goodHarriott
I
5

You can try this method as well and get different view styles based upon theme used.

<style name="FilterDialogTheme" parent="@android:style/Theme.Holo.Light.Dialog">
    <item name="android:windowNoTitle">false</item>
</style>

In Dialog constructor

  public FilterDialog(Context context) {
        super(context, R.style.FilterDialogTheme);
    }

Use @style/Theme.Appcompat.Light.Dialog for your project.

Insalubrious answered 1/8, 2016 at 9:0 Comment(0)
D
2

You should use an AlertDialog.Builder instead of just creating a Dialog:

// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

// 2. Chain together various setter methods to set the dialog characteristics
builder.setView(R.layout.admin_password_dialog);
builder.setTitle("Enter An Administrative Password");

// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();
dialog.show();

See here for the Android Developers Guide on Dialogs.

Dilisio answered 9/2, 2016 at 22:58 Comment(8)
I wasn't sure if reassigning the builder for the set methods was necessary, so I made my own answer, just in case :)Harriott
This works perfectly for my device, but I'm getting a warning this won't work for phones lower than Lollipop. My current minimum API Level is 16, is this going to work on devices below lollipop?Pet
I'm not quite sure why you get that warning... AlertDialog has been added in API Level 1 (according to the docs) and the used methods should also be there since API Level 1Dilisio
and I'm absolutely, positively sure I used that in many older projects... strangeDilisio
Yeah I threw it up on a 4.4.4 emulator and I get java.lang.NoSuchMethodError: android.app.AlertDialog$Builder.setViewPet
I figured it out. I added the layout into the default constructor new AlertDialog.Builder(this, R.layout.admin_password_dialog) and it works now. Thank you for your helpPet
@RyanNewman - Looked at the wrong doc the first time. Should be an R.style theme resource for that second paramHarriott
I actually take that back. When I added the layout into the constructor it takes up the full screen without my custom layoutPet

© 2022 - 2024 — McMap. All rights reserved.