Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dialog.setTitle not showing a title

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?

like image 646
Ryan Newman Avatar asked Feb 09 '16 22:02

Ryan Newman


People also ask

Which method is used to set a title to a alert dialog?

Alert Dialog code has three methods: setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message. setIcon() method is use to set the icon on Alert dialog box.

How do I show custom dialog?

This example demonstrate about how to make custom dialog in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is dialog fragment?

Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.


2 Answers

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);
like image 93
Raafat Alhmidi Avatar answered Oct 04 '22 09:10

Raafat Alhmidi


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();
    }
});
like image 24
OneCricketeer Avatar answered Oct 04 '22 08:10

OneCricketeer