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.
Give a name to the project and click "Next".
Select the "Phone and Tablet" and click "Next".
Select an empty activity and click "Next".
At last, give the activity a name and click on "Finish".
Step 2
Setup the Gradle by just locate the Gradle Scripts>>Build. Gradle 1.
And type the following dependency in your app's build.gradle.
Step 3
Locate the Gradle Scripts>>Build.Gradle 2
And type the following dependency in your app's build.gradle.
Step 4
Next, go to app >> res >> layout >> activity_main.xml. Select activity page.
And, just type the code as follows.
The code copy is given below.
- <TextView
- android:id="@+id/tx"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:layout_centerVerticle="true"
- android:text="0"
- android:textAppearance="?android:attr/textAppearance"
- android:textSize="100sp" />
-
- <Button
- android:id="@+id/bt"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="click me"
- android:layout_alignParentBottom="true"
- android:layout_alignParentRight="true"
- android:layout_alignParentEnd="true"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
-
- />
Step 5
Next, go to app >> java>>Mainactivity.java. Select Mainactivity page:
And just type the code as below.
Code copy is here,
- public class MainActivity extends AppCompatActivity {
- private int mCounter = 0;
- Button btn;
- TextView txv;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- btn (Button) findViewById(R.id.bt);
- txv (TextView) findViewById(R.id.tx);
-
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- mCounter ++;
- txv.setText(Integer.toString(mCounter));
- }
- });
- }
- }
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.
Step 7
Verify the preview.
->After the code is applied, the preview will appear like this.
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.
Run the application in your desired emulator (Shift + F10).
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*