activity_main.xmltoolbar.xmlsimonetta.xmlstyles.xmlMainActivity.java
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" tools:context=".MainActivity"> <include android:id="@+id/toolbar" layout="@layout/toolbar" /> <TextView android:id="@+id/text_view_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:fontFamily="@font/simonetta" android:text="Hello World!" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="@font/simonetta" android:text="Hello World!" android:textSize="30sp" android:textStyle="italic" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="@font/simonetta" android:text="Hello World!" android:textSize="30sp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="@font/simonetta" android:text="Hello World!" android:textSize="30sp" android:textStyle="bold|italic" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:elevation="4dp" android:theme="@style/ToolbarThemeOverlay" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> </android.support.v7.widget.Toolbar>
<?xml version="1.0" encoding="utf-8"?> <font-family xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <font app:font="@font/simonetta_regular" app:fontStyle="normal" app:fontWeight="400" /> <font app:font="@font/simonetta_italic" app:fontStyle="italic" app:fontWeight="400" /> <font app:font="@font/simonetta_black" app:fontStyle="normal" app:fontWeight="700" /> <font app:font="@font/simonetta_black_italic" app:fontStyle="italic" app:fontWeight="700" /> </font-family>
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:fontFamily">@font/simonetta</item> </style> <style name="ToolbarThemeOverlay" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <item name="android:fontFamily">@font/architects_daughter</item> </style> </resources>
package com.codinginflow.fontsexample; import android.graphics.Typeface; import android.support.v4.content.res.ResourcesCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); final TextView textView = findViewById(R.id.text_view_1); final Typeface typeface = ResourcesCompat.getFont(this, R.font.architects_daughter); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setTypeface(typeface); } }); } }