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 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 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 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 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
How to Change Input Type of EditText Programmatically in Android? EditText in Android UI or applications is an input field, that is used to give input text to the application. This input text can be of multiple types like text (Characters), number (Integers), etc. These fields can be used to take text input from the users to perform any desired operation. Let us s
3 min read
How to Change Typeface of TextView in Android? A typeface is a particular design for alphabets that separates it from other typefaces in terms of style, size, and weight variations. In general, there are a lot of local typefaces available on your device or software for use. However, many more typefaces are available on the Internet that can be d
2 min read
How to Get Internal Memory Storage Space in Android Programmatically? Every device has an internal memory where it can store files and applications. The internal memory of devices can vary anywhere between 4 GB to 512GB. As internal memory gets filled up with a stack of files and applications, the available space decreases. Developers, to save internal memory, design
3 min read
How to Change the Screen Orientation Programmatically using a Button in Android? Generally, the screen orientation of any application is Portrait styled. But when it comes to gaming or any other multimedia service such as watching a video, the screen orientation must change functionally from Portrait to landscape or vice-versa when the functionality is not required. So a develop
3 min read