0% found this document useful (0 votes)
36 views

Program 5

This document describes a counter application with XML and Java code. The XML layout defines the user interface which includes labels and buttons to display and control the counter. The Java code implements a background thread to increment the counter value every second, updating the main thread's text view. Buttons allow starting and stopping the counter and updating the thread and UI accordingly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Program 5

This document describes a counter application with XML and Java code. The XML layout defines the user interface which includes labels and buttons to display and control the counter. The Java code implements a background thread to increment the counter value every second, updating the main thread's text view. Buttons allow starting and stopping the counter and updating the thread and UI accordingly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1 Program 5: Counter Application

XML Code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Counter application"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#03A9F4"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.081" />

<TextView
android:id="@+id/txt_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Counter Value"
android:textAllCaps="true"
android:textColor="#FF0000"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.193" />

<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:textAllCaps="true"
android:textColor="#FFEB3B"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.303" />

<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stop"
android:textAllCaps="true"
android:textColor="#FFEB3B"
Mobile Application Development – 18CSMP68
2 Program 5: Counter Application

android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.415" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java Code:
package com.example.counter;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener {
Button buttonStart, buttonStop;
TextView counterValue;
public int counter=0;
public boolean running=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStart=(Button)findViewById(R.id.btn_start);
buttonStart.setOnClickListener(this);
buttonStop=(Button)findViewById(R.id.btn_stop);
buttonStop.setOnClickListener(this);
counterValue=(TextView)findViewById(R.id.txt_value);
}

@Override
public void onClick(View v) {
if(v.equals(buttonStart)){
counterStart();

}else if(v.equals(buttonStop)) {
counterStop();
}
}

private void counterStop() {


this.running=false;
buttonStart.setEnabled(true);
buttonStop.setEnabled(false);
}

private void counterStart() {


counter=0;

Mobile Application Development – 18CSMP68


3 Program 5: Counter Application

running=true;
System.out.println("Start->"+Thread.currentThread().getName());
new MyCounter().start();
buttonStart.setEnabled(false);
buttonStop.setEnabled(true);
}

Handler handler = new Handler(Looper.getMainLooper())


{
public void handleMessage(Message mes) {
counterValue.setText(String.valueOf(mes.what));
}
};

class MyCounter extends Thread{


public void run()
{
System.out.println("MyCounter-
>"+Thread.currentThread().getName());
while(running){
counter++;
handler.sendEmptyMessage(counter);
try{
Thread.sleep(1000);
}catch(Exception e){}

}
}
}

Mobile Application Development – 18CSMP68


4 Program 5: Counter Application

Output:

Mobile Application Development – 18CSMP68

You might also like