Counter App In Android Using Android Studio

Introduction

 
In this article, we are going to see how to create a Counter app in Android using the Android Studio. It will show you how many times the activity is done; i.e., operation is repeated. There are many types of counter processes that can be done.
 
Step 1
 
Create a new project in Android Studio.
 
Android
 
Give a name to the project and click "Next".
 
Android
 
Select the "Phone and Tablet" and click "Next".
 
Android
 
Select an empty activity and click "Next".
 
Android
 
At last, give the activity a name and click on "Finish".
 
Android
 
Step 2
 
Setup the Gradle by just locate the Gradle Scripts>>Build. Gradle 1.
 
Android
 
And type the following dependency in your app's build.gradle.
 
Android
 
Step 3
 
Locate the Gradle Scripts>>Build.Gradle 2
 
Android
 
And type the following dependency in your app's build.gradle.
 
Android
 
Step 4
 
Next, go to app >> res >> layout >> activity_main.xml. Select activity page.
 
Android
 
And, just type the code as follows.
 
Android
 
The code copy is given below.
  1. <TextView  
  2.        android:id="@+id/tx"  
  3.        android:layout_width="wrap_content"  
  4.        android:layout_height="wrap_content"  
  5.        android:layout_centerInParent="true"  
  6.        android:layout_centerVerticle="true"  
  7.        android:text="0"  
  8.        android:textAppearance="?android:attr/textAppearance"  
  9.        android:textSize="100sp" />  
  10.   
  11.    <Button  
  12.        android:id="@+id/bt"  
  13.        android:layout_width="wrap_content"  
  14.        android:layout_height="wrap_content"  
  15.        android:text="click me"  
  16.        android:layout_alignParentBottom="true"  
  17.        android:layout_alignParentRight="true"  
  18.        android:layout_alignParentEnd="true"  
  19.        android:layout_alignParentLeft="true"  
  20.        android:layout_alignParentStart="true"  
  21.   
  22.        />  
Step 5
 
Next, go to app >> java>>Mainactivity.java. Select Mainactivity page:
 
Android
 
And just type the code as below.
 
Android  
 
Code copy is here,
  1. public class MainActivity extends AppCompatActivity {  
  2.     private int mCounter = 0;  
  3.     Button btn;  
  4.     TextView txv;  
  5.   
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.   
  11.         btn (Button) findViewById(R.id.bt);  
  12.         txv (TextView) findViewById(R.id.tx);  
  13.   
  14.         btn.setOnClickListener(new View.OnClickListener() {  
  15.             @Override  
  16.             public void onClick(View view) {  
  17.                 mCounter ++;  
  18.                 txv.setText(Integer.toString(mCounter));  
  19.             }  
  20.         });  
  21.     }  
  22. }  
Step 6
 
After step 4, Sync all the dependency gradles and Mainactivity.java resource files by clicking the Sync button on the top right corner of the gradle page.
 
Android
 
Step 7
 
Verify the preview.
->After the code is applied, the preview will appear like this.
 
Android
 
Step 8
 
Next, go to Android Studio and deploy the application. Select an Emulator or your Android mobile with USB debugging enabled. Give it a few seconds to make installations and set permissions.
 
Android
 
Run the application in your desired emulator (Shift + F10).
 
Android
 
Explanation of source code
 
The source code provided in this article is just the dependencies of activity and the code used in activity_main.xml will create the counting process and define its attributes.
 

Summary

 
In this article we created the app named Counter app, then we have inserted a Gradle and we learned how to use the counter and finally, we have deployed that as output.
 
*Support and Share; Thank You*