Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Zoom In and Out to TextView in Android

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article, we are going to implement the zoom in and zoom out feature in TextView. As we zoom we will see the image after zoom in. Basically, we are going to learn how to increase or decrease the text size in android. 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. 

Zoom In and Out to TextView in Android 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

Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
        android:textSize="32sp" />

Step 3: Working with the file

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


import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
    TextView text;
    final static float move = 200;
    float ratio = 1.0f;
    int bastDst;
    float baseratio;
    protected void onCreate(Bundle savedInstanceState) {
        text = findViewById(;
        text.setTextSize(ratio + 15);
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getPointerCount() == 2) {
            int action = event.getAction();
            int mainaction = action & MotionEvent.ACTION_MASK;
            if (mainaction == MotionEvent.ACTION_POINTER_DOWN) {
                bastDst = getDistance(event);
                baseratio = ratio;
            } else {
                // if ACTION_POINTER_UP then after finding the distance
                // we will increase the text size by 15
                float scale = (getDistance(event) - bastDst) / move;
                float factor = (float) Math.pow(2, scale);
                ratio = Math.min(1024.0f, Math.max(0.1f, baseratio * factor));
                text.setTextSize(ratio + 15);
        return true;
    // get distance between the touch event
    private int getDistance(MotionEvent event) {
        int dx = (int) (event.getX(0) - event.getX(1));
        int dy = (int) (event.getY(0) - event.getY(1));
        return (int) Math.sqrt(dx * dx + dy * dy);
    public boolean onTouch(View v, MotionEvent event) {
        return false;


My Personal Notes arrow_drop_up
Last Updated : 14 Apr, 2021
Like Article
Save Article
Similar Reads
Related Tutorials