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:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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.
Similar Reads
How to Increase or Decrease TextView Font Size in Android Programmatically?
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 goi
3 min read
How to Change ActionBar Title Programmatically in Android?
Whenever we develop an Android application and run it, we often observe that the application comes with an ActionBar with the name of the application in it. This happens by default unless explicitly changed. This text is called the title in the application. One can change the title of the applicatio
3 min read
How to Resize Images Programmatically in Android?
There are many applications available on the net for performing image operations such as cropping, reducing image file size, or resizing the images to particular dimensions. Most of these applications are API based where the image is uploaded to anonymous servers, various functions are performed and
3 min read
How to Take Screenshot Programmatically in Android?
In every android phone, we have feature to take screenshots of screens. In this article, we are going to explain how to take screenshots programmatically. Step-by-Step ImplementationStep 1: Create a New ProjectTo create a new project in Android Studio please refer to How to Create/Start a New Projec
4 min read
How to Set Background Drawable Programmatically in Android?
In many android applications, we can get to see that the background color of this application changes dynamically when updated from the server. For updating this color we have to set the background color of our layout programmatically. In this article, we will take a look at How to Set Background Dr
3 min read
How to Change the Whole App Language in Android Programmatically?
Android 7.0(API level 24) provides support for multilingual users, allowing the users to select multiple locales in the setting. A Locale object represents a specific geographical, political, or cultural region. Operations that require these Locale to perform a task are called locale-sensitive and u
5 min read
How to Get the Device's IMEI and ESN Programmatically in Android?
Many times while building Android Applications we require a unique identifier to identify the specific mobile users. For identifying that user we use a unique address or identity. For generating that unique identity we can use the android device id. In this article, we will take a look at How to get
4 min read
How to Get RAM Memory in Android Programmatically?
RAM (Random Access Memory) of a device is a system that is used to store data or information for immediate use by any application that runs on the device. Every electronic device that runs a program as a part of its application has some amount of RAM associated with it. Mobile devices nowadays come
3 min read
How to Programmatically Take a Screenshot on Android?
Ever wanted to take some perfect screenshot of a particular view, or perhaps some UI element ruined your favorite screenshot? Don't worry much, this Geeks for Geeks article will help you achieve it by making an app from scratch. As Below is the code for the title name in this article, we are going t
3 min read
How to programmatically hide Android soft keyboard
In this article, we will learn about how to hide soft keyboard programmatically. The keyboard generally hides but there are certain instances when it does not hide. So for better user experience, the keyboard is hidden programmatically. Without using below approach, the default response of the app i
2 min read