상세 컨텐츠

본문 제목

[안드로이드] 버튼 색상 바꾸기, 버튼 클릭시 색상 변경

안드로이드

by aries574 2020. 6. 2. 14:33

본문


2021/01/02 - [안드로이드] - [안드로이드] 버튼 숨김(INVISIBLE), 보여짐(VISIBLE), 사라짐(GONE) 만들어 보기

2020/12/22 - [안드로이드] - [안드로이드]버튼 색상 모양 코드에서 변경하는 방법

2020/06/08 - [안드로이드] - [안드로이드]버튼클릭 랜덤숫자 생성 랜덤 배경색 바꾸기 Math.random()


 

안드로이드에서 흔하게 사용하는게 바로 버튼입니다. 

기본으로 만들면 회색바탕에 검은글씨로 생깁니다.

그냥 쓰기에는 너무 밋밋하죠.

그럼 색깔을 변경해봅시다.

 


   <Button
       android:id="@+id/button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Button"
/>

 

1. 검은색바탕에 흰색글씨

 

검은색바탕은 background속성을 사용하고, 흰색 글씨는 textColor속성을 사용했습니다.

쉽게는 속성안에 rgb값을 직접 넣을 수 있지만, 공통으로 자주 사용할 시에는 변경 시 

일일이 다 바꿔줘야 하기 때문에 

등록하고 사용하면 편리합니다. 

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON"
android:background="@color/jinBlack"
android:textColor="@color/hite"
/>

 

사용법은 아래경로를 보시면 됩니다.

2020/04/14 - [안드로이드] - [안드로이드] Background(백그라운드) 색상 등록 및 사용

 

버튼의 색상을 바꾼거는 좋았는데, 막상 실행해 보니 버튼을 눌러도 눌렀다는 표시가 전혀 나지 않습니다.

기본버튼일때는 누르면 눌렀다고 변화가 생겼는데 말입니다. 

저도 처음엔 당황했고, 평소에 쓰던 앱들은 버튼 누르면 변화가 있던데 어떻게 했을까? 

고민을 했고, 검색했고 방법을 찾았습니다.

 

바로바로 등록을 하는 것입니다. 

res -> drawable 폴더에 Drawable Resource FIle을 생성합니다.

예) buttonshape.xml, buttonshape2.xml, button_color_drawable.xml

buttonshape: 검은색버튼

buttonshape2: 연한검정버튼

button_color_drawable: 버튼 기본모습, 눌렀을 때 바뀌는 모습

 

 

buttonshape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners
        android:radius="30dp"
        />
    <solid
        android:color="@color/black"
        />
    <padding
        android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp"
        />
    <size
        android:width="150dp"
        android:height="40dp"
        />
</shape>

 

buttonshape2.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners
        android:radius="30dp"
        />
    <solid
        android:color="@color/softBlack"
        />
    <padding
        android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp"
        />
    <size
        android:width="150dp"
        android:height="40dp"
        />
</shape>

button_color_drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"
        android:drawable="@drawable/buttonshape2"/>

    <item android:drawable="@drawable/buttonshape"/>
</selector>

 

colors.xml

<color name="jinBlack">#242424</color>
<color name="softBlack">#7E7E7E</color>
<color name="hite">#FFFFFF</color>

실제 사용법

이제 실행해서 클릭해 보시면 눌렀을때 눌렀다는 걸 알 수 있습니다.

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON"
android:background="@drawable/button_color_drawable"
android:textColor="@color/hite"
/>

 

 

 

2021/01/02 - [안드로이드] - [안드로이드] 버튼 숨김(INVISIBLE), 보여짐(VISIBLE), 사라짐(GONE) 만들어 보기

2020/12/22 - [안드로이드] - [안드로이드]버튼 색상 모양 코드에서 변경하는 방법

2020/06/08 - [안드로이드] - [안드로이드]버튼클릭 랜덤숫자 생성 랜덤 배경색 바꾸기 Math.random()

 

반응형

관련글 더보기

댓글 영역