Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the fonts of Title in Action bar in android

I want to change the Fonts of my title in the action bar. I have set the title programatically, in xml it was not showing any effect. Now I want to change the font of that title. I saw some post related to this, but It was not having the problem which I am facing.

Here is my code.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //SETTING ITS LAYOUT
    setContentView(R.layout.activity_start);
Typeface typeface = getResources().getFont(R.font.dancingscriptregular);


    //SETTING TITLE OF THE APP IN BLUE COULOR
    getSupportActionBar().setTitle(Html.fromHtml("<font color='#1F9FD9'>Expense Manager <font>"));

  ...

 }

Now what next?, how to use the typeface to set the font of my title.

Please Help!

In androidMenifest.xml

<activity
        android:parentActivityName=".StartActivity"
        android:name=".ExpenseActivity"
        android:fitsSystemWindows="true"
        android:label="@string/expenseActivity" <!--This Line is not working-->
        android:screenOrientation="portrait"
        android:textColor="@color/batteryChargedBlue"
        android:theme="@style/ExpenseTheme" />
    <activity

ExpenseTheme

<style name="ExpenseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/white</item>
    <item name="colorPrimaryDark">@color/batteryChargedBlue</item>
    <item name="colorAccent">@color/greenBlue</item>
    <item name="android:navigationBarColor">@color/batteryChargedBlue</item>

</style>

activity_start.xml Code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StartActivity">

<LinearLayout
    android:id="@+id/part_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_above="@+id/part_2"
    android:gravity="center"
    >

    <TextView
        android:id="@+id/welcome"
        android:gravity="center"
        android:layout_marginBottom="20dp"
        android:textColor="@color/greenBlue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="@font/caviar_dreams_bold"
        android:text="Welcome !!!"
        android:textSize="55dp"


        />

    <TextView
        android:id="@+id/info"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:textColor="@color/greenBlue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="@font/dancingscriptregular"
        android:text="@string/info"
        android:textSize="35dp"
        />

    <EditText
        android:id="@+id/imaEt"
        android:layout_marginTop="60dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/enterIMA"
        android:background="@drawable/border_and_lines_tp"
        android:padding="15dp"
        android:textColorHint="@color/greenBlue"
        android:fontFamily="@font/caviar_dreams_bold"
        android:layout_gravity="center"
        android:inputType="numberDecimal"
        />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/imaBtn"
        android:layout_width="110dp"
        android:layout_height="55dp"
        android:background="@color/batteryChargedBlue"
        android:theme="@style/ButtonTheme"
        android:text="Enter"
        android:textSize="15sp"
        android:gravity="center"
        android:layout_marginTop="15dp"
        android:onClick="setInitialMonthlyAmount"
        app:cornerRadius="5dp"
        android:textColor="@color/white"/>

    <com.google.android.material.button.MaterialButton
        android:id="@+id/imaSkip"
        android:layout_width="75dp"
        android:layout_height="50dp"
        android:background="@color/batteryChargedBlue"
        android:theme="@style/AppTheme"
        android:text="Skip"
        android:textSize="15sp"
        android:gravity="center"
        android:layout_marginTop="15dp"
        android:onClick="setOnSkipClicked"
        app:cornerRadius="5dp"
        android:textColor="@color/white"/>

    <!-- <Button
         android:id="@+id/imaBtn"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:layout_marginTop="15dp"
         android:background="@drawable/borders_and_lines"
         android:onClick="setInitialMonthlyAmount"
         android:text="Enter"
         android:textColor="#FFF" /> -->

    <!-- <Button
         android:id="@+id/imaSkip"
         android:layout_gravity="center"
         android:layout_marginTop="15dp"
         android:text="Continue >>"
         android:textColor="#FFF"
         android:onClick="setOnSkipClicked"
         android:background="@drawable/borders_and_lines"
         android:layout_width="100dp"
         android:layout_height="30dp"
         /> -->



</LinearLayout>

<LinearLayout
    android:id="@+id/part_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">

    <TextView
        android:id="@+id/help"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/help"
        android:textColor="@color/greenBlue"
        android:fontFamily="@font/dancingscriptregular"
        android:textStyle="bold"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="10dp"
        android:gravity="right"
        android:textSize="20dp"
        android:clickable="true"/>

</LinearLayout>
</RelativeLayout>

This my actual layout and I have achieved by using the following file, which I have posted. now I just want to change the font of the black circled text.

like image 257
Noman khanbhai Avatar asked Sep 22 '19 06:09

Noman khanbhai


People also ask

How can I change the font of title bar in android?

First, add a font file in the src/main/assets/fonts/ of your project. Then create variables for Toolbar and text title and call the method findViewById(). Create a new Typeface from the specified font data. And at last setTypeface in text title.


1 Answers

You have different options.
In your activity you can use something like in your layout:

   <com.google.android.material.appbar.AppBarLayout>

        <com.google.android.material.appbar.MaterialToolbar
            style="@style/MyToolbar"
            ...
            />

    </com.google.android.material.appbar.AppBarLayout>

Then you can define a custom style:

  <!-- Toolbar -->
  <style name="MyToolbar" parent="Widget.MaterialComponents.Toolbar">
    <item name="titleTextAppearance">@style/My_TextAppearance_Toolbar</item>    
  </style>

  <style name="My_TextAppearance_Toolbar" parent="@style/TextAppearance.MaterialComponents.Headline6">
    <item name="fontFamily">....</item>
    <item name="android:fontFamily">....</item>
    <item name="android:textStyle">bold|italic</item>
  </style>

enter image description here

Otherwise you can also use the Toolbar API:

   toolbar.setTitle("Material Title");
   toolbar.setTitleTextAppearance(this,R.style.My_TextAppearance_Toolbar);

Another option is to use app:titleTextAppearance attribute in your layout:

  <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:titleTextAppearance="@style/My_TextAppearance_Toolbar"
            .../>
like image 139
Gabriele Mariotti Avatar answered Sep 26 '22 23:09

Gabriele Mariotti