MainActivity.java:

package com.example.myapplication;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    TextView countTextView;
    Button incrementButton;
    Button decrementButton;
    Button resetButton;

    int count = 0;

    // FIX: keep the counter value across screen rotation
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("count", count);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        countTextView = findViewById(R.id.countTextView);
        incrementButton = findViewById(R.id.incrementButton);
        decrementButton = findViewById(R.id.decrementButton);
        resetButton = findViewById(R.id.resetButton);

        if (savedInstanceState != null) {
            count = savedInstanceState.getInt("count", 0);
            countTextView.setText(String.valueOf(count));
        }


        incrementButton.setOnClickListener(v -> {
            count++;
            countTextView.setText(String.valueOf(count));
        });


        decrementButton.setOnClickListener(v -> {
            count--;
            countTextView.setText(String.valueOf(count));
        });

        resetButton.setOnClickListener(v -> {
            count = 0;
            countTextView.setText(String.valueOf(count));
        });
    }
}


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="30dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Counter App"
        android:textSize="28sp"
        android:textStyle="bold"
        android:textColor="#673AB7"
        android:gravity="center"
        android:layout_marginBottom="40dp" />

    <TextView
        android:id="@+id/countTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="60sp"
        android:textStyle="bold"
        android:layout_marginBottom="40dp" />

    <Button
        android:id="@+id/incrementButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="INCREMENT" />

    <Button

        android:id="@+id/decrementButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="DECREMENT" />

    <Button
        android:id="@+id/resetButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="RESET" />

</LinearLayout>
