How do i get the feedback from Borderless buttons back after changing Backgroundcolor? selectableItemBackground

StackOverflow https://stackoverflow.com/questions/16759665

Question

yesterday i tried to solve the problem that my buttons don't give any visible feedback after disabling and enabling them. my problem

But the answers didn't fit in my wish how this buttons shoud be. In the end it came up that i shoud make my own buttons with drawables instead of using the stock buttons. But i disided that i don't want to change my buttons. So i hope i will finde a other way with your help.

The importaned thing to know is that i changed the stock buttons in the way it is discriped her:How to create standard Borderless buttons

my buttons in xml:

        <Button
            android:id="@+id/buttonEat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:paddingBottom="@dimen/padding_size"
            android:paddingTop="@dimen/padding_size"
            android:text="@string/button_eat"
            android:textColor="@color/white"
            android:textColorHint="@color/white"
            android:textSize="@dimen/text_size" />

I'm working with API 14 so the API 11 isue isn't the problem.

I have two methods witch are disabling and enabling my buttons. After enabling them, they still funktion but give no touch feedback.

private void startSleeping()
{
    editorState.putBoolean("SLEEPING", true);
    editorState.commit();

    buttonDrink.setEnabled(false);
    buttonEat.setEnabled(false);
    buttonWash.setEnabled(false);
    buttonDrink.setBackgroundColor(getResources().getColor(R.color.darkgray));
    buttonEat.setBackgroundColor(getResources().getColor(R.color.darkgray));
    buttonWash.setBackgroundColor(getResources().getColor(R.color.darkgray));
    buttonSleep.setBackgroundColor(getResources().getColor(R.color.orange));
    buttonWash.setTextColor(getResources().getColor(R.color.lightgray));
    buttonDrink.setTextColor(getResources().getColor(R.color.lightgray));
    buttonEat.setTextColor(getResources().getColor(R.color.lightgray));
    buttonSleep.setTextColor(getResources().getColor(color.black));
}

private void stopSleeping()
{
    editorState.putBoolean("SLEEPING", false);
    editorState.commit();

    buttonDrink.setEnabled(true);
    buttonEat.setEnabled(true);
    buttonWash.setEnabled(true);

// **Her is the problem**
// **if tried diffrent things**

// **First try: (brings back the old color but not the feedback)**
//      buttonDrink.setBackgroundColor(android.R.attr.selectableItemBackground);
// **Second try: (App crashes. Why? i don't know. The discription of selectableItemBackground says it is a drawable...)**
//buttonDrink.setBackgroundDrawable(getResources().getDrawable(android.R.attr.selectableItemBackground));
// **Third try: (eclips isn't accepting this because the attribut is a int and not a drawable)**
//buttonDrink.setBackgroundDrawable(android.R.attr.selectableItemBackground);
//**Fourth try: (brings back a feedback but changes the lock, feedback color...)**
TypedArray a = getBaseContext().obtainStyledAttributes(new int[]{android.R.attr.selectableItemBackground});
    Drawable backdraw = a.getDrawable(0);
    buttonDrink.setBackgroundDrawable(backdraw);

    buttonEat.setBackgroundColor(android.R.attr.selectableItemBackground);
    buttonWash.setBackgroundColor(android.R.attr.selectableItemBackground);
    buttonSleep.setBackgroundColor(android.R.attr.selectableItemBackground);
    buttonWash.setTextColor(getResources().getColor(R.color.white));
    buttonDrink.setTextColor(getResources().getColor(R.color.white));
    buttonEat.setTextColor(getResources().getColor(R.color.white));
    buttonSleep.setTextColor(getResources().getColor(R.color.white));
}

But there must be a way to give these buttons back funktionality from selectableItemBackground.

The problem seems to be the chaning of the background color. Do anyone have any idea? pls let me know

Was it helpful?

Solution

Instead of changing the background color of the buttons in the code, you can create a custom drawable and there define colors for various states (enabled, pressed, ...). To create a custom drawable, just create an XML file in /res/drawable folder, here is some sample content:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" >
        <shape>
            <!-- define style for disabled state here -->
        </shape>
    </item>
    <item android:state_pressed="true" >
        <shape>
            <!-- define style for pressed state here -->
        </shape>
    </item>
    <item>
        <shape>
            <!-- define style for normal state here -->
        </shape>
    </item>
</selector>

example content for the shape tag:

<solid
    android:color="#ffffff"/>
<stroke
    android:width="1dp"
    android:color="#000000" />
<corners
    android:radius="4dp" />
<padding
    android:left="10dp"
    android:top="10dp"
    android:right="10dp"
    android:bottom="10dp" />

Now you can set this drawable as the button's background in the layout xml file. You then will only have to change the text color of the buttons.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top