Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views3 pages

Counter

The document contains an Android layout and Java code for a simple counter application. It features a vertical LinearLayout with a TextView to display the counter and two buttons to start and stop the counting process. The MainActivity class manages the counter logic using a separate thread and a Handler to update the UI.

Uploaded by

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

Counter

The document contains an Android layout and Java code for a simple counter application. It features a vertical LinearLayout with a TextView to display the counter and two buttons to start and stop the counting process. The MainActivity class manages the counter logic using a separate thread and a Handler to update the UI.

Uploaded by

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

<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">

<TextView
android:id="@+id/txt_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="30dp"
android:text="Counter" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_gravity="center"
android:id="@+id/btn_start"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:layout_gravity="center"
android:id="@+id/btn_stop"/>
</LinearLayout>

package com.example.counter;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener,
Runnable {
int i=0;
TextView txtcount;
Button btnstart,btnstop;
Thread thread;
boolean running=false;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnstart=(Button)findViewById(R.id.btn_start);
btnstop=(Button)findViewById(R.id.btn_stop);
btnstart.setOnClickListener(this);
btnstop.setOnClickListener(this);
txtcount=(TextView)findViewById(R.id.txt_count);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method
if(v.equals(btnstart))
{
running=true;
thread=new
Thread(this);
thread.start();
}
else if(v.equals(btnstop))
{ //thread.interrupt();
running=false;
}
}
Handler hand;

{
hand = new Handler() {
public void handleMessage(Message m) {
txtcount.setText("" + m.what);
}
};
}

@Override
public void run() {
// TODO Auto-generated method stub
//int i=0;
while(i<100 && running)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch
System.out.println(e);
}
hand.sendEmptyMessage(i);
i++;
}
}
}

You might also like