Reed
Reed

Reputation: 14984

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)

Upvotes: 7

Views: 8456

Answers (3)

thomasd
thomasd

Reputation: 1022

Haven't done RemoteViews in a while:

Can you add a custom view that has a method that takes a single string. Then you could call this method using RemoteViews.setString and this method could parse the numbers from the string and apply it as a background.

Upvotes: 0

Raj Kumar
Raj Kumar

Reputation: 430

use following class 

import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.view.View;

public class UtilForGradientBackground {

   public static void gradientBgCreatorFromHex(View view, String bgColorHex, String gradColorHex) {

      ColorDefinitionResult bgColor = getArgbFromHexaString(bgColorHex);

      ColorDefinitionResult gradientColor = getArgbFromHexaString("1b6da7");
      CreateGradientBackground(view, bgColor, gradientColor);

   }

   public static void CreateGradientBackground(View view, ColorDefinitionResult bgColor, ColorDefinitionResult gradientColor) {

      int argbBgColor = Color.argb((int) bgColor.Alpha, bgColor.Red, bgColor.Green, bgColor.Blue);
      int argbGradient = Color.argb((int) gradientColor.Alpha, gradientColor.Red, gradientColor.Green, gradientColor.Blue);

      final Shader upperShader = new LinearGradient(0, 0, 0, 40, argbBgColor, argbGradient, Shader.TileMode.CLAMP);

      float[] roundedCorner = new float[] { 0, 0, 0, 0, 0, 0, 0, 0 };

      ShapeDrawable normal = new ShapeDrawable(new RoundRectShape(roundedCorner, null, null));
      normal.getPaint().setShader(upperShader);

      normal.setPadding(7, 3, 7, 0);
      StateListDrawable stateList = new StateListDrawable();
      stateList.addState(new int[] {}, normal);
      view.setBackgroundDrawable(stateList);
   }

   public static ColorDefinitionResult getArgbFromHexaString(String hexColorString) {
      ColorDefinitionResult colorDefinitionResult = new ColorDefinitionResult();
      if (hexColorString.length() == 6) {
         String redHex = hexColorString.substring(0, 2);
         String greenHex = hexColorString.substring(2, 4);
         String blueHex = hexColorString.substring(4, 6);
         colorDefinitionResult.Red = Integer.parseInt(redHex, 16);
         colorDefinitionResult.Green = Integer.parseInt(greenHex, 16);
         colorDefinitionResult.Blue = Integer.parseInt(blueHex, 16);
         colorDefinitionResult.Alpha = 255;

      }
      return colorDefinitionResult;
   }
}


and use it as follow:
give it your View id and RGB values as arguments
      View findViewById = findViewById(R.id.your_view_id);
      UtilForGradientBackground.gradientBgCreatorFromHex(findViewById, "2E64FE", "819FF7");

Upvotes: 0

Sherif elKhatib
Sherif elKhatib

Reputation: 45942

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;
    }
}

Upvotes: 8

Related Questions