-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05. Counter.java
More file actions
140 lines (121 loc) · 4.31 KB
/
05. Counter.java
File metadata and controls
140 lines (121 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
Counter App
-----------------------------------------------------------------------
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:id="@+id/textView"
android:layout_marginTop="100dp"
android:text="Counter App"
android:textSize="36sp"
android:textStyle="bold"
android:textColor="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lbl_text"
android:layout_marginTop="30dp"
android:text="Counter Value"
android:textColor="#020202"
android:textSize="30sp"
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="50dp"
android:text="Start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
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="50dp"
android:text="Stop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_Start"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
-----------------------------------------------------------------------
Java Code
-----------------------------------------------------------------------
package com.example.counter;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
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 lblCouter;
Button btnStart,btnStop;
int counter=0;
boolean running=false;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lblCouter = (TextView) findViewById(R.id.lbl_text);
btnStart = (Button) findViewById(R.id.btn_Start);
btnStop = (Button) findViewById(R.id.btn_Stop);
btnStart.setOnClickListener(this);
btnStop.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;
}
}
@SuppressLint("HandlerLeak")
Handler handler = new Handler()
{
public void handleMessage(Message m)
{
lblCouter.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) { }
}
}
}
}