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"/> 
6
  • You can use a custom textview and inflate on dialog. Commented Feb 21, 2015 at 7:25
  • Can you please show me an example code for this. Commented Feb 21, 2015 at 7:28
  • 1
    You want to change the title color, size or dialog body? Commented Feb 21, 2015 at 7:29
  • You just need to create custom dialog, to have control over its title, content and all things around your dialog. Follow this link , or follow this link , or just make research in google with text "android dialog custom title" or with "android create custom dialog" Commented Feb 21, 2015 at 7:30
  • I want to change Title color, text size of my dialog. Commented Feb 21, 2015 at 7:36

7 Answers 7

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); 
Sign up to request clarification or add additional context in comments.

3 Comments

in my case it doesn't work I have style <>: <style name="DialogTitle" parent="TextAppearance.AppCompat.Large"> <item name="android:textSize">50sp</item> <item name="android:textColor">#f0f</item> </style> then: - textSize is ignored - but color is changed
The default AlertDialog title font is TextAppearance.AppCompat.Title.
@GrzegorzDev Have you managed to increase the title size?
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); 

1 Comment

This was nice and easy. perfect when you don't need extra overhead of creating a layout.
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(); 

Comments

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); 

Comments

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.

Comments

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!

Comments

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); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.