Change background color of remoteview dynamically - android

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.

Related

How to clear color for TopDrawable of Textview?

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);

Changing background of a button in widget: 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)

Call SetAlpha in widget application

I have a ImageView as background of appWidgets layout. So I want to change both background resource (nine patch image) and background transparency. I tried as below:
rv.setInt(R.id.widget_background, "setBackgroundColor", 0);
rv.setInt(R.id.widget_background, "setBackgroundResource", nBackgroundId); // set background_id
rv.setInt(R.id.widget_background, "setAlpha", nTransparency); // set transparent
But nothing is happened, could anybody tell me the solution to solve my problem?
Which version is your android sdk? It requires Froyo or greater

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

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