I have a widget with a config activity, where the user can select a color for the background of the widget from a color picker. I am using the method below where I have an ImageView and create a Bitmap which I dynamically set on the ImageView.
http://konsentia.com/2011/03/dynamically-changing-the-background-color-in-android-widgets/
public static Bitmap getBackground (int bgcolor)
{
try
{
Bitmap.Config config = Bitmap.Config.ARGB_8888; // Bitmap.Config.ARGB_8888 Bitmap.Config.ARGB_4444 to be used as these two config constant supports transparency
Bitmap bitmap = Bitmap.createBitmap(2, 2, config); // Create a Bitmap
Canvas canvas = new Canvas(bitmap); // Load the Bitmap to the Canvas
canvas.drawColor(bgcolor); //Set the color
return bitmap;
}
catch (Exception e)
{
return null;
}
}
Then
remoteViews.setImageViewBitmap(R.id.bgcolor, getBackground(bgcolor));
What I want to do though is let the user also choose if they want rounded corners on the widget. Is it possible to dynamically change both the color and whether the widget has rounded corners? From the examples I have looked at for rounding corners it seems you need to know the dimensions of the View so that you can round the edges before setting the Bitmap. I don't think this is possible from within a widget though... any ideas?
There are 2 methods of doing this:
Create some white 9 patch bitmap resources with rounded corners and square corners and use RemoteViews.setInt to change the color/transparency of the widget background ImageView (requires Froyo or greater), e.g.
if(roundCorners)
remoteViews.setImageViewResource(R.id.widget_background, R.drawable.round_corners);
else
remoteViews.setImageViewResource(R.id.widget_background, R.drawable.square_corners);
remoteViews.setInt(R.id.widget_background, "setColorFilter", someColor);
remoteViews.setInt(R.id.widget_background, "setAlpha", someAlphaLevel);
I've used both methods and recommend (2) for maximum compatibility.
Too late for the answer, but maybe it will be useful to someone. Recently I also figured out how to make rounded corners in RemoteViews.
In my case, I needed to display the image by url and make it round cornenrs. Unfortunately, remoteViews.setImageViewResource
and remoteViews.setInt
didn't work right.
I solved the problem with Glide:
val notificationTarget = NotificationTarget(
baseContext,
R.id.id_of_your_image_view,
expandedView,
notification,
NOTIFICATION_ID
)
Glide.with(baseContext.applicationContext)
.asBitmap()
.load("url of the image")
.circleCrop() // the method that rounds the corners
.into(notificationTarget)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With