Skip to main content

Android Counter App

In this article, we are going to make a simple counter android app. This app lets you start a timer which starts counting from 1 and stops when you press the button.
1. Create a new android project.
2. Open activity_main.xml and add the following code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context="com.example.gunjan.androidcodemode.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_counter"
        android:textSize="30sp"
        android:text="0"
        android:layout_gravity="center"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bt_start_stop"
        android:text="Start"/>
</LinearLayout>
3. Open MainActivity.java and add the following code
package com.example.gunjan.androidcodemode;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {

    // Local Variable
    TextView textViewCount;
    Button buttonStartStop;

    // Initial value of count
    int count = 0;

    // Flag variable to check if start button is clicked
    boolean start = false;

    // Declare handler object
    // Handler is used to schedule messages and runnables which is to
    // be executed in future
    Handler handler = new Handler();

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

        // Initialize variable
        textViewCount = (TextView)findViewById(R.id.tv_counter);
        buttonStartStop = (Button)findViewById(R.id.bt_start_stop);

        // Set click listener on button to handle click events
        buttonStartStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // If start button is clicked
                // Set start to true
                // Start the initial runnable task by posting through the handler
                if(start == false){
                    start = true;
                    handler.post(runnable);
                }
                // Else,
                // Means button is clicked again to stop the counter
                // Set start to false
                // Change text of button
                // disable button
                else{
                    start = false;
                    buttonStartStop.setText("Stop");
                    buttonStartStop.setEnabled(false);
                }

            }
        });
    }

    // Method to run counter till start is true
    // with delay of 1 second
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if(start){
                // Increment count by 1
                // Set count on text view
                count+=1;
                textViewCount.setText(String.valueOf(count));
                handler.postDelayed(runnable, 1000);
            }

        }
    };
}
4. Run app.

OUTPUT-
        

Comments

Popular Posts