Ad Code

Add button number increment and decrement in Android Studio

Hello developers, in this tutorial I will share with you how to add button number increment and decrement in Android Studio. It is a simple and easy step, but I found new users who finding this question, that's why I made this post.



button number incrementor

Add button number increment and decrement in Android Studio

Here at the bottom, I have given the Activity code and also an XML code. Create a new project in Android Studio and add this activity and xml code into your project. And also, do not forget to declare the Activity in AndroidManifest.xml file.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:gravity="center"
    tools:context=".MainActivity">

    <androidx.cardview.widget.CardView
        android:layout_width="wrap_content"
        app:cardCornerRadius="10dp"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="wrap_content"
            android:gravity="center"
            android:layout_height="wrap_content">


            <TextView
                android:id="@+id/decrementButton"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:text="-"
                android:textSize="25sp"
                android:gravity="center"
                android:textStyle="bold"
                android:background="#E53935"
                android:textColor="@color/white"/>


            <TextView
                android:id="@+id/numberText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textColor="#E53935"
                android:layout_marginRight="20dp"
                android:textSize="25sp"
                android:layout_marginLeft="20dp"
                android:text="0"/>


             <TextView
                 android:id="@+id/incrementButton"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:text="+"
                android:textSize="20sp"
                android:gravity="center"
                 android:background="#E53935"
                 android:textStyle="bold"
                android:textColor="@color/white"/>



        </LinearLayout>


    </androidx.cardview.widget.CardView>


</LinearLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView decrementButton, numberText, incrementButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        decrementButton = findViewById(R.id.decrementButton);
        numberText = findViewById(R.id.numberText);
        incrementButton = findViewById(R.id.incrementButton);



        decrementButton.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View view) {

                int currentNumber = Integer.parseInt(numberText.getText().toString());
                numberText.setText(Integer.toString(currentNumber - 1));

                //if you do not need in less then 0

//                if(currentNumber != 0) {
//                    numberText.setText(Integer.toString(currentNumber - 1));
//                } else {
//                    //number is 0
//                }
            }
        });

         incrementButton.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View view) {

                int currentNumber = Integer.parseInt(numberText.getText().toString());
                numberText.setText(Integer.toString(currentNumber + 1));

            }
        });


    }
}

Thank you for visiting our site and if you face any problem related to Android studio then contact me. I will try to help you as soon as possible.

Post a Comment

0 Comments

Ad Code