Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to Increase or Decrease TextView Font Size in Android Programmatically?
Next article icon

How to Increase or Decrease TextView Font Size in Android Programmatically?

Last Updated : 13 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

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: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">      <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:

WhatsAppImage20210212at14416PM
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:


Next Article
How to Increase or Decrease TextView Font Size in Android Programmatically?

A

annianni
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Android
  • Technical Scripter 2020
Practice Tags :
  • Java

Similar Reads

    How to Change TextView Size Programmatically in Android?
    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 thi
    3 min read
    How to Increase/Decrease Screen Brightness in Steps Programmatically in Android?
    Screen brightness is one such factor that directly affects the Users as well as the Battery on a device. Android devices are Smart systems and have an inbuilt system for Auto-Brightness. But mostly this feature is unchecked by the users or set off by default. Irrespective of whether this feature is
    5 min read
    How to Find Dots-Per-Inch (DPI) of Screen in Android Programmatically?
    Dots-Per-Inch or DPI is a measure of pixel density over the physical area on the screen. A pixel is the smallest unit of any screen display. and the sum of all the pixels present on the screen is termed as Screen Resolution. The pixels available to the user are called Viewport and in this article, w
    2 min read
    How to Increase/Decrease Screen Brightness using Volume Keys Programmatically in Android?
    Screen brightness is one such factor that directly affects the Users as well as the Battery on a device. Android devices are Smart systems and have an inbuilt system for Auto-Brightness. But mostly this feature is unchecked by the users or set off by default. Irrespective of whether this feature is
    5 min read
    How to Find the Screen Resolution of a Device Programmatically in Android?
    Screen Resolution refers to the number of pixels on display. A higher resolution means more pixels and more pixels provide the ability to display more visual information. This entity is widely used in applications related to the broadcasting of real-time visuals such as live video, gaming, etc for o
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences