1

There are already many similar questions on SO, like this one, but the answers don't apply (anymore?).

This is how I create the button:

<Button
    android:id="@+id/myId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

This is how I change the color:

myButton.getBackground().setColorFilter(lightSalmon, PorterDuff.Mode.SRC);

So far, so good. Now I want to change it back to the default. This is what I tried so far:

myButton.getBackground().clearColorFilter();

makes the button background white instead of the default grey. Same as:

myButton.getBackground().setColorFilter(null);

This completely changes the look of the button:

myButton.setBackgroundResource(android.R.drawable.btn_default);

It does reset the color, but the button looks completely different. Also not what I want.

I found setTint() in the API, but I haven't tried it, because it requires API level 21 (I'm on 15). Is there a backwards compatible way for setTint()?

Trying to remember the default color programmatically doesn't work either, because getColorFilter() also requires API level 21. (setColorFilter()on the other hand was there since API level 1).

Another option I can think of is to just apply the default grey (if I can determine its value). But I fear this might be biased: The default grey might be different on other phones, with other Android versions, other themes and styles...

What other options do I have?

user1785730
  • 3,150
  • 4
  • 27
  • 50
  • I think you should specify the theme you are using like `btn_default_normal_holo_light` to get the correct drawable resource (something like: https://android.googlesource.com/platform/packages/apps/UnifiedEmail/+/89e2c5b/res/drawable/btn_default.xml) – ymz Dec 22 '18 at 20:45
  • Could you expand on that? What do mean "specify the theme"? – user1785730 Dec 22 '18 at 20:57
  • It seems you are using API level 20 or lower... since android v4 there is a default theme named `HOLO` there is a chance that the color you are looking is dependent on this theme... please check https://developer.android.com/reference/android/support/design/R.drawable for reference and try to use `abc_item_background_holo_dark` or `abc_item_background_holo_light` – ymz Dec 22 '18 at 21:15
  • Yes, I'm on API level 15. How exactly am I supposed to use `abc_item_background_holo_dark` or `abc_item_background_holo_light`? – user1785730 Dec 22 '18 at 21:24
  • `myButton.setBackgroundResource(android.R.drawable.abc_item_background_holo_light);` – ymz Dec 23 '18 at 11:07
  • That results in a compile error: Cannot resolve symbol 'abc_item_background_holo_light'. – user1785730 Dec 24 '18 at 17:07

1 Answers1

-1

Use TRANSPARENT

findViewById(R.id.button_id).setBackgroundColor(Color.TRANSPARENT);

It should work.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Sahil Paudel
  • 301
  • 7
  • 13