1

I have a custom background defined on the drawable folder that basically makes a rounded shape.

The problem that I'm facing is when I click on the button, the button "loses" the custom background and get a default one of Android.

I have tried creating a selector and adding those items with state_focused and so like this on but nothing works.

Is there an easy way to avoid Android changing the background of a button when clicking on it?

This is my button before the click:

button

And this is my button after the click:

button

Here is the axml with the button code:

<Button
   android:id="@+id/lockDeviceButton"
   android:text="@string/lockThisDevice"                                
   android:textColor="@android:color/white"
   android:background="@drawable/button_rounded_green"
   android:drawableLeft="@drawable/icon_lock"
/>

Here is the rounded layout:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <corners android:radius="24dp" />
  <solid android:color="#03ae50" />
</shape>

And here is what I have tried so far to keep the same background for the states:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <!--  Non focused states 
      -->
  <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/rounded_corners" />
  <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/rounded_corners" />
  <!--  Focused states 
      -->
  <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/rounded_corners" />
  <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/rounded_corners" />
  <!--  Pressed 
      -->
  <item android:state_pressed="true" android:drawable="@drawable/rounded_corners" />
</selector>
Maturano
  • 951
  • 4
  • 15
  • 42
  • 1
    android doesn't change the background color of your button by itself. are you sure you are not doing it in the `onClickListener` block? – MehranB Jan 07 '21 at 19:45
  • 1
    @MehranB After a further investigation, I found out the issue. There was a "hidden" command somewhere in the code that was overwriting the background. Thank you – Maturano Jan 07 '21 at 20:36
  • No worries bro. – MehranB Jan 07 '21 at 20:42

1 Answers1

0

After further investigation, I found out the problem and it was not related to the design itself. It was a hidden code that was overwriting the background of the button for one specific case.

I refactored the code to become cleaner and everything is fine now.

Maturano
  • 951
  • 4
  • 15
  • 42