I want to change the font of the title of Alert Dialog box.
Can anybody tell how can I do it?
You can add this
TextView textViewt = (TextView) alertDialog.findViewById(android.R.id.title);
textViewt.setTypeface(your font typeface);
after the line
alertDialog.show();
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);
Sample layout saved as res/layout/link.xml:
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.
There is an easy way to set a new custom view to dialog's title. We can define every custom view such as TextView and add it some custom properties and at last set it to dialog's title, such as below:
AlertDialog.Builder builder = new AlertDialog.Builder(OrderItemsActivity.this);
TextView title_of_dialog = new TextView(getApplicationContext());
title_of_dialog.setHeight(50);
title_of_dialog.setBackgroundColor(Color.RED);
title_of_dialog.setText("Custom title");
title_of_dialog.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
title_of_dialog.setTextColor(Color.WHITE);
title_of_dialog.setGravity(Gravity.CENTER);
builder.setCustomTitle(title_of_dialog);
builder.create().show();
Here, I define a dynamic TextView and set some properties to it. Finally, I set it to dialog's title using "setCustomTitle()" function.
TextView tv_message = new TextView(this);
Typeface typeface = Typeface.createFromAsset(
getAssets(),
"fonts/OpenSans-Semibold.ttf"
);
// Set the text view layout parameters
tv_message.setLayoutParams(
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
);
// Set message text color
tv_message.setTextColor(Color.RED);
// Set message gravity/text align
tv_message.setGravity(Gravity.START);
// Set message text size
tv_message.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
// Set message custom font
tv_message.setTypeface(typeface);
// Set message background color
tv_message.setBackgroundColor(Color.YELLOW);
// Set message text padding
tv_message.setPadding(15, 25, 15, 15);
tv_message.setText("Are you sure?");
tv_message.setTextColor(Color.BLACK);
Do like this :
Dialog dialog = new Dialog(ActivityName.this);
dialog.setContentView(R.layout.dialog_name);
dialog.setCancelable(true);
dialog.findViewById(R.id.text).setTypeface(Typeface.createFromAsset(getAssets(), "font.ttf"));
Have your font.ttf file in the assets folder and use it like above
You can change appearence of AlertDialog
by following this block of code:
AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Message").show();
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setTextSize(10);//to change font size
//to change font family
Typeface face = Typeface.createFromAsset(getAssets(),"font/fontFileName.ttf");
textView.setTypeface(face);
Put font file in assets folder. In my case I created a subdirectory called font.
And you can also check for this Question which have an accepted answer.