Monk
Monk

Reputation: 57

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.

Upvotes: 0

Views: 3326

Answers (2)

Edhar Khimich
Edhar Khimich

Reputation: 1654

This one will work. This is on Kotlin.

class MainActivity : Activity() {

private var counter = 0f
private var size: Float = 0f

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    counter = pixelsToSp(text_view.textSize)

    inc_btn.setOnClickListener {
        changeText(true)
    }

    dec_btn.setOnClickListener {
        if(counter > 0) changeText(false)
    }
}

private fun changeText(incement: Boolean){
    size = if(incement) ++counter
    else --counter

    text_view.textSize = size
}

fun pixelsToSp(px: Float): Float {
    val scaledDensity = resources.displayMetrics.scaledDensity
    return px / scaledDensity
}
}

Updated:

We should be sure to convert px from getTextSize() method to sp.

Upvotes: 0

Noah
Noah

Reputation: 96

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

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

and

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

Upvotes: 1

Related Questions