SeekBar는 progress 속성을 사용하여 초기값을 설정할수 있다.


SeekBar 이벤트는 setOnSeekBarChangeListener를 사용한다.


세 가지 이벤트를 사용할 수 있다.

 - onStartTrackingTouch : SeekBar에 터치하는 순간 이벤트 발생

 - onStopTrackingTouch : SeekBar에 터치가 끝나는 순간 이벤트 발생

 - onProgressChanged : SeekBar의 값이 변할 때 이벤트 발생



1
2
3
4
5
6
7
8
9
10
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:progress="50" />
cs


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
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        final TextView textView = (TextView) findViewById(R.id.textview);
        SeekBar seekBar = (SeekBar) findViewById(R.id.seekbar);
        textView.setText(String.valueOf(seekBar.getProgress()));
 
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                textView.setText(String.valueOf(progress));
            }
 
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                textView.setText("트래킹 시작");
            }
 
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                textView.setText("트래킹 종료");
            }
        });
    }
cs


SeekBar를 세로로 표시하려면 rotation 속성을 사용한다.


1
2
3
4
5
    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:rotation="270" />
cs


SeekBar 외형은 background 속성에 배경을 변경


progressDrawable 속성에 진행상태를 변경


thumb 속성에 손잡이를 변경



1
2
3
4
5
6
7
8
    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_seekbar"
        android:padding="10dp"
        android:progressDrawable="@drawable/pd_seekbar"
        android:thumb="@drawable/selector_seekbar_thumb" />
cs


bg_seekbar.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="90"
        android:centerColor="#666666"
        android:centerY="0.5"
        android:endColor="#999999"
        android:startColor="#FFFFFF" />
 
    <corners android:radius="5dp" />
 
    <stroke
        android:width="1dp"
        android:color="#999999" />
 
</shape>
cs


pd_seekbar.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
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="10dp" />
 
            <gradient
                android:angle="-90"
                android:centerColor="#00CC00"
                android:endColor="#FA8072"
                android:startColor="#FA8072" />
 
            <stroke
                android:width="10dp"
                android:color="#999999" />
        </shape>
    </item>
 
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="10dp" />
 
                <gradient
                    android:angle="-90"
                    android:centerColor="#666666"
                    android:centerY="0.5"
                    android:endColor="#F4A460"
                    android:startColor="#F0F8FF" />
 
                <stroke
                    android:width="1dp"
                    android:color="#999999" />
            </shape>
        </clip>
    </item>
</layer-list>
cs


selector_seekbar_thumb.xml

1
2
3
4
5
6
7
8
9
10
11
12
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/bg_seekbar_thumb_pressed"
        android:state_pressed="true" android:state_window_focused="true" />
 
    <item android:drawable="@drawable/bg_seekbar_thumb_pressed"
        android:state_focused="true" android:state_window_focused="true" />
 
    <item android:drawable="@drawable/bg_seekbar_thumb_pressed"
        android:state_selected="true" android:state_window_focused="true" />
 
    <item android:drawable="@drawable/bg_seekbar_thumb_normal" />
</selector>
cs


bg_seekbar_thumb_pressed.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
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:innerRadius="0dp"
            android:shape="ring"
            android:thickness="14dp"
            android:useLevel="false">
            <size
                android:width="32px"
                android:height="32px" />
            <gradient
                android:centerColor="#99cc99"
                android:endColor="#cccccc"
                android:startColor="#cccccc"
                android:type="sweep"
                android:useLevel="false" />
 
            <stroke
                android:width="2dp"
                android:color="#33666666" />
 
        </shape>
    </item>
 
    <item>
        <shape
            android:innerRadius="0dp"
            android:shape="ring"
            android:thickness="10dp"
            android:useLevel="false">
            <size
                android:height="32px"
                android:width="32px" />
            <gradient
                android:centerColor="#ffffff"
                android:centerY="1"
                android:endColor="#666666"
                android:gradientRadius="20"
                android:startColor="#999999"
                android:type="radial"
                android:useLevel="false"/>
            <stroke
                android:width="1dp"
                android:color="#ffffff" />
        </shape>
    </item>
</layer-list>
cs


bg_seekbar_thumb_normal.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
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:innerRadius="0dp"
            android:shape="ring"
            android:thickness="14dp"
            android:useLevel="false">
            <size
                android:height="32px"
                android:width="32px" />
            <gradient
                android:centerColor="#999999"
                android:endColor="#00CED1"
                android:startColor="#8A2BE2"
                android:type="sweep"
                android:useLevel="false" />
 
            <stroke
                android:width="2dp"
                android:color="#33666666" />
        </shape>
    </item>
    <item>
        <shape
            android:innerRadius="0dp"
            android:shape="ring"
            android:thickness="10dp"
            android:useLevel="false">
            <size
                android:height="32px"
                android:width="32px" />
            <gradient
                android:centerColor="#ffffff"
                android:centerY="1"
                android:endColor="#00CED1"
                android:gradientRadius="20"
                android:startColor="#8A3BE2"
                android:type="radial"
                android:useLevel="false" />
            <stroke
                android:width="1dp"
                android:color="#ffffff" />
        </shape>
    </item>
</layer-list>
cs


'Android' 카테고리의 다른 글

[Android] AutoCompleteTextView 사용하기  (0) 2019.03.14
[Android] EditText 사용하기  (0) 2019.03.13
[Android] 라디오 버튼(RadioButton) 사용  (0) 2019.03.11
[Android] CheckBox 사용  (0) 2019.03.08
[Android] ToggleButton/Switch 사용  (0) 2019.03.07

라디오 버튼은 <RadioButton>을 사용한다.


<RadioGroup>을 사용하여 라디오 버튼을 그룹화 할 수 있다.


라디오 그룹의 setOnCheckedChangeListener 이벤트를 사용하여 클릭한 라디오 버튼의 ID를 알 수 있다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
 
        <RadioButton
            android:id="@+id/radiobutton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="라디오버튼1" />
 
        <RadioButton
            android:id="@+id/radiobutton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="라디오버튼2" />
 
 
        <RadioButton
            android:id="@+id/radiobutton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="라디오버튼3" />
    </RadioGroup>
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = (RadioButton) findViewById(checkedId);
                Toast.makeText(MainActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
cs


'Android' 카테고리의 다른 글

[Android] EditText 사용하기  (0) 2019.03.13
[Android] SeekBar 사용  (0) 2019.03.12
[Android] CheckBox 사용  (0) 2019.03.08
[Android] ToggleButton/Switch 사용  (0) 2019.03.07
[Android] Button 상태에 따라 이미지 변경하기  (0) 2019.03.06

체크박스는 <CheckBox>를 사용한다.


On/Off 상태를 구할 땐 setOnCheckedChangeListener를 사용한다.


1
2
3
4
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox);
 
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(MainActivity.this"체크상태 : " + isChecked, Toast.LENGTH_SHORT).show();
            }
        });
    }
cs




ToggleButton과 Switch는 On/Off 상태를 표현할 수 있다.


On/Off 상태 변화 감지는 setOnCheckedChageListener에서 할수 있다.


On일때 isChecked의 값은 True, Off일때 isChecked의 값이 False가 된다.


1
2
3
4
5
6
7
8
9
    <ToggleButton
        android:id="@+id/ToggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <Switch
        android:id="@+id/Switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        ToggleButton tb = (ToggleButton) findViewById(R.id.ToggleButton);
 
        Switch sw = (Switch) findViewById(R.id.Switch);
 
        tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(MainActivity.this"tb : "+isChecked, Toast.LENGTH_SHORT).show();
            }
        });
 
        sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(MainActivity.this"sw : "+isChecked, Toast.LENGTH_SHORT).show();
            }
        });
    }
cs


우선 상태에 따라 drawable 리소스를 만든다.


상태별 이미지를 지정한 selector 리소스를 정의한다. 


정의한 selector 리소스를 Button의 Background로 지정한다.


bg_button_nomal.xml

1
2
3
4
5
6
7
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="270"
        android:endColor="#FF2A68"
        android:startColor="#FF5E3A" />
    <corners android:radius="10dp" />
</shape>
cs


bg_button_pressed.xml

1
2
3
4
5
6
7
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="270"
        android:endColor="#1D62F0"
        android:startColor="#1AD6FD" />
    <corners android:radius="10dp" />
</shape>
cs


selector_button.xml

1
2
3
4
5
6
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/bg_button_normal"
        android:state_pressed="false" />
    <item android:drawable="@drawable/bg_button_pressed"
        android:state_pressed="true" />
</selector>
cs


main.xml

1
2
3
4
5
6
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/selector_button"
        android:text="button"/>
cs


'Android' 카테고리의 다른 글

[Android] CheckBox 사용  (0) 2019.03.08
[Android] ToggleButton/Switch 사용  (0) 2019.03.07
[Android] Button 추가하기  (0) 2019.03.05
[Android] ProgressBar Customize  (0) 2019.03.04
[Android] ProgressBar 표시하기  (0) 2019.03.02

layout에 Button을 추가한다.


1
2
3
4
5
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button"/>
cs


Button의 클릭 이벤트는 setOnClickListener를 이용한다.


layout에서 onClick속성을 이용하는 방법도 있지만 추천하는 방법은 아니므로 setOnClickListener에 구현하는 것이 좋다. 


1
2
3
4
5
6
7
8
9
10
11
12
13
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this"버튼이 클릭되었습니다.", Toast.LENGTH_SHORT).show();
            }
        });
    }
cs


'Android' 카테고리의 다른 글

[Android] ToggleButton/Switch 사용  (0) 2019.03.07
[Android] Button 상태에 따라 이미지 변경하기  (0) 2019.03.06
[Android] ProgressBar Customize  (0) 2019.03.04
[Android] ProgressBar 표시하기  (0) 2019.03.02
[Android] gravity 속성  (0) 2019.03.01

'res/drawable' 폴더에서 진행 부분을 표시할 drawable 리소스를 작성한다.


ProgressBar의 background부분과 진행률을 표시할 부분을 정의한다.


진행률을 표시할 부분은 clip하여 나타낼 수 있도록 <clip> 태그를 넣어준다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
 
    <item android:id="@android:id/background">
        <shape>
            <gradient
                android:angle="270"
                android:endColor="@color/FlatDarkCyan"
                android:startColor="@color/FlatLightCyan" />
        </shape>
    </item>
 
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <gradient
                    android:angle="270"
                    android:endColor="@color/FlatDarkBlue"
                    android:startColor="@color/FlatLightBlue" />
            </shape>
        </clip>
    </item>
</layer-list>
cs


drawable작성이 완료되었으면 'android:progressDrawable'을 사용하여 layout에 적용한다.


1
2
3
4
5
6
    <ProgressBar
        android:id="@+id/progress_horizontal"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:progressDrawable="@drawable/pd_progress"/>
cs


'Android' 카테고리의 다른 글

[Android] Button 상태에 따라 이미지 변경하기  (0) 2019.03.06
[Android] Button 추가하기  (0) 2019.03.05
[Android] ProgressBar 표시하기  (0) 2019.03.02
[Android] gravity 속성  (0) 2019.03.01
[Android] style 속성 사용  (0) 2019.02.28

ProgressBar에서 수평으로 표시하려면 'style="?android:attr/progressBarStyleHorizontal" 을 사용한다.


1
2
3
4
5
    <ProgressBar
        android:id="@+id/progress_horizontal"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
cs


setMax 함수에는 최대값을, setProgress 함수에는 메인 진행률을 setSecondaryProgress 함수에는

서브 진행률을 지정할 수 있다.


1
2
3
4
5
6
7
8
9
10
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_horizontal);
        progressBar.setMax(100);
        progressBar.setProgress(30);
        progressBar.setSecondaryProgress(70);
    }
cs


원형 ProgressBar의 경우 크기에 따라 세가지가 있다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    <ProgressBar        
        android:id="@+id/progress_small"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
 
    <ProgressBar
        android:id="@+id/progressbar"
        style="?android:attr/progressBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
 
    <ProgressBar
        android:id="@+id/progress_large"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
cs


'Android' 카테고리의 다른 글

[Android] Button 추가하기  (0) 2019.03.05
[Android] ProgressBar Customize  (0) 2019.03.04
[Android] gravity 속성  (0) 2019.03.01
[Android] style 속성 사용  (0) 2019.02.28
[Android] TextView에 긴 문자열 생략해서 표시하기  (0) 2019.02.27

+ Recent posts