Changing background of a button in widget: Android - android

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)

Related

Change background color of remoteview dynamically

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.

Changing app widget background color programmatically

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

Changing the text color in a widget using "R.color.red"

I'm trying to change the text color in my widget depending on an if. So I have my remoteViews sorted and I can change the actual text with no problem but when I try and change the color using R.color.red it just shows up as black.
RemoteViews remoteViewSmall = new RemoteViews(this.getPackageName(), R.layout.smallwidgetlayout);
remoteViewSmall.setTextColor(R.id.widgetdatasmall, R.color.red);
Is this an incorrect way of retrieving the color "red" I have set in my colors.xml?
You can use the Color.RED. Would that be what you are looking for?
Or are you trying to re-define the colors?
Try this if that is the case:
remoteViewSmall.setTextColor(R.id.widgetdatasmall, getResources().getColor(R.color.red));
It might be because of layout re use.
Set the value on every possible condition. That means set the value to red if you have to and otherwise set it to black.

Android: Create a ColorDrawable object without using xml

Is it possible to create a ColorDrawable object without using xml? I would like to be able to change the backgroundColor of a view programmatically, using setBackgroundColor() or setBackgroundDrawable() or setBackgroundResource(), but I want to be able to specify the RGB values in code, not XML. Is this possible?
I know you can get a View as a Drawable and apply a color filter to it (useful for coloring in Button views) by doing the following:
Drawable d_delete = findViewById(R.id.btn_delete).getBackground();
PorterDuffColorFilter filter_red = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
d_delete.setColorFilter(filter_red);
I know the questions it's a bit old but I've got an easy one. Maybe someone arrive here looking for the answer
View view = findViewById(R.id.view_with_colored_background);
String rgbColor = "#CCFFCC";
view.setBackgroundColor(Color.parseColor(rgbColor));

How to update ImageButton BackgroundColor in RemoteViews

I have an appwidget which has got a ImageButton.I can update ImageButton image but i can't update backgroundcolor with using setInt() method;because imagebutton setBackgroundColor is not annoated with RemotableViewMethod.class.What can i do else?
http://developer.android.com/reference/android/widget/RemoteViews.html#setInt%28int,%20java.lang.String,%20int%29
Example:
rv.setInt(R.id.view_id, "setBackgroundColor", 0xFF0000FF);
You can try using a different layout in your RemoteViews constructor that has the right background color. You create a RemoteViews object on every update, and you tell that RemoteViews object what layout to use. From my testing, if you inflate something different than before, that will replace what the app widget presently uses. The various RemoteViews setters are for things that you cannot readily handle via layouts (e.g., dynamic text for a TextView).
You can use ImageView instead of layout background, we can set ImageView src to change background (may be you will use android:scaleType="fitXY")

Categories

Resources