I have a Widget for my Android App. On this Widget there is a button. The user can freely choose the color of the button. So I also have to set the colors for the different button states dynamically.
Normally, I would do something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorButtonRedTransparentPressed"
android:state_pressed="true" />
<item android:drawable="@color/colorButtonRedTransparentPressed"
android:state_focused="true" />
<item android:drawable="@color/colorButtonRedTransparentNotPressed" />
</selector>
As mentioned, I have to do this dynamically because the user is free to choose the color:
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[] {}, new ColorDrawable(Color.parseColor(customButton.getColorHex())));
drawable.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(Color.parseColor("#0000")));
The challenge:
The button is on a Widget. So i cannot just simply call:
myButton.setBackground(drawable);
I need to call something like this:
remoteView.setInt(R.id.button_fixedvalue, "setBackground", drawable)
But the function expects an integer and I don't have one.
Does anyone have a different approach?
question from:
https://stackoverflow.com/questions/65927525/android-remoteview-set-statelistdrawable-programmatically 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…