Skip to content
Related Articles

Related Articles

How to Increase or Decrease TextView Font Size in Android Programmatically?
  • Difficulty Level : Hard
  • Last Updated : 18 Feb, 2021

In this App, we are going to learn how to increase or decrease TextView Font Size in android programmatically. Like we have seen that in many apps we sometimes want to enlarge the text. So here basically we are going to implement that. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Increase or Decrease TextView Font Size in Android Programmatically Sample GIF

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with the activity_main.xml file



Here we are basically creating two Buttons to increase and decrease the text size. And one TextView that contains Loram Ipsum dummy text. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <LinearLayout
        android:id="@+id/wrapper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal"
        android:orientation="horizontal">
  
        <Button
            android:id="@+id/increase"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Increase" />
  
        <Button
            android:id="@+id/decrease"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Decrease" />
    </LinearLayout>
  
    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/wrapper"
        android:layout_centerInParent="true">
  
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
  
        </LinearLayout>
    </ScrollView>
  
    <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="0dp"
        android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis sem augue,
                      aliquam bibendum fringilla quis, volutpat ut arcu. Sed nulla metus, gravida
                      id pulvinar quis, rhoncus in velit. Pellentesque semper mollis leo,
                      vitae molestie risus. Curabitur nec suscipit tortor. Quisque non purus eu
                      quam pretium mollis sed in turpis. Duis elit magna, ullamcorper vitae elementum
                      in, auctor eget ligula. Maecenas ultricies diam non nisl facilisis porta.
                      Suspendisse diam ante, accumsan sit amet enim nec, bibendum semper arcu.
                      Nunc a imperdiet odio. Morbi id est finibus ex mollis interdum vulputate non
                      eros. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vestibulum 
                      sit amet dictum ante, vitae condimentum augue. Proin ultricies enim nisl,
                      eu pharetra arcu venenatis sit amet. Pellentesque sodales, justo eu iaculis
                      rhoncus, magna mi ullamcorper enim, a mattis neque sapien eu nisi. Duis a 
                      turpis euismod nibh mattis egestas sed vel sem. Maecenas non tempor tellus,
                      id facilisis erat. Nullam id commodo nisi. Ut sed arcu lectus. Mauris lacus 
                      libero, pharetra et neque vitae, tincidunt dapibus magna. Sed non scelerisque 
                      leo, non pharetra mi. In sollicitudin metus ut lacus vestibulum efficitur.
                      Sed cursus pellentesque ante at vehicula. Nunc eros metus, mattis at aliquet at,
                      euismod et libero.!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
</RelativeLayout>

This is how our activity will look:

activity_main.xml

Step 3: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    Button increase, decrease;
    private float ourFontsize = 14f;
    TextView text;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        increase = findViewById(R.id.increase);
        decrease = findViewById(R.id.decrease);
        text = findViewById(R.id.tv_text);
        increase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // increasing the size by 4 unit
                ourFontsize += 4f;
                // assigning new textsize to our text
                text.setTextSize(TypedValue.COMPLEX_UNIT_SP, ourFontsize);
            }
        });
        decrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // decreasing the size by 4 unit
                ourFontsize -= 4f;
                // assigning new textsize to our text
                text.setTextSize(TypedValue.COMPLEX_UNIT_SP, ourFontsize);
            }
        });
    }
}

Output:

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!
My Personal Notes arrow_drop_up
Recommended Articles
Page :