To make use of different font style in your android project follow the steps mentioned below:-
1) Create an Assets resource directory in the main Directory of your project.
2)Now download the font style that you wish to use in your project having ".ttf" extension and paste the file in your Assets folder.
3)After putting fonts in the assets folder , you can access it in your java code using Typeface class.
Call static method of Typeface class createFromAsset() to get your custom font from assets.
The syntax is "Typeface customefont=Typeface.createFromAsset(MainActivity.this.getAssets(), "fonts.ttf");"
4) To set this custom font object to your TextView Typeface property. You need to call setTypeface() method to do that. Its syntax is "timeelapsed.setTypeface(customefont);"
activity_main.xml
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_marginLeft="70dp"
android:id="@+id/set_time_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time Elapsed"
android:textColor="#000"/>
<TextView
android:layout_marginTop="20dp"
android:layout_marginLeft="70dp"
android:id="@+id/remaining_time_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remaining Time"
android:textColor="#000"/>
<TextView
android:layout_weight="1"
android:id="@+id/user_name"
android:text="USER_NAME"
android:textSize="25sp"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView elapsedTime,timeelapsed,remainingTime,username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timeelapsed=(TextView)findViewById(R.id.set_time_tv);
remainingTime=(TextView)findViewById(R.id.remaining_time_tv);
username=(TextView)findViewById(R.id.user_name);
Typeface customefont=Typeface.createFromAsset(MainActivity.this.getAssets(), "fonts.ttf");
timeelapsed.setTypeface(customefont);
remainingTime.setTypeface(customefont);
username.setTypeface(customefont);
}
}
0 Comment(s)