Issue
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.
@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>
Solution
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!
Answered By - majid ghafouri
Answer Checked By - Timothy Miller (JavaFixing Admin)