7

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)

Community
  • 1
  • 1
Reed
  • 14,703
  • 8
  • 66
  • 110
  • View.setBackgroundDrawable(Drawable) were deprecated in API16. The method is now [View.setBackground(Drawable)](http://developer.android.com/reference/android/view/View.html#setBackground(android.graphics.drawable.Drawable)). Doesn't this work? – MartinHaTh Jan 25 '14 at 17:05
  • 1
    It's a widget, so I have to set it via [RemoteViews](http://developer.android.com/reference/android/widget/RemoteViews.html) which does not offer that method. I put that line just to give a clear example of what I wanted. – Reed Jan 25 '14 at 17:26

3 Answers3

8

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;
    }
}
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • I saw that method, but wasn't particularly comfortable using it since the docs state `Caller beware: this may throw` – Reed Jan 31 '14 at 21:33
  • I haven't had a chance to test or play around with this yet, but it seems like the most promising approach. – Reed Feb 01 '14 at 15:56
  • I'm curious of **what** may throw using that method... It seems there are missing words in that phrase from the beginning (I remember seen the RemoteViews doc for the first time 3 years ago) – Jose_GD May 08 '14 at 14:49
0
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");
Raj Kumar
  • 402
  • 4
  • 9
  • I appreciate the help, but this would not work for a widget. I already have working code to create a gradient. That part was fairly simple. The issue is that I can't pass a drawable to `RemoteViews`. And I cannot call `view.setBackgroundDrawable(...)`, because it is only accessible via `RemoteViews`. I can only pass a bitmap to the widget, and I can't find a way to set the bitmap with a proper width/height to match the widget's size. – Reed Jan 31 '14 at 16:55
  • ColorDefinitionResult not found – Sharath kumar Apr 03 '18 at 10:08
0

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.

thomasd
  • 1,002
  • 9
  • 14