2

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!

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

4 Answers4

2

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();
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
Dhanumjay
  • 540
  • 7
  • 17
  • Thanks! Do I add this to my exsiting code or replace it, and if i need to replace it, where/what should i delete? –  Jun 24 '17 at 10:32
  • first create layout with TextView with your title and make textview as center in layout and just replace your layout name here R.layout.your_layout_name – Dhanumjay Jun 24 '17 at 10:39
  • I already have a layout with some ImageButtons and 1 TextView which is used and 2 (normal) Buttons, so, do I really need to create a new layout? I thought "AlertDialog" is a PopUp which doesn't need a layout with any specifications –  Jun 24 '17 at 10:48
  • are you using custom layout? then add title in that image buttons layout only and remove setTitle() – Dhanumjay Jun 24 '17 at 10:51
  • I guss this explains my Problem or with this u see what i need to rename/delete ^^" [There you go](http://imgur.com/a/QPuMq) –  Jun 24 '17 at 10:59
  • are you showing alertDialog on pressing ImageButton? or is that your alert dialog Layout? – Dhanumjay Jun 24 '17 at 11:03
  • if i press my imageButton with 'onClick="Anzeiegn_Reaper" I show an altertDialog (i guess, i copied this copde as i searched it yesterday). I didnt now i can have an extra layout for alertDialogs, is this probably better? so should i "switch" to creating layout for alertDialogs instead of my "old" version in java-code? –  Jun 24 '17 at 11:12
  • if you just want to centre title of alertDialog then follow my answer or if you wish to change alert dialog view according to your requirement then design entire alertDialog layout and use setView() to set your layout. – Dhanumjay Jun 24 '17 at 11:18
  • and can i place additional textviews for the messages in my titlebar.xml layout? because i want 4 sentences as a message, but i shouldnt be shown as a continuous text. after each sentence there should be 1 "free" line where nothing is written. I solved it with a hardcoded text: [Here you go!](http://imgur.com/a/7zUaf) but i want it shorter, with several strings as u can see in the picure as well. . –  Jun 24 '17 at 11:33
  • yes check this link https://stackoverflow.com/questions/22655599/alertdialog-builder-with-custom-layout-and-edittext-cannot-access-view – Dhanumjay Jun 24 '17 at 11:45
  • I would like to ask you when implementing this code how you do dismiss() or cancel(), since the variable that can execute it is helpDialog and the variable is declared after the alert.setPositiveButton() method, so inside that method the call: helpDialog.dismiss() not possible – wiki Aug 18 '22 at 18:31
0
// 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 );
Shweta Chauhan
  • 6,739
  • 6
  • 37
  • 57
  • In "View view = inflater.inflate(R.layout.mylayout, null);" "null" is marked and Android Studio says "Avoid passing null as view root" –  Jun 24 '17 at 10:46
0

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);
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114
0

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>