user8189894
user8189894

Reputation:

How can I center my AlertDialog Title?

Here's my code, rest wirtten below.

public void Anzeigen_Reaper(View view) {
    AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
    helpBuilder.setTitle("Test Title");
    helpBuilder.setMessage("Test Message");
    helpBuilder.setPositiveButton("Ok, got it!",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                }
            });
    AlertDialog helpDialog = helpBuilder.create();
    helpDialog.show();
}

I don't know how I can center my title (helpBuilder.setTitle("Test Title");) without adding several "\t". Of course i could add 20 time "\t" to get it at the center but probably there is a special instruction/abbreviation for this.

I couldn't add help.Builder.setGravity("GRAVITY.CENTER); because setGravity was marked red.

Does anyone knows how to solve this? If its possible without styles, but if it's necessary, tell me how I do this, because I dont know how "styles.xml" works.

Thanks in advance!

Upvotes: 2

Views: 3914

Answers (4)

Bronskidev
Bronskidev

Reputation: 5

Surely it will be useful for someone. My solution looks like this.

<style name="AlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
        <item name="materialAlertDialogTitleTextStyle">@style/ALrtDialog.Title</item>

    <style name="ALrtDialog.Title" parent="MaterialAlertDialog.MaterialComponents.Title.Text.CenterStacked">
        <item name="android:layout_width">match_parent</item>
    </style>

Upvotes: 0

Mahendra Liya
Mahendra Liya

Reputation: 13218

You need to create a custom Dialog - you won't be able to do this for the default AlertDialog. In the layout of the custom dialog, you can create the layout as you like - align the text of the title in center and so on.

You can even use styles to style the layout of the Dialog. Some snippet for styling the dialog:

Inside your styles.xml add the following code:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowTitleStyle">@style/CustomDialogTitle</item>
    </style>

    <style name="CustomDialogTitle" parent="@android:style/TextAppearance.DialogWindowTitle">
        <item name="android:gravity">center_horizontal</item>
    </style>
    <style name="DialogWindowTitle">
    <item name="android:maxLines">1</item>
    <item name="android:scrollHorizontally">true</item>
    <item name="android:textAppearance">@android:style/TextAppearance.DialogWindowTitle</item>
    </style>
</resources>

Then refer the style when you create the dialog as:

Dialog dialog = new Dialog(this, R.style.CustomDialog);
dialog.setTitle("Your title");
dialog.setContentView(R.layout.custom_dialog);

Upvotes: 1

Dhanumjay
Dhanumjay

Reputation: 525

titlebar.xml

 <?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">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_margin="10dp"
    android:textStyle="bold"
    android:text="Title"/>

</RelativeLayout>

alertDialog code:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater inflater   = getLayoutInflater();
View view                 = inflater.inflate(R.layout.titlebar, null);

alert.setCustomTitle(view);
alert.setMessage("Your Message");
alert.setPositiveButton(
  "Ok, got it!",
  new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {

    }
  }
);
AlertDialog helpDialog = alert.create();
helpDialog.show();

Upvotes: 2

Shweta Chauhan
Shweta Chauhan

Reputation: 6981

// Creating the AlertDialog with a custom xml layout

AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layoutname, null);
helpBuilder.setView(view);

TextView txtTitle = new TextView(this);
// Customise your Title 
txtTitle.setText("Test Title");
txtTitle.setGravity(Gravity.CENTER);

helpBuilder.setCustomTitle(txtTitle );

Upvotes: 0

Related Questions