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

Program 5

Uploaded by

Maithreya TM
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Program 5

Uploaded by

Maithreya TM
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Program 5

Write a program to create an activity with two buttons START and STOP. On
Pressing of the START button, the activity must start the counter by displaying
the numbers from One and the counter must keep on counting until the STOP
button is pressed. Display the counter value in a TextViewcontrol.

1. Create a New Android Project with Empty Activity.


2. Open activity_main.xml file from res layout folder, check/add
ConstraintLayout as theroot view.
3. Create the layout design using Drag and Drop framework.
4. Add Listeners to Button Click Event:
 Create a class which implements OnClickListener interface.
 Override onClick() method of OnClickListener Interface.
Register the button for click event by calling setOnClickListener()
method of Viewclass and pass the object of the class that implemented
OnClickListener Interface.
5. Create a Thread to start the counter logic.
6. Steps to Create a Thread
 Create a class that extends Thread Class.
 Override run method of Thread Class.
Use start() method of thread class to start the thread.
7. Create Handler class to receive message from child thread, Handler
executes in MainThread.
8. Steps to Create Handler
 Create Object of type Handler.
OverridhandleMessage() of handler class.
9. Pass the counter value to be displayed to the handler.
10. Update the UI to display the counter value received from thread.
Design

activity_main.xml

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


<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.andr
oid.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/lbl_counter"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="Counter Application"
android:textSize="36dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView android:id="@+id/lbl_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Counter Value"
android:textColor="@color/colorAccent"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/lbl_text" />

<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Stop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_start" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.program5;

import androidx.appcompat.app.AppCompatActivity;

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

import org.w3c.dom.Text;
public class MainActivity extends
AppCompatActivity implements View.OnClickListener
{
TextView lblCounter;
Button btnStart,btnStop;

int counter=0;
Boolean running=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lblCounter=(TextView)findViewById(R.id.lbl_text);
btnStart=(Button)findViewById(R.id.btn_start);
btnStop=(Button)findViewById(R.id.btn_stop);
btnStop.setOnClickListener(this);
btnStart.setOnClickListener(this);
}

public void onClick(View v)


{
if(v.equals(btnStart))
{
counter=0;
running=true;
new MyCounter().start();
}
else if(v.equals(btnStop))
{
running=false;
}
}

Handler handler=new Handler()


{
public void handleMessage(Message m)
{
lblCounter.setText(String.valueOf(m.what));
}

};

class MyCounter extends Thread


{
public void run()
{
while(running)
{
counter++;
handler.sendEmptyMessage(counter);

try { Thread.sleep(1000);
}
catch(Exception e) { }
}
}
}
}
Sample Output

You might also like