简体   繁体   中英

How to assign a drawable to a widgets button

hoping someone can help. Not sure if this is even possible but here goes.

I have an Android widget which consists of a single button. I want to programmatically set a drawable as the background. For example in a standard activity I might do...

Button mybutton = (Button) findViewById(R.id.abutton);
GradientDrawable gd = new GradientDrawable();
gd.setCornerRadius(45);
gd.setColor(Color.parseColor("#FFFFFF));
mybutton.setBackground(gd);

The reason I am trying to do this is to programmatically change the colour of the widget button to a colour the user picks but also maintain the rounded corners of the button as defined in the drawable above.

I've worked out I can change the colour of the widget button like so.

RemoteViews views;
views = new RemoteViews(context.getPackageName(), R.layout.mylayout);
views.setInt(R.id.widgetbutton,"setBackgroundColor", Color.parseColor(widgethexcolor));

But this resets the button to its default rectangular shape without the rounded corners so I would prefer to make it a drawable.

Any ideas?

Thanks all!

Nat

You could change the color by calling Drawable.setColorFilter(int color, PorterDuff.Mode mode) or Drawable.setColorFilter(ColorFilter colorFilter) . I think this approach will prevent you from losing the rounded corners.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM