How to Set a Custom Font in the ActionBar Title?
I agree that this isn't completely supported, but here's what I did. You can use a custom view for your action bar (it will display between your icon and your action items). I'm using a custom view and I have the native title disabled. All of my activities inherit from a single activity, which has this code in onCreate:
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = LayoutInflater.from(this);
View v = inflator.inflate(R.layout.titleview, null);
//if you need to customize anything else about the text, do it here.
//I'm using a custom TextView with a custom font in my layout xml so all I need to do is set title
((TextView)v.findViewById(R.id.title)).setText(this.getTitle());
//assign the view to the actionbar
this.getActionBar().setCustomView(v);
And my layout xml (R.layout.titleview in the code above) looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent" >
<com.your.package.CustomTextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:maxLines="1"
android:ellipsize="end"
android:text="" />
</RelativeLayout>
How to change the fonts of Title in Action bar in android
Just use these code:
if you are using "Holo Theme" then use this:
int titleId = getResources().getIdentifier("action_bar_title", "id",
"android");
TextView yourTextView = (TextView) findViewById(titleId);
yourTextView.setTextColor(getResources().getColor(R.color.black));
Typeface yourTypeface = Typeface.createFromAsset(getAssets(), "fonts/your_font.ttf");
//or get from resource
yourTextView.setTypeface(yourTypeface);
if you are using "Material Theme" then use this:
Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
TextView textView1 = (TextView) toolbar.getChildAt(0);//title
TextView textView2 = (TextView) toolbar.getChildAt(1);//subtitle
textView1.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
textView2.setTextColor(getResources().getColor(R.color.colorAccent));
Typeface yourTypeface1 = Typeface.createFromAsset(getAssets(), "fonts/your_font1.ttf");
Typeface yourTypeface2 = Typeface.createFromAsset(getAssets(), "fonts/your_font2.ttf");
//or get from resource
textView1.setTypeface(yourTypeface1);
textView2.setTypeface(yourTypeface2);
it works and it is very simple!
screen shot
How to change the font of ActionBar title in AppCompat 23
I resolved my problem creating a custom action bar style and custom title text style:
In the app theme:
<style name="Theme.AppLib" parent="Theme.AppCompat.Light">
<item name="android:typeface">serif</item>
<item name="actionBarStyle">@style/Theme.AppLib.ActionBar.Solid</item>
</style>
And the styles:
<style name="Theme.AppLib.ActionBar.Solid" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
<item name="background">@drawable/cab_background_top</item>
<item name="titleTextStyle">@style/Theme.AppLib.ActionBar.TitleTextStyle</item>
</style>
<style name="Theme.AppLib.ActionBar.TitleTextStyle" parent="">
<item name="android:textColor">@color/text_dark</item>
<item name="android:textSize">18sp</item>
</style>
Android: How to give custom font style to action bar title
Try this one:
SpannableString s = new SpannableString("My Title");
s.setSpan(new TypefaceSpan(this, "MyTypeface.otf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Update the action bar title with the TypefaceSpan instance
ActionBar actionBar = getActionBar();
actionBar.setTitle(s);
Hope it helps
Reference
UPD: The source code of custom TypefaceSpan is here
How to Set a Custom Font Family in the Xamarin ActionBar Title?
You can create a custom renderer in android for this purpose. The renderer simply changes the typeface of your navigation bar using the resource defined in your android project. You can use the following renderer in android:
[assembly: ExportRenderer(typeof(NavigationPage), typeof(AndroidNavigationRenderer))]
namespace Xamarin.Droid.DependencyServices
{
public class AndroidNavigationRenderer : NavigationPageRenderer
{
private Android.Support.V7.Widget.Toolbar toolbar;
public override void OnViewAdded(Android.Views.View child)
{
base.OnViewAdded(child);
if (child.GetType() == typeof(Android.Support.V7.Widget.Toolbar))
{
toolbar = (Android.Support.V7.Widget.Toolbar)child;
toolbar.ChildViewAdded += Toolbar_ChildViewAdded;
}
}
private void Toolbar_ChildViewAdded(object sender, ChildViewAddedEventArgs e)
{
var view = e.Child.GetType();
if (e.Child.GetType() == typeof(Android.Support.V7.Widget.AppCompatTextView))
{
var textView = (Android.Support.V7.Widget.AppCompatTextView)e.Child;
textView.Typeface = Typeface.CreateFromAsset(this.Context.Assets, "fonts/QuicksandRegular.ttf");
toolbar.ChildViewAdded -= Toolbar_ChildViewAdded;
}
}
}
}
For UWP, you can define a custom style in your app.xaml to override the default style of your title bar. You can use the following code in UWP:
<Style x:Key="TitleTextBlockStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="18"/>
<Setter Property="FontStyle" Value="Normal"/>
<Setter Property="FontWeight" Value="SemiBold"/>
<Setter Property="FontFamily" Value="Assets/Fonts/QuicksandRegular.ttf#Quicksand"/>
<Setter Property="Padding" Value="10"/>
</Style>
Simply change the font family to the font family of your choice.
how to set default font family available in android to action bar title
With a Toolbar
you can use:
<com.google.android.material.appbar.MaterialToolbar
app:titleTextAppearance="@style/myTitleTextAppearance"
with:
<style name="myTitleTextAppearance" parent="TextAppearance.MaterialComponents.Subtitle1" parent="TextAppearance.AppCompat.Subhead">
<item name="fontFamily">sans-serif-medium</item>
<item name="android:fontFamily">sans-serif-medium</item>
.....
</style>
with an ActionBar
you add in your app theme the actionBarStyle
attribute:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="actionBarStyle">@style/myActionBar</item>
</style>
with:
<style name="myActionBar" parent="Widget.MaterialComponents.ActionBar.PrimarySurface">
<item name="titleTextStyle">@style/myActionBarTextTitle</item>
</style>
<style name="myActionBarTextTitle" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="fontFamily">sans-serif-medium</item>
<item name="android:fontFamily">sans-serif-medium</item>
</style>
Related Topics
Get/Pick an Image from Android'S Built-In Gallery App Programmatically
Key Hash For Android-Facebook App
Best Practice For Storing and Protecting Private API Keys in Applications
What Is the Main Purpose of Settag() Gettag() Methods of View
Difference Between "@Id/" and "@+Id/" in Android
Android: Check Whether the Phone Is Dual Sim
Onactivityresult Method Is Deprecated, What Is the Alternative
How to Use Getdownloadurl in Recent Versions
How to Create a Custom Dialog Box in Android
How to Format Date and Time in Android
How to Filter Specific Apps For Action_Send Intent (And Set a Different Text For Each App)
Difference Between Getcontext() , Getapplicationcontext() , Getbasecontext() and "This"
Jdbc VS Web Service For Android
How to Send an Object from One Android Activity to Another Using Intents
Failed to Resolve: Com.Android.Support:Cardview-V7:26.0.0 Android