How do I set the background color of the home screen app widget programmatically?
Remember widget is remoteView. You have very limited resource to updates UI of widget and not directly.
You can try :
remoteViews.setInt(viewId, "setBackgroundColor", Color.BLACK);
I never used it but i guess it may be the way.
I guess you need to change color dynamically.
You can change color of ImageView image in "RemoteViews" by doing this:
remoteviews.setInt(viewid, "setColorFilter", color);
Widget.setBackgroundColor(Color.LTGRAY);
Find the following code..
code:
Button button;
//to change background color..
button.setBackgroundColor(Color.Yellow);
use
Yourwidget.setBackgroundColor(Color.RED);
hope help
Related
In my application I change color of Top drawable of Textview using below code.
tvTopDrawable.setColorFilter("#E1BEE7", Mode.SRC_ATOP);
Now I want to clear this color from my drawable.
I have tried tvTopDrawable.clearColorFilter();
But it seems its not working sometime.
I used that same image in other Activity of my Application.
Where to write clearColorFilter() so it clears the color from drawable,Can anyone help with this?
Thanks in Advance.
Try setting color filter to transparent
tvTopDrawable.setColorFilter("#00E1BEE7", Mode.SRC_ATOP);
As of my previous question
here
I successfully managed to do this by transparent image. Now i want to change background color of images dynamically in remoteviews(PS I want to make widget like that and color of images in given link changes dynamically).
I tried following code:
ColorFilter cf = new PorterDuffColorFilter(-15032095, Mode.MULTIPLY);
Drawable d= context.getResources().getDrawable(R.drawable.panel1);
d.mutate();
d.setColorFilter(cf);
rv.setBitmap(R.id.rl_noti_main, "setColorFilter", drawableToBitmap(d));
but it didn't help. How can i achieve this?
I have color codes in all formats integer, HEX or string whatever it will be needed.
Please note that i want to do this only for given shapes in this link and for remoteview.
Thanks in Advance :)
For someone else looking for it
remoteView.setInt(R.id.container, "setBackgroundColor", backgroundColor);
You cannot dynamically update widgets.
You can use the setBitmap() method to change the bitmap of a view inside a RemoteView. If the view is at the background, it should change the background. And then update the widget to make the changes to be applied.
If you are using RemoteViews in Notifications, you should update your notification after this. If you are using it in a Widget, you should use appWidgetManager.updateAppWidget(appWidgetId, views); function.
I need to change background of a button programmatically. That button is placed in a widget.
I got an example of changing background color here, but this is only for changing color, and I want to use an image instead of a color.
Is there any way to apply a background image to a button programmatically?
Thank you
Instead of using setBackgroundColor like in the example you linked you should be able to use setBackgroundResource I think.
In Your AppWidgetProvider class:
private RemoteViews v;
... in onUpdate:
v = new RemoteViews(context.getPackageName(), R.layout.widget);
...
icon = context.getResources().getIdentifier("ic_action_refresh_"+choosenTheme, "drawable", context.getPackageName());
v.setInt(R.id.wi_bu_refresh, "setBackgroundResource", icon);
Where:
icon is just resource id (found by getIdentifier method dynamically for every theme).
R.layout.widget is widget xml layout.
R.id.wi_bu_refresh is a button in widget layout, which background You want to change.
Hope it helps, cheers.
You can use this:
setInt(YouButtonId, "setBackgroundResource", YourImageResourceID);
use setBackgroundResource instead of setBackgroundColor .
I think you need to use public void setBackgroundResource (int resid)
I read a couple of posts but none of them had the working solution.
Once you do
button.setBackgroundColor(0x00000000);
How do you revert the button's background color back to default color?
use:
btn.setBackgroundResource(android.R.drawable.btn_default);
If the background color was set using
btn.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
it can be reset using:
btn.getBackground().clearColorFilter();
In contrast to button.setBackgroundColor() setting the color this way preserves the button's shape.
Nobody mentioned TRANSPARENT
use it like this
findViewById(R.id.button_id).setBackgroundColor(Color.TRANSPARENT);
Thank me later
this worked better for me :
Button defbtn=new Button(this);
btn.setBackground(defbtn.getBackground());
how to use color state list for background?
I know android:background="#drawable/drawable_selector", but android:background="#color/color_selector" will cause exceptions.
but android:background="#FFFFFF" works again, can anyone explains why?
now i want to change a layout's background color(not a drawable) when it's pressed,
how to do it?
dynamically you can change like this. Use this if it is useful for you -
textView.setBackgroundColor(Color.parseColor(getResources().getString(R.string.red)));
put the color in res/values/colors.xml,
like #FFFFFF,
and then create a drawable xml in drawable directory,
,that is ok.