λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸ’š 개발/μ•ˆλ“œλ‘œμ΄λ“œ

[ μ•ˆλ“œλ‘œμ΄λ“œ] λ²„νŠΌ λˆŒλ €μ„ λ•Œ λ²„νŠΌ 색깔과 ν…μŠ€νŠΈ 색깔이 λ°”λ€Œκ²Œ ν•˜κΈ°. λ²„νŠΌ μ»€μŠ€ν…€! - selector, ripple

by devshin.kr 2021. 11. 10.
728x90

μ΄λ ‡κ²Œ λ˜λŠ” 것을 λ§Œλ“€ 것이닀.

(Before) μ•ˆλ“œλ‘œμ΄λ“œμ—μ„œ μ œκ³΅ν•˜λŠ” κΈ°λ³Έ λ²„νŠΌ
(After) λ‚΄κ°€ μ»€μŠ€ν…€ν•œ λ²„νŠΌ

 

쑰금 λ³΅μž‘ν•΄ 보일 수 μžˆλŠ”λ°... γ…Ž 천천히 따라 였면 쉽닀.

λ³΅μž‘ν•˜κ³  λͺ¨λ₯΄κ² μœΌλ©΄ 일단 λ³΅λΆ™ν•΄μ„œ λ„£κ³  값을 λ°”κΏ” κ°€λ©΄μ„œ 확인해 λ³΄μ‹œκΈΈ..

 

 

0.

MainActivity.java

(νŒ¨ν‚€μ§€λͺ…κΉŒμ§€ λ”°λΌν•˜μ§€λŠ” 말 것... κ°„ν˜Ή 그런 μ‚¬λžŒλ“€λ„ μžˆλŠ” 것 κ°™μ•„μ„œ.. ν˜Ήμ‹œλ‚˜)

앱이 μ‹€ν–‰λ˜κ³  2.5 초 뒀에 λ²„νŠΌμ΄ ν™œμ„±ν™”λ˜λŠ” μ•‘μ…˜μ„ λ§Œλ“€μ–΄ 보자.

package com.devshin93.drawabletest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.button);

        // 앱이 μ‹€ν–‰λ˜κ³  2.5초 뒀에 λ²„νŠΌμ΄ ν™œμ„±ν™” λœλ‹€.
        new android.os.Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                btn.setEnabled(true);
            }
        }, 2500);
    }
}

 

 

1.

activtity_main.xml

src > main > res > layout μ—μ„œ ν…ŒμŠ€νŠΈ νŽ˜μ΄μ§€λ‘œ μ‚¬μš©ν•  activity_main.xml 을 λ§Œλ“ λ‹€. (μ•ˆλ“œλ‘œμ΄λ“œ ν”„λ‘œμ νŠΈκ°€ μƒμ„±λ˜λ©΄ 기본으둜 λ§Œλ“€μ–΄μ Έ μžˆμ„ 것이긴 함.)

activity_main.xml 을 λ§Œλ“€κ³  μ•ˆμ— μ½”λ“œλ₯Ό μž‘μ„±ν•œλ‹€κ³  ν•΄μ„œ λΉŒλ“œ λ˜μ§€ μ•ŠλŠ”λ‹€. @drawable/btn_ripple κ³Ό @drawable/txt_color κ°€ μ•ˆ λ§Œλ“€μ–΄μ Έ 있기 λ•Œλ¬Έ. μ•„λž˜μ—μ„œ λ§Œλ“€ 것

μ£Όμ˜ν•  점 1. λ²„νŠΌμ„ λ§Œλ“œλŠ” 데 λ²„νŠΌ νƒœκ·Έκ°€ <Button 이 μ•„λ‹Œ, <android.appcompat.widget.AppCompatButton 으둜 μ‹œμž‘ν•œλ‹€.

μ£Όμ˜ν•  점 2. λ²„νŠΌμ˜ 색깔을 λ°”κΏ” μ£ΌκΈ° μœ„ν•΄ android:background="@drawable/btn_ripple" 을 μΆ”κ°€ν–ˆλ‹€.

μ£Όμ˜ν•  점 3. λ²„νŠΌμ˜ 색깔이 λ°”λ€œμ— 따라 λ²„νŠΌ μ•ˆμ˜ ν…μŠ€νŠΈ 색깔도 λ°”κΏ” μ£ΌκΈ° μœ„ν•΄ android:textColor="@drawable/txt_color" 을 μΆ”κ°€ν–ˆλ‹€.

μ£Όμ˜ν•  점 4. λ²„νŠΌμ΄ ν™œμ„±ν™” λ˜κ±°λ‚˜ λΉ„ν™œμ„±ν™” 됨에 따라 색을 λ°”κΏ” μ£ΌκΈ° μœ„ν•΄ android:enable:"false" λ˜λŠ” "true" 둜 ν•œλ‹€. (아무 값도 μ—†λ‹€λ©΄, λ””ν΄νŠΈλŠ” true 이닀.)
(μ—¬κΈ°μ—μ„œ false λ‚˜ true 둜 μΌ€μ΄μŠ€μ— 따라 λ°”λ€ŒλŠ” μ½”λ“œλŠ” java μ—μ„œ μž‘μ„±ν•œλ‹€.)
<?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">

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:background="@drawable/btn_ripple"
        android:textColor="@drawable/txt_color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:enabled="false"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

 

2.

btn_disable.xml

src > main > res > drawable μ—μ„œ μƒˆλ‘œμš΄ shape λ₯Ό μƒμ„±ν•œλ‹€.

더보기

μƒˆλ‘œμš΄ 파일 μƒμ„±ν•˜λŠ” 방법

android:enable="false" 일 λ•Œ κ°€μ§€κ²Œ 될 λ²„νŠΌμ˜ λͺ¨μ–‘ 및 색깔이닀.
android:shape="rectangle" : λͺ¨μ–‘은 μ‚¬κ°ν˜•
android:padding="10dp" : μ‚¬κ°ν˜• λͺ¨μ–‘ μ•ˆμ— λ“€μ–΄ 올 ν…μŠ€νŠΈμ˜ νŒ¨λ”© κ°’
android:color="#CBCBCB" : μ‚¬κ°ν˜•μ˜ 색깔
android:bottomLeftRadius="5dp" ... λ“±μ˜ radius : λͺ¨μ„œλ¦¬μ˜ 각(?) 0dp 둜 갈 수둝 λΎ°μ‘±ν•˜κ³ , 값이 컀질 수둝 λ‘₯κ·Ό λͺ¨μ–‘이 λœλ‹€.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding = "10dp">
    <solid android:color="#CBCBCB"/>
    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp"/>
</shape>
더보기

Split 을 λˆ„λ₯΄λ©΄ λ§Œλ“€κ³  μžˆλŠ” λͺ¨μ–‘을 ν™•μΈν•˜λ©΄μ„œ λ§Œλ“€ 수 μžˆλ‹€.

λͺ¨μ„œλ¦¬μ™€ 색깔을 확인해 보자.

 

 

 

3.

btn_normal.xml

src > main > res > drawable μ—μ„œ μƒˆλ‘œμš΄ shape λ₯Ό μƒμ„±ν•œλ‹€.

android:enable="true" 일 λ•Œ κ°€μ§€κ²Œ 될 λ²„νŠΌμ˜ λͺ¨μ–‘ 및 색깔이닀.
2번과 λ‹€λ₯Έ 것은 android:color κ°’ 뿐이닀.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding = "10dp">
    <solid android:color="#1473E6"/>
    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp"/>
</shape>
더보기

λͺ¨μ„œλ¦¬μ™€ 색깔을 ν™•μΈν•˜μž.

 

 

 

4.

btn_ok.xml

src > main > res > drawable μ—μ„œ μƒˆλ‘œμš΄ selector λ₯Ό μƒμ„±ν•œλ‹€.

<selector μ•ˆμ—λŠ” <item 듀이 λ“€μ–΄κ°€κ²Œ λœλ‹€.
android:state_enabled="true" 인 경우, μ–΄λ–€ λͺ¨μ–‘μ˜ λ²„νŠΌμ„ κ°€μ§€κ²Œ 될 지 μ •ν•΄μ£Όκ³ , <!-- 3λ²ˆμ—μ„œ λ§Œλ“€μ–΄ μ€€ btn_normal.xml 을 μ‚¬μš©ν•˜μž. -->
android:state_enabled="false" 인 경우, μ–΄λ–€ λͺ¨μ–‘μ˜ λ²„νŠΌμ„ κ°€μ§€κ²Œ 될 지 μ •ν•΄μ€€λ‹€. <!-- 2λ²ˆμ—μ„œ λ§Œλ“€μ–΄ μ€€ btn_disable 을 μ‚¬μš©ν•˜μž. -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="true"
        android:drawable="@drawable/btn_normal"/>
    <item
        android:state_enabled="false"
        android:drawable="@drawable/btn_disable"/>
</selector>
더보기

μ•„μ΄ν…œμ˜ μƒνƒœμ— 따라 μœ„μ—μ„œ λ§Œλ“€μ–΄μ€€ xml 둜 바뀐닀.

 

 

 

5.

btn_ripple.xml

src > main > res > drawable μ—μ„œ μƒˆλ‘œμš΄ ripple 을 μƒμ„±ν•œλ‹€.

ripple : μž”λ¬Όκ²° (사전적 의미)
ripple 은 λ²„νŠΌμ„ λˆŒλ €μ„ λ•Œ, μΌμ‹œμ μœΌλ‘œ λ°”λ€ŒλŠ” 것을 λ§ν•œλ‹€. 4λ²ˆμ—μ„œ λ§Œλ“€μ–΄ μ€€ btn_ok.xml 을 μ‚¬μš©ν•œλ‹€.
android:color="@color/white" <!-- λ²„νŠΌμ„ λˆŒλ €μ„ λ•Œ μΌμ‹œμ μœΌλ‘œ ν•˜μ–€μƒ‰μœΌλ‘œ λ°”λ€Œμ—ˆλ‹€ λŒμ•„ μ˜¨λ‹€. -->
android:drawable="@drawable/btn_ok" <!-- 4λ²ˆμ—μ„œ λ§Œλ“  btn_ok.xml μ‚¬μš© -->
btn_ripple.xml 은 1λ²ˆμ—μ„œ android:background="@drawable/btn_ripple" 둜 λ“€μ–΄κ°€κ²Œ λœλ‹€.
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/white">
    <item android:drawable="@drawable/btn_ok"/>
</ripple>
더보기

μΌμ‹œμ μœΌλ‘œ λ°”λ€ŒλŠ” 색깔은 ν•˜μ–€μƒ‰μœΌλ‘œ,

λ²„νŠΌμ˜ 색깔은 μœ„μ—μ„œ λ§Œλ“€μ–΄ μ€€ btn_ok(ν‰μ†Œ 색깔이 νŒŒλž‘μƒ‰μ΄μ–΄μ„œ λ―Έλ¦¬λ³΄κΈ°μ—μ„œλŠ” νŒŒλž‘μœΌλ‘œ λœ¨λ‚˜ 보닀)

 

 

 

6.

txt_color.xml

src > main > res > drawable μ—μ„œ μƒˆλ‘œμš΄ selector 을 μƒμ„±ν•œλ‹€.

2~5λ²ˆμ€ λ²„νŠΌμ˜ 색깔과 λͺ¨μ–‘을 μž‘μ•„μ£ΌκΈ° μœ„ν•¨μ΄μ—ˆκ³ ,
6λ²ˆμ€ λ²„νŠΌμ΄ 변함에 따라 ν…μŠ€νŠΈμ˜ 색깔을 λ°”κΏ” μ£ΌκΈ° μœ„ν•΄ μž‘μ„±ν•˜λŠ” μ½”λ“œμ΄λ‹€.
txt_color.xml 은 1λ²ˆμ—μ„œ android:textColor="@drawable/txt_color" 둜 λ“€μ–΄κ°€κ²Œ λœλ‹€.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_enabled="true"
        android:color="#FFFFFF" />
    <item android:state_enabled="false"
        android:color="#AEAEAE" />

</selector>
더보기

#FFFFFF 와 #AEAEAE λŠ” λ―Έλ¬˜ν•˜κ²Œ 닀름!! ν•˜μ–‘κ³Ό νšŒμƒ‰!

 

λŒ“κΈ€