0% found this document useful (0 votes)
14 views2 pages

COUNTER

The document contains Java code and XML code to create a counter application in Android. It defines a main activity class with methods to increment a counter value displayed in a text view when the start button is pressed and stop incrementing when the stop button is pressed. The XML layout code defines the user interface with text views, buttons, and their properties laid out in a relative layout.

Uploaded by

Preethi D Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

COUNTER

The document contains Java code and XML code to create a counter application in Android. It defines a main activity class with methods to increment a counter value displayed in a text view when the start button is pressed and stop incrementing when the stop button is pressed. The XML layout code defines the user interface with text views, buttons, and their properties laid out in a relative layout.

Uploaded by

Preethi D Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

JAVA CODE

package com.example.counterapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView txtCounter;
Button btn_start,btn_stop;
int count=0;
Handler customHandler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCounter= (TextView)findViewById(R.id.textView2);
btn_start =(Button)findViewById(R.id.button1);
btn_stop=(Button)findViewById(R.id.button2);
btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customHandler.postDelayed(updateTimerThread,0);
}
});
btn_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customHandler.removeCallbacks(updateTimerThread);
}
});
}
private final Runnable updateTimerThread =new Runnable() {
@Override
public void run() {
txtCounter.setText(""+count);
customHandler.postDelayed(this,1000);
count++;
}
};
}

XML CODE

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="332dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="41dp"
android:layout_marginLeft="41dp"
android:layout_marginEnd="38dp"
android:layout_marginRight="38dp"
android:layout_marginBottom="516dp"
android:text="Counter Application"
android:textSize="36sp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="236dp"
android:layout_marginRight="236dp"
android:layout_marginBottom="89dp"
android:text="Start"
android:textSize="30sp"
app:backgroundTint="#4CAF50" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="55dp"
android:layout_marginRight="55dp"
android:layout_marginBottom="92dp"
android:text="STOP"
android:textSize="30sp"
app:backgroundTint="#EC5449" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="165dp"
android:layout_marginRight="165dp"
android:layout_marginBottom="434dp"
android:text="counter value"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>

You might also like