Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Change Gradient Background of Widget

What I am trying to accomplish:

int[] colors = new int[]{colorDark,colorLight}
GradientDrawable gd = new GradientDrawable(TOP_BOTTOM, colors);
remoteView.setBackgroundDrawable(gd); //method does not exist

Obviously this is not possible.

How can I accomplish this? (if it's possible)

I do not want to have to create multiple shapes in xml files for different colors, because this limits options.

I have tried converting my drawable to a bitmap and calling setImageViewBitmap. I converted with this code and used this code to get the width/height, but I'm unable to get the widget to be filled (additionally, the device's display width/height really aren't what I need anyway)

like image 730
Reed Avatar asked Jan 22 '14 22:01

Reed


1 Answers

I am just guessing. Can you try to extend RemoteViews and override the apply function:

public class MySpecialRemoteViews extends RemoteViews {

    //add the Constructors

    public View apply(Context context, ViewGroup parent) {
        View result = super.apply(context, parent);

        //your code
        int[] colors = new int[]{colorDark,colorLight}
        GradientDrawable gd = new GradientDrawable(TOP_BOTTOM, colors);
        result.setBackgroundDrawable(gd);
        //end of your code

        return result;
    }
}
like image 176
Sherif elKhatib Avatar answered Nov 19 '22 07:11

Sherif elKhatib