8

I am working on an Android application in which I want to set the text size of my dialog box. I have used the following code an XML for textview but it is only setting the size of my fields. I want to set the text size and color of my title as well.

final CharSequence[] items = { "Fake Request", "Abusive Language","Indecent Approach","Unacceptable Attitude","Dangerous Behavior",};

           ArrayAdapter<CharSequence> itemsAdapter = new
           ArrayAdapter<CharSequence> (this,
                    R.layout.menu_items, items);
           AlertDialog.Builder   builder = new AlertDialog.Builder(this);
           builder.setTitle("My Title");
           builder.setIcon(R.drawable.icon);
           builder.setAdapter(itemsAdapter, new DialogInterface.OnClickListener()
           {
               public void onClick(DialogInterface dialog, int item) {
                          switch(item) {
                                  case 0:
                              //Mark as Beautiful
                                  break;
                               case 1:
                              //Mark as Beautiful
                                  break;
                            case 2: 
                                //Mark as Not a Portrait
                                  break;
                            case 3:
                              //Mark as Offensive
                                  break;
                           case 4:
                              //Mark as Spam
                                  break;
                           case 5:
                              //cancel
                           break;
                           }
               }
           }

               );
           builder.show();

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@android:id/text1"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:padding="10dip"
       android:layout_margin="5dip"
       android:gravity="center_vertical"
       android:textSize="5dip"
       android:textColor="@color/red_theme"
       android:typeface="normal"
       android:lineSpacingExtra="0dip"/>
halfer
  • 19,824
  • 17
  • 99
  • 186
Usman Khan
  • 3,739
  • 6
  • 41
  • 89
  • You can use a custom textview and inflate on dialog. – Surender Kumar Feb 21 '15 at 07:25
  • Can you please show me an example code for this. – Usman Khan Feb 21 '15 at 07:28
  • 1
    You want to change the title color, size or dialog body? – Surender Kumar Feb 21 '15 at 07:29
  • I want to change Title color, text size of my dialog. – Usman Khan Feb 21 '15 at 07:36
  • Possible duplicate of [Change title font of Alert Dialog box](https://stackoverflow.com/questions/16913956/change-title-font-of-alert-dialog-box) – Vega Sep 06 '19 at 17:13
  • You just need to create custom dialog, to have control over its title, content and all things around your dialog. Follow this [link](https://stackoverflow.com/questions/13341560/how-to-create-a-custom-dialog-box-in-android) , or follow this [link](https://stackoverflow.com/questions/4852573/how-to-add-title-to-the-custom-dialog) , or just make research in google with text "android dialog custom title" or with "android create custom dialog" – Stoycho Andreev Feb 21 '15 at 07:30

7 Answers7

8

If you want to do it all using styles:

<style name="MyDialog" parent="Base.Theme.AppCompat.Light.Dialog">
    <item name="android:windowTitleStyle">@style/DialogTitle</item>
</style>

<style name="DialogTitle" parent="TextAppearance.AppCompat.Large">
    <item name="android:textSize">18sp</item>
</style>

And create your dialog as such

new AlertDialog.Builder(this, R.style.MyDialog);
zed
  • 3,180
  • 3
  • 27
  • 38
  • 3
    in my case it doesn't work I have style <>: then: - textSize is ignored - but color is changed – Grzegorz Dev Jun 07 '19 at 08:36
  • The default `AlertDialog` title font is `TextAppearance.AppCompat.Title`. – Sam Chen Feb 12 '20 at 03:01
7
//Use this code
TextView myMsg = new TextView(this);
  myMsg.setText("Succesfully send!");
  myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
  myMsg.setTextSize(20); 
  myMsg.setTextColor(Color.WHITE);
  //set custom title
        builder.setCustomTitle(myMsg);
Surender Kumar
  • 1,123
  • 8
  • 15
7

Use custom title using builder.setCustomTitle(textView) to set size and color of title.

AlertDialog.Builder builder = new AlertDialog.Builder(context);
    TextView title = new TextView(context);
    title.setText("Title");
    title.setBackgroundResource(R.drawable.gradient);
    title.setPadding(10, 10, 10, 10);
    title.setGravity(Gravity.CENTER);
    title.setTextColor(getResources().getColor(R.color.green));
    title.setTextSize(22);
    builder.setCustomTitle(title);
    builder.setMessage("Message");
    builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {

    }
    });
    builder.setNegativeButton(R.string.no, null);
    AlertDialog dialog = builder.show();
    messageView = (TextView)dialog.findViewById(android.R.id.message);
    messageView.setGravity(Gravity.CENTER);
    dialog.show();
Hemanth
  • 2,717
  • 2
  • 21
  • 29
2

Create a Custom Theme for Dialog in Style.xml and apply this to your Dialog.

Sample Theme:

 <style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
        <item name="android:bottomBright">@color/white</item>
        <item name="android:bottomDark">@color/white</item>
        <item name="android:bottomMedium">@color/white</item>
        <item name="android:textSize">10</item>
        <item name="android:centerBright">@color/white</item>
        <item name="android:centerDark">@color/white</item>
        <item name="android:centerMedium">@color/white</item>
        <item name="android:fullBright">@color/orange</item>
        <item name="android:fullDark">@color/orange</item>
        <item name="android:topBright">@color/blue</item>
        <item name="android:topDark">@color/blue</item>
    </style>

And in you Code:

AlertDialog.Builder   builder = new AlertDialog.Builder(this, R.style.CustomDialogTheme);
Waqar Khan
  • 468
  • 4
  • 18
0

To achieve this:

  1. Create an xml with list view
  2. Inflate that view with inflater
  3. Set custom view in AndroidBuilder
  4. Use custom Adapter to inflate row in listview.

Doing above point you will achieve your customization.

You can use below example for same:

http://www.mkyong.com/android/android-custom-dialog-example/ http://www.edumobile.org/android/android-development/custom-listview-in-a-dialog-in-android/

I hope it will help for you.

Yogendra
  • 4,817
  • 1
  • 28
  • 21
0

This is the simplest way

Customize a textView first,

TextView dialogTitle = new TextView(getApplicationContext());
dialogTitle.setText("Your Title Text");
dialogTitle.setTextColor(getResources().getColor(R.color.your_color));
dialogTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);

Then write

builder.setCustomTitle(dialogTitle);

instead of

builder.setTitle("My title");

Done!

Apurva
  • 7,871
  • 7
  • 40
  • 59
0

it´s a little bit late... but if someone have the same problem:

With androidx.appcompat.app.AlertDialog

AlertDialog alertdialog;
...
alertdialog.show();
final View v = alertdialog.findViewById(R.id.title_template);
    if (v != null) {
        v.setBackgroundColor(getResources().getColor(colorPrimaryDark));
        TextView title = v.findViewById(R.id.alertTitle);
        title.setTextSize(30);
    }