Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Prog 5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

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) Firstly Create an Application by Name “CounterActivity”
2) Go to xml code of design change the layout to “RelativeLayout”
3) Add TextView component & change the following properties:
• Size: 38dp
• Text: “Counter Application”
• Center-Align
4) Add TextView component & change the following properties:
• Text: “Counter Value”
5) Add Button components & change the following properties:
• Size: 38dp
• Text: Start
• id: “@+id/btn_start”
6) Add Button components & change the following properties:
• Size: 38dp
• Text: Stop
• id: “@+id/btn_stop”

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>
JAVA CODE
package com.example.progm5;
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++;
}
};
}

You might also like