안드로이드 세계

[Android] 버튼 백그라운드 변경문제 본문

안드로이드(Android)/코틀린(Kotlin)

[Android] 버튼 백그라운드 변경문제

리안94 2021. 2. 19. 17:49

최신 버전(Android Studio 4.1.2)으로 올라오면서 버튼 백그라운드로 색상을 변경하게 되면 변경되지 않는 문제점이 발견되었다.

 

이전까지의 적용방법으로 적용을 해본다. 라운드 처리를 하고 버튼의 색을 빨간색, 윤곽선을 흰색으로 해보겠다.

button_round.xml

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

    <corners
        android:radius="10dp"/>

    <solid
        android:color="@color/red"/>

    <stroke
        android:color="@color/white"
        android:width="2dp"/>

</shape>

 

activity_main.xml

<?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">

    <Button
        android:id="@+id/btn_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_round"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

이전에는 이렇게 사용하면 라운드된 버튼과 색이 변경된 것을 볼 수 있었다.

 

하지만 최신 버전으로 들어오게 되면 라운드 처리는 되었지만 색의 변경이 전혀 이루어지지 않은 것을 확인할 수 있다.

 

해결방법은 다음과 같다.

 

  1. backgroundTint = "@null" background = "@drawable/button_round"적용
  2. Button -> AppCompatButton으로 변경
  3. Theme에서 MaterialComponents -> AppCompat으로 변경

 

 

위와 같은 방법이 있다.

 

1번의 방법

<?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">

    <Button
        android:id="@+id/btn_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_round"
        app:backgroundTint="@null"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

2번의 방법

<?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/btn_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_round"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

3번의 방법

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.AppName" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

 

3번의 경우 테마가 변경이 되었기 때문에 아래의 사진을 참고하여 내용을 수정해주면 좋다.

 

버튼 이슈 : github.com/material-components/material-components-android/issues/889

 

[Button] android:background not working · Issue #889 · material-components/material-components-android

Description: after I change my app them to Theme.MaterialComponents.NoActionBar , In my xml file Button label's android:background="@drawable/login_btn_bg" not working,and I can't...

github.com

 

Comments