Button with getBackground (). SetAlpha version 5 - Lollipop doesn't work correctly

I have this code and it works for every version since API 14, but it doesn't work correctly on Android 5.0 (Lollipop).

The way the buttons are displayed is shown below.

enter image description here


button click1


buttonArrivals.getBackground().setAlpha(180);
buttonDepartures.getBackground().setAlpha(255);

      

enter image description here


button pressed 2


buttonArrivals.getBackground().setAlpha(255);
buttonDepartures.getBackground().setAlpha(180);

      

In the Lollipop version, the buttons are displayed with the same Alpha, but I have never set the same alpha. I am just using the code above.

UPDATE 24/11/2014

Here is the xml of buttons (AutoResizeButton extends button)

br.com.timo.gru.util.AutoResizeButton
            android:id="@+id/buttonArrivals"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="#00abbd"
            android:drawableLeft="@drawable/icon_aviao_desemb"
            android:drawablePadding="-5dp"
            android:drawableStart="@drawable/icon_aviao_desemb"
            android:gravity="center"
            android:paddingEnd="0dp"
            android:paddingLeft="2dp"
            android:paddingRight="0dp"
            android:text="@string/chegadas"
            android:textColor="@android:color/white"

br.com.timo.gru.util.AutoResizeButton
            android:id="@+id/buttonPartidas"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="#00abbd"
            android:drawableLeft="@drawable/icon_aviao_partida"
            android:drawablePadding="-5dp"
            android:drawableStart="@drawable/icon_aviao_partida"
            android:ellipsize="end"
            android:gravity="center"
            android:text="@string/partidas"
            android:textColor="@android:color/white"

      

+3


source to share


1 answer


Internally, the ColorState (used by ColorDrawable) is shared between the two buttons (optimization), so whenever you change the alpha on the background of one button, the other button will also receive that change. You can try to change the background binding before changing its alpha:

buttonArrivals.getBackground().mutate().setAlpha(180);
buttonDepartures.getBackground().mutate().setAlpha(255);

      

You can also read a good explanation by Romain Guy on why this happens: http://curious-creature.org/2009/05/02/drawable-mutations

However, it looks like you are trying to implement something that is easily achievable with Android selectors. You can specify a different color for each button state (in your case selected / unselected), so in your code, you just need to update the state:



buttonArrivals.setSelected(true);
buttonDepartures.setSelected(false);

      

And the selector will look like this:

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

    <item android:color="#ff00abbd"
        android:state_selected="true" >
    </item>

    <item android:color="#b400abbd"
        android:state_selected="false">
    </item>

</selector>

      

+7


source







All Articles