Jazz
Jazz

Reputation: 314

why Android dialog title not showing on android versions marshmallow?

I was trying to create a custom dialog here is my code :

        val myDialog = Dialog(this)
        myDialog.setContentView(R.layout.my_dialog)
        myDialog.setTitle("My Dialog!")
        myDialog.setCancelable(true)
        myDialog.show()

Here is my_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
        android:text="Hello world!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:textSize="30sp"/>

</android.support.constraint.ConstraintLayout>

I have run this on android version lollipop It works fine here is screenshot on lollipop :

Dialog on Android version Lollipop

as you can see dialog title is showing but when I try to run this on Android version marshmallow title stop showing here is screenshot on marshmallow :

Dialog on android version marshmallow

as you can see dialog title not showing on android version marshmallow. I have try adding this line for showing title :

myDialog.create()

but title still not showing. what should I do?

Upvotes: 3

Views: 1241

Answers (2)

Mark Morgan Miranda
Mark Morgan Miranda

Reputation: 141

Have you tried applying custom style... The dialog adopts the Style of the parent Activity which might have been set to NoTitle.

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

On the java code

new AlertDialog.Builder(new ContextThemeWrapper(Context, R.style.Dialog))

Upvotes: 2

Bhoomika Patel
Bhoomika Patel

Reputation: 1925

Use AlertDialog instead of Dialog.

  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle(getString(R.string.app_name));
        alertDialogBuilder.setMessage(getString(R.string.disclaimer))
                .setCancelable(false)
                .setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.dismiss();
                            }
                        });
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();

Upvotes: 1

Related Questions