Java – Why doesn’t Android dialog title appear on Marshmallow in Android version?

Why doesn’t Android dialog title appear on Marshmallow in Android version?… here is a solution to the problem.

Why doesn’t Android dialog title appear on Marshmallow in Android version?

I’m trying to create a custom dialog, here’s my code :

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

This 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’ve run it on the android version of lollipop and it works fine Here is the screenshot on lollipop:

Dialog on Android version Lollipop

As you can see, the dialog title is showing up, but when I try to run on the Android version, the marshmallow header stops showing up, here is the screenshot on marshmallow :

Dialog on android version marshmallow

As you can see, the dialog title is not displayed on the Android version of Marshmallow. I tried adding this line to show the header:

myDialog.create()

But the title is still not displayed. What am I going to do?

Solution

Have you tried applying a custom style… The dialog box is styled by a parent activity that may have been set to NoTitle.

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

About Java code

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

Related Problems and Solutions