5

I have two questions

1) Does anyone know, how to apply styles or formatting to alert dialog. I currently use Builder builder = new AlertDialog.Builder(this); And use setMessage() method to set the content.

2) Also I would like to know how to change the color of the links created by linkify. I don't want the default blue color.

Vivek
  • 4,526
  • 17
  • 56
  • 69

2 Answers2

12

Q1. You have to inflate or customize and create a style and apply to AlertDialog

Heres how you inflate a layout and apply it to AlertDialog

LayoutInflater li = LayoutInflater.from(ctx);
View view = li.inflate(R.layout.formatted_dialog, null);

AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("Formatted");
builder.setView(view);

define all the formatting and styles required in the layout you specified.

You can access specific textview defined in the layout using inflated View i.e.

LayoutInflater li = LayoutInflater.from(ctx);
View view = li.inflate(R.layout.formatted_dialog, null);
TextView label=(TextView)view.findViewById(R.id.i_am_from_formatted_layout_lable);

Q2. android:textColorLink="#FF00FF" can be used to specify color of link.

EDIT:

Sample layout saved as res/layout/link.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <TextView
   android:id="@+id/text"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="http://www.google.com"
   android:autoLink="web"
   android:textColorLink="#FF00FF"
  />

</LinearLayout>

In your onCreate() or where or whenever you want to call AlertDialog

LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.link, null);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Formatted");
builder.setView(view).create().show();
TextView text=(TextView) findViewById(R.id.text);

replace this with context object if you are calling from some other method.

Shardul
  • 27,760
  • 6
  • 37
  • 35
3

you can use following code to change typeface and Text color by extracting TextView from default alertDialog:

TextView txtAlertMsg = (TextView)alert.findViewById(android.R.id.message);
txtAlertMsg.setGravity(Gravity.CENTER);
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
Anand Tiwari
  • 1,583
  • 10
  • 22
  • 1
    Hi Anand, I tried as said by you but I can't figure out that where I am doing wrong. I got NullPointerException at Gravity set....My Code is Look like as below: AlertDialog.Builder builder=new AlertDialog.Builder(context); builder.setIcon(0); builder.setTitle("My Title"); builder.setMessage(msg); builder.setNeutralButton("Ok", null); AlertDialog alertDialog=builder.create(); alertDialog.show(); ((TextView)alertDialog.findViewById(android.R.id.title)).setGravity(Gravity.CENTER); ((TextView)alertDialog.findViewById(android.R.id.message)).setGravity(Gravity.CENTER); – Kalpesh Jul 31 '12 at 13:37