Saturday, September 24, 2016

Custom Number Picker Android

Print Screen:

2) MainActivity.java
?
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
package com.example.activitylifecycle;
 
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
 
public class MainActivity extends Activity implements OnClickListener {
 Button btnUp, btnDown;
 TextView textViewUp, textViewMid, textViewBottom;
 
 int nStart = 5;
 int nEnd = 250;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  btnUp = (Button) findViewById(R.id.button1);
  btnDown = (Button) findViewById(R.id.button2);
 
  textViewUp = (TextView) findViewById(R.id.textView1);
  textViewMid = (TextView) findViewById(R.id.textView2);
  textViewBottom = (TextView) findViewById(R.id.textView3);
 
  textViewUp.setText("5");
  textViewMid.setText("6");
  textViewBottom.setText("7");
 
  btnUp.setOnClickListener(this);
  btnDown.setOnClickListener(this);
 
 }
 
 @Override
 public void onClick(View v) {
  String getString = String.valueOf(textViewMid.getText());
  int curent = Integer.parseInt(getString);
  if (v == btnUp) {
   if (curent < nEnd) {
    curent++;
    textViewUp.setText(String.valueOf(curent - 1));
    textViewMid.setText(String.valueOf(curent));
    textViewBottom.setText(String.valueOf(curent + 1));
   }
 
  }
  if (v == btnDown) {
   if (curent > nStart) {
    curent--;
    textViewUp.setText(String.valueOf(curent - 1));
    textViewMid.setText(String.valueOf(curent));
    textViewBottom.setText(String.valueOf(curent + 1));
   }
  }
 }
}
3)activity_main.xml
?
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
<RelativeLayout 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:background="#bfbfbf" >
 
    <View
        android:id="@+id/view2"
        android:layout_width="30dp"
        android:layout_height="1dp"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:background="#000000" />
 
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_centerVertical="true"
        android:text="TextView"
        android:textColor="#000000"
        android:textSize="20sp" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:background="@drawable/down_btn" />
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_alignLeft="@+id/button2"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:text="TextView"
        android:textColor="#ccffff"
        android:textSize="20sp" />
 
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:text="TextView"
        android:textColor="#ccffff"
        android:textSize="20sp" />
 
    <View
        android:id="@+id/view1"
        android:layout_width="30dp"
        android:layout_height="1dp"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignLeft="@+id/view2"
        android:layout_centerHorizontal="true"
        android:background="#000000" />
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_alignLeft="@+id/textView1"
        android:layout_marginBottom="18dp"
        android:background="@drawable/up_btn" />
 
</RelativeLayout>

No comments:

Post a Comment