Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Change TextView Size Programmatically in Android?

  • Last Updated : 15 Feb, 2022

A TextView in Android is a UI element to display text. It can be programmed in the layout file statically as well as in the main code dynamically. Thus, various attributes of a TextView such as the text, text color, text size, TextView background, and its size can be changed programmatically. In this article, we will show you how you could programmatically change TextView size in Android. 

In the sample application that we have demonstrated in this article, there are two UI elements that are a TextView and a Button. We have programmed the Button in such a way that with every click, the TextView dimensions are doubled. Meaning, for, before that first click, the TextView length and height are 50 sp and 50sp. After the first click, the dimensions become 100sp. On the second click, the dimensions are 200sp. for length and height. Now follow the below steps to create this application.

Note: This method of changing the TextView size can also be applied to other UI elements.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. We have declared a TextView of 50sp x 50sp and a Button.

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">
  
    <TextView
        android:id="@+id/text_view"
        android:layout_width="50sp"
        android:layout_height="50sp"
        android:background="#0f9d58"
        android:layout_centerInParent="true"/>
  
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/text_view"
        android:text="click"/>
  
</RelativeLayout>

Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. In the main code, we have programmed the Button in such a way that if it is clicked, the current layout parameters of the TextView, i.e. the width and the height are multiplied by 2. Comments are added inside the code to understand the code in more detail.

Kotlin




import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.RelativeLayout
import android.widget.TextView
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declaring and Initializing the 
          // TextView and the Button from the layout file
        val mTextView = findViewById<TextView>(R.id.text_view)
        val mBtn = findViewById<Button>(R.id.btn)
  
        // When the Button is clicked, the current 
          // parameters are fetched in a local value
        // and doubled.
        // This local value is then assigned to the 
          // actual TextView layout parameters.
        mBtn.setOnClickListener {
            val mParams = mTextView.layoutParams as RelativeLayout.LayoutParams
            mParams.apply {
                width *= 2
                height *= 2
            }
            mTextView.layoutParams = mParams
        }
  
    }
}

Output:

We can see that at every Button click, the TextView layout dimensions are doubled from their previous values.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!