Android slider making textt bigger

  Kiến thức lập trình
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity  {

    public Button button;
    public TextView napis;
    public SeekBar slider;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        button = findViewById(R.id.button);
        napis = findViewById(R.id.napis);
        String[] napisy = {"coś1", "coś2", "coś3", "coś4"};
        int a = 0;
         button.setOnClickListener(new View.OnClickListener() {
             int a = 0;
             @Override
             public void onClick(View v) {

                 a++;

                 if(a > 3){
                     a = 0;
                     napis.setText(napisy[a]);
                 }
                 napis.setText(napisy[a]);


             }
         });

        slider = findViewById(R.id.slider);
        slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                napis.setTextSize(TypedValue.COMPLEX_UNIT_SP, progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


    }


}

This code is for an Android app’s main activity. Here’s a breakdown:

It imports necessary Android libraries.
The MainActivity class extends AppCompatActivity.
It overrides the onCreate method to set up the activity.
It enables edge-to-edge display using the EdgeToEdge library.
It sets the content view to activity_main.
It adjusts padding to account for system bars using ViewCompat.setOnApplyWindowInsetsListener.
It initializes UI elements: a Button, a TextView, and a SeekBar.
It sets an array of strings (napisy) and an integer variable a.
It sets a click listener for the button to cycle through the array elements and update the text view accordingly.
It sets a listener for the seek bar to dynamically change the text size of the text view based on the seek bar’s progress.
Overall, this code sets up a simple Android app with a button to cycle through text and a seek bar to adjust text size.

LEAVE A COMMENT