I want to change the font of the title of Alert Dialog box.
Can anybody tell how can I do it?
I want to change the font of the title of Alert Dialog box.
Can anybody tell how can I do it?
I found a simple solution..
At first I was setting the title of my alert box by
builder.setTitle("My Title");
So I was not able to change the font of it..
Then what worked for me is..
I created a simple TextView :
TextView tv2;
And set all properties of TextView which I wanted...
And then I replaced my
builder.setTitle("My Title");
line with
builder.setCustomTitle(tv2);
and now I can change Title Color
, Font
Etc By Changing tv2's
Properties..
No answers provide a way to change the title typeface of an AlertDialog
. Here is what I have done:
Create the below class:
public static class TypefaceSpan extends MetricAffectingSpan {
private final Typeface typeface;
public TypefaceSpan(Typeface typeface) {
this.typeface = typeface;
}
@Override public void updateDrawState(TextPaint tp) {
tp.setTypeface(typeface);
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override public void updateMeasureState(TextPaint p) {
p.setTypeface(typeface);
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}
Add the following utility method:
/**
* <p>Return spannable string with applied typeface in certain style</p>
*
* http://stackoverflow.com/questions/8607707/how-to-set-a-custom-font-in-the-actionbar-title
*
* @param typeface
* The typeface to set to the {@link SpannableString}
* @param string
* the string to place in the span
* @return SpannableString that can be used in TextView.setText() method
*/
public static SpannableString typeface(Typeface typeface, CharSequence string) {
SpannableString s = new SpannableString(string);
s.setSpan(new TypefaceSpan(typeface), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return s;
}
Finally, set the typeface when creating your AlertDialog
:
Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/your_font.ttf");
new AlertDialog.Builder(getActivity())
.setTitle(FontUtils.typeface(typeface, "The title"))
/* .. */
.create();
Just use the following line to get an identifier to the dialog's title:
int dialogTitle = mCtnx.getResources().getIdentifier( "alertTitle", "id", "android" );
use this one for android.support.v7.app.AlertDialog
TextView title= (TextView)alertDialog.findViewById(R.id.alertTitle);
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.
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
Instead of setting the text of the alertdialog, you should set a custom view form your layouts. And before you do so, modify your view's font. try this example
TextView content = new TextView(this);
content.setText("on another font");
content.setTypeface(Typeface.SANS_SERIF);
//Use the first example, if your using a xml generated view
AlertDialog.Builder myalert = new AlertDialog.Builder(this);
myalert.setTitle("Your title");
myalert.setView(content);
myalert.setNeutralButton("Close dialog", null);
myalert.setCancelable(true);
myalert.show();
code for xml...replace your font in the xml
<TextView
android:id="@+id/yourid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="your content" />
Include this layout:
<?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="Hello_World"
android:textColorLink="#FF00FF"
/>
</LinearLayout>
Then use it inside Ur Activity
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"));
You can try Spannable as well.
SpannableString s = new SpannableString("My App");
s.setSpan(new TypefaceSpan(this, "font.otf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new RelativeSizeSpan(1.3f), 0,s.length(), 0);
builder.setTitle(s)
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.
You can add this
TextView textViewt = (TextView) alertDialog.findViewById(android.R.id.title);
textViewt.setTypeface(your font typeface);
after the line
alertDialog.show();
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.
Use this method from support library 26.
ResourcesCompat.getFont(context, R.font.<YOUR_FONT>);
Try setting theme for the dialog. Change the typeface attribute like this :
<style name="CustomDialog" parent="android:Theme.Dialog">
<item name="android:typeface">serif</item>
</style>
Though this changes Fonts of all the textviews within the dialog. so make sure TextViews are set to proper typeface
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);