Android How to change android button font programmatically
In this article we are going to learn about how to control button font using xml.
Before starting the actual coding I am assuming you are familiar with Android basics and know how to modify your activity. If not feel free to visit respective URL.
So let's start with actual coding.
Step 1: Add a new button.
<Button android:id ="@+id/push_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button - Monospace Font" android:padding="15dp" />
Above code is going add a new button in our main layout.
Step 2: Modify MainActivity.java
Button btn2 = (Button) findViewById(R.id.push_button); //require to import Typeface class btn2.setTypeface(Typeface.MONOSPACE);
In the above line of code we first define a button and finally using setTypeface property to controlling font.
Run your app. Below is the output.
Similarly we created three more button. Below is the image.
Exercise for you. Create other remaining button.
Below is the font we used for remaining buttons.
In this article we are controlling font using Java code. If you want to know how to control font using xml check this article.
And finally I hope you learned something from this article. Do not forgot to share the amazing app you created after reading this article.
No comments: