Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change widget layout background programmatically in Android

Take this widget layout for example (part of my whole widget layout)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/widget_background_dark_4x2"
android:orientation="horizontal"
android:id="@+id/widget_main"
>

I want to be able to change the background drawable used based on a users selection. For example using remote views I can update the colour of a textview by doing something like this:

remoteView.setTextColor(R.id.text_view1, Color.WHITE);

However I have been finding it difficult to the same for the background of my linear layout. I did try this:

remoteView.setBitmap(R.id.widget_main, "setBackgroundDrawable", ((BitmapDrawable) context.getResources().getDrawable(R.drawable.widget_background_dark_4x2)).getBitmap());

But I get the following error:

06-01 22:46:36.305: WARN/AppWidgetHostView(244): android.widget.RemoteViews$ActionException: view: android.widget.LinearLayout doesn't have method: setBackgroundDrawable(android.graphics.Bitmap)

<< EDIT >> Have also tried this:

Bitmap bitmap = ((BitmapDrawable)context.getResources().getDrawable(R.drawable.widget_background_light_4x2)).getBitmap();
remoteView.setBitmap(R.id.widget_main, "setBackgroundDrawable",bitmap );

But unfortunately get the following error:

06-01 23:11:26.039: WARN/AppWidgetHostView(244): updateAppWidget couldn't find any view, using error view 06-01 23:11:26.039: WARN/AppWidgetHostView(244): android.widget.RemoteViews$ActionException: view: android.widget.LinearLayout doesn't have method: setBackgroundDrawable(android.graphics.Bitmap)

like image 406
Pelly Avatar asked Jun 01 '11 12:06

Pelly


People also ask

How to set background image to layout in Android programmatically?

To set Background:RelativeLayout layout = (RelativeLayout) findViewById(R. id. background); layout. setBackgroundResource(R.

How to set button background in Android programmatically?

setBackgroundResource() method is used to change the button background programmatically. setBackgroundResource(int id) accepts id of drawable resource and applies the background to the button.

How can change relative layout background color in android programmatically?

Programmatically using Java code. java file, then you can do it by using setBackgroundColor() method on your layout. To your Parent View Layout add an attribute @id/id_name and map it to a variable in your java file.


2 Answers

Did you try:

remoteView.setInt(R.id.widget_main, "setBackgroundResource", R.drawable.widget_background_dark_4x2);
like image 172
foofaa Avatar answered Oct 11 '22 14:10

foofaa


Ok, well the only solution that I could end up coming up with for this was to change the way I was displaying the background image in the LinearLayout. As you can see above I was using the android:background tag to display the image. What I ended up doing was that I removed this and then encased my entire widget layout (which was inside a linearlayout inside a relativelayout. I then just had an ImageView decalred first within the RelativeLayout and then had the rest of the widgets linearlayout sit on top of it. Then within my code I just set the imageview source via the remote view and it worked like a treat.

Not the most elegant/efficient way of coding it, but it worked!

like image 43
Pelly Avatar answered Oct 11 '22 13:10

Pelly