Search code examples
javaandroidxmltextviewtext-size

Increase and decrease text size on button click in Android


I am a beginner.

I want to increase text size of a textview when a button is clicked, also decrease the size when another button is clicked.

But the problem is, it's only increasing, the size of text is not decreasing.

I'm getting text size using getTextSize() method.

Here is my XML

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".textSize">

    <Button
        android:id="@+id/up"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="124dp"
        android:text=" Size Up"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/down"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/down"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="24dp"
        android:layout_marginBottom="124dp"
        android:text=" Size down"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/up" />

    <TextView
        android:id="@+id/myTextView"
        android:layout_width="328dp"
        android:layout_height="58dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="216dp"
        android:layout_marginEnd="8dp"
        android:text="TextView"
        android:textSize="30dp"
        app:layout_constraintEnd_toEndOf="@+id/down"
        app:layout_constraintStart_toStartOf="@+id/up"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Here is Java code:

package com.example.toast;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class textSize extends AppCompatActivity {
TextView textView;
Button upButton, downButton;
Float textSize,diff=2.0f;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_size);

        upButton=findViewById(R.id.up);
        downButton= findViewById(R.id.down);
        textView=findViewById(R.id.myTextView);

        upButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textSize=textView.getTextSize();
                textSize = textSize+diff;
           textView.setTextSize(textSize);

            }
        });
        downButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textSize=textView.getTextSize();
                textSize = textSize-diff;
                textView.setTextSize(textSize);

            }
        });

    }
}

Please tell me where I'm making mistake.


Solution

  • Instead of setting and using a textsize class variable, just do:

    textView.setTextSize(textView.getTextSize() + diff)
    

    and

    textView.setTextSize(textView.getTextSize() - diff)