forked from hussien89aa/AndroidTutorialForBeginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeekBarandThread.text
106 lines (82 loc) · 2.7 KB
/
SeekBarandThread.text
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
//layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hussienalrubaye.androidpermission.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sb1"
/>
<TextView
android:text="counter=1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvCounter"
android:textSize="42sp" />
<Button
android:text="Start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button6"
android:onClick="bustart" />
<Button
android:text="Stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button5"
android:onClick="buStop" />
</LinearLayout>
</RelativeLayout>
//code
SeekBar sb1;
int MaxCounter=100;
TextView tvCounter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sb1=(SeekBar)findViewById(R.id.sb1);
tvCounter=(TextView)findViewById(R.id.tvCounter) ;
sb1.setMax(MaxCounter);
}
boolean IsRunning=false;
int CounterUp=0;
public void bustart(View view) {
IsRunning=true;
myThread t=new myThread();
t.start();
}
public void buStop(View view) {
IsRunning=false;
}
class myThread extends Thread{
@Override
public void run() {
while (IsRunning){
if (CounterUp<MaxCounter){
// call ui
runOnUiThread(new Runnable() {
@Override
public void run() {
sb1.setProgress(CounterUp);
tvCounter.setText("Couunter "+ CounterUp);
}
});
CounterUp=CounterUp+1;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}