Search code examples
androidandroid-layoutandroid-theme

Change Title header of AlertDialog


I am trying to change the title header of Alertdialog box but the output is not what I exactly wanted. I am creating the following style in styles.xml:

<style name="question_dialog" 
  parent="@android:style/Theme.Holo.Dialog">
    <item name="android:windowTitleStyle">@style/question_dialog_title</item>
</style>

<style name="question_dialog_title" parent="android:Widget.TextView">
<item name="android:background">#5cc5cc</item>
<item name="android:textSize">21sp</item>
<item name="android:textColor">#ffffff</item>
</style>

The java code is as follows :

new AlertDialog.Builder(this, 
R.style.question_dialog).setTitle("Assam Quiz" ). 
setMessage("Hello world Hello world"). 
setPositiveButton("OK", (dialog, which) - > 
{dialog.dismisd();
}).show();
}

The AlertDialog image is attached. enter image description here


Solution

  • out of the style - I think your dialog has some header layout set to it that prevents the title to be at the top, but if that is the case or not - you can easily set a custom title for the dialog header giving it the header layout only, and so you will have the full control for the dialog header:

    // creating the Dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    // getting dialog context
    Context mContext = builder.getContext();
    // building the inflater
    LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
    // inflate the dialog header layout
    View mView = mLayoutInflater.inflate(R.layout.simple_dialog_header, null);
    // get the TextView for the header (contained in the header layout)
    TextView mTextView = (TextView) mView.findViewById(R.id.title_text);
    // set the text for that TextView
    mTextView.setText(message);
    // set the custom header view for the dialog
    builder.setCustomTitle(mView);
    /*
    here you can set positive , negative ,neutral buttons
    or set the dialog message or any attribute you want
    */
    // finally, show the dialog
    builder.show();
    

    and for the header layout (R.layout.simple_dialog_header):

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        android:orientation="vertical"
        android:paddingBottom="15dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="15dp">
    
        <TextView
            android:id="@+id/title_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:maxLines="1"
            android:textAlignment="center"
            android:textColor="@color/primary"
            android:textSize="16sp"
            android:textStyle="bold" />
    
    </LinearLayout>