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?

Krishna Kachhela
  • 773
  • 1
  • 7
  • 24
Ryan Newman
  • 846
  • 3
  • 17
  • 35
  • http://stackoverflow.com/questions/4852573/how-to-add-title-to-the-custom-dialog might give you a workaround – Gavriel Feb 09 '16 at 22:44

4 Answers4

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);
Raafat Alhmidi
  • 1,106
  • 12
  • 18
  • 4
    That's called a solution, suggesting to use `AlertDialogue` instead of dialogue is not the solution, it may be a work around... – kAmol May 01 '17 at 18:20
  • 6
    Why does Android development make me want to bang my head against a desk. – Daniel Wilson Oct 03 '18 at 12:07
  • @Raafat Alhoumaidy After applying this the dialog background become black with title color as white, Why so?? – KJEjava48 Jul 25 '19 at 08:41
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();
    }
});
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 2
    @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 dialog – OneCricketeer Feb 09 '16 at 23:17
  • When I try adding the onClickLIstener I'm getting a null object refernce. Do you know why? – Ryan Newman Feb 09 '16 at 23:20
  • 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 `setView` – OneCricketeer Feb 09 '16 at 23:24
  • 1
    @RyanNewman - Apparently, you have to call `show()` first, I made the edit... that isn't very intuitive... :/ – OneCricketeer Feb 09 '16 at 23:26
  • 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 into – Ryan Newman Feb 09 '16 at 23:33
  • 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. – OneCricketeer Feb 09 '16 at 23:36
  • 1
    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. – OneCricketeer Feb 09 '16 at 23:38
  • When I put it up on a 4.4.4 simulator it crashes which I'm not sure why – Ryan Newman Feb 09 '16 at 23:38
  • I was confused on what you meant by the support library. It works now. Thank you! – Ryan Newman Feb 09 '16 at 23:40
  • In gradle, you can add `compile 'com.android.support:appcompat-v7:23.1.1'`, but as long as it works, that's good – OneCricketeer Feb 09 '16 at 23:41
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.

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.

Valentin Kuhn
  • 753
  • 9
  • 25
  • I wasn't sure if reassigning the builder for the `set` methods was necessary, so I made my own answer, just in case :) – OneCricketeer Feb 09 '16 at 23:09
  • 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? – Ryan Newman Feb 09 '16 at 23:14
  • I'm not quite sure why you get that warning... AlertDialog has been added in API Level 1 (according to the [docs](http://developer.android.com/reference/android/app/AlertDialog.html#setView%28android.view.View%29)) and the used methods should also be there since API Level 1 – Valentin Kuhn Feb 09 '16 at 23:19
  • and I'm absolutely, positively sure I used that in many older projects... strange – Valentin Kuhn Feb 09 '16 at 23:21
  • Yeah I threw it up on a 4.4.4 emulator and I get `java.lang.NoSuchMethodError: android.app.AlertDialog$Builder.setView` – Ryan Newman Feb 09 '16 at 23:24
  • 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 help – Ryan Newman Feb 09 '16 at 23:27
  • @RyanNewman - Looked at the wrong doc the first time. Should be an `R.style` [theme resource for that second param](http://developer.android.com/reference/android/app/AlertDialog.Builder.html) – OneCricketeer Feb 09 '16 at 23:30
  • I actually take that back. When I added the layout into the constructor it takes up the full screen without my custom layout – Ryan Newman Feb 09 '16 at 23:32