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. <Button
  11. android:id="@+id/bt"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="click me"
  15. android:layout_alignParentBottom="true"
  16. android:layout_alignParentRight="true"
  17. android:layout_alignParentEnd="true"
  18. android:layout_alignParentLeft="true"
  19. android:layout_alignParentStart="true"
  20. />
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. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. btn (Button) findViewById(R.id.bt);
  10. txv (TextView) findViewById(R.id.tx);
  11. btn.setOnClickListener(new View.OnClickListener() {
  12. @Override
  13. public void onClick(View view) {
  14. mCounter ++;
  15. txv.setText(Integer.toString(mCounter));
  16. }
  17. });
  18. }
  19. }
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*