0

With one button i'm disabling three other button's with button.setEnabled(false).
That works fine.
When this button is pressed again, the tree button's will be enabled with button.setEnabled(true).
They are still doing what they shoud and are fine responding to the onClickListener.
But since the enabling they do not visible respond when they get pressed.

How do i reactivate them right?
(i already searcht in google but didn't find anything).


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);
    buttonDrink.setBackgroundColor(getResources().getColor(R.color.transparent));
    buttonEat.setBackgroundColor(getResources().getColor(R.color.transparent));
    buttonWash.setBackgroundColor(getResources().getColor(R.color.transparent));
    buttonSleep.setBackgroundColor(getResources().getColor(R.color.transparent));
    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));
}

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

enter image description here

Spenhouet
  • 6,556
  • 12
  • 51
  • 76

1 Answers1

1

This is because you are setting the background resource as transparent instead of what you applied in the layout as the background drawable to those buttons. Re-apply those background drawables and you'll get the touch feedback again. That should solve your problem.

On a side note for future implementations you can directly provide the enabled/disabled background resource in the drawable's list-selector itself. use state_enabled = true/false. This eliminates the need of doing all of that in the code.

Sam
  • 3,413
  • 1
  • 18
  • 20
  • That sounds like it makes sense but how do i re-apply the background drawables? – Spenhouet May 25 '13 at 10:22
  • in your stop sleeping function instead of setBackgroundColor... do `setBackgroundDrawable(getResources().getDrawable(R.drawable.yourDrawableName));` – Sam May 25 '13 at 10:26
  • bevor you answerd i tried: buttonDrink.setBackgroundColor(android.R.attr.selectableItemBackground); But had the same effect as setting it transparent. But i think you get me wrong. I don't use drawables for buttons. – Spenhouet May 25 '13 at 10:36
  • so what you are saying is that you were using the buttons as stock ... and only while disabling them you added background color? If you were using stock buttons then the drawable set by default already has a in-built drawable for the disabled view. So you don't need to setBackroundColor after disabling either. However if you must change it on disabling, then you must add the stock drawable back on enabling. `setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default);` – Sam May 25 '13 at 10:48
  • Not exactly as stock. I changed something (that i don't know anymore) so that the bottons have no surrounding. I've setted the backgroundDrawable with btn_default and yes there is a touch feedback. But it looks like a button on my original button. Oh man, why didn't i documented what i did with this buttons... :/ But thanks that you are trying to help me anyway... :) – Spenhouet May 25 '13 at 11:18
  • pass `android.R.attr.selectableItemBackground` to `setBackgroundDrawable` not to `setBackgroundColor`. That will do it. Bear in mind this was added in API level 11, won't work with with versions below that. – Sam May 25 '13 at 11:29
  • I'm working with API 14 so this is no problem. But setBackgroundDrawable just accepts drawables and attr.slectableItemBackground isn't a drawable (i belive). I tried using it as a drawable resource but then the app is crashing (eventually because it isn't a drawable). Edit: oh no... the description tells me that it is a drawable: "public static final int selectableItemBackground Added in API level 11 Background drawable for standalone items that need focus/pressed states. " – Spenhouet May 25 '13 at 11:36
  • http://developer.android.com/reference/android/R.attr.html#selectableItemBackground - documentation says it's a drawable. Why not just create your drawable if the framework's resource is not working, there would be no point of discussion then. -> http://developer.android.com/guide/topics/resources/color-list-resource.html – Sam May 25 '13 at 11:40
  • I was doing the buttons like this: http://stackoverflow.com/questions/8855791/how-to-create-standard-borderless-buttons-like-in-the-design-guidline-mentioned – Spenhouet May 25 '13 at 14:36