开发者

Adding Custom Background for Widget Based on User Input

开发者 https://www.devze.com 2023-01-12 02:28 出处:网络
I have a widget that displays a set of information. What I would like to do is to give the user the opportunity to ch开发者_如何学编程oose the background color/image. I would like to have a popup when

I have a widget that displays a set of information. What I would like to do is to give the user the opportunity to ch开发者_如何学编程oose the background color/image. I would like to have a popup when the user is selecting the widget to choose the background.

So how would I make the popup?

And how would I apply the background dynamically?

Thank you for any help you can give!!


I think it might would be better if you open a PreferenceActivity by tapping the widget. Then choose the appropriate picture and get the appropriate prefs by using:

String background = prefs.getString("background", "bgpink"); // get the Prefs!
RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.widget);

if (background.equals("bgpink")) {
    views.setImageViewResource(R.id.widgetLayout, R.drawable.bgpink); // set the background
}

The setBackground code does not work. I read the documentation of "setImageViewResource" and this is an open issue i think. Let me know if you found a solution for setting the background.


I haven't done this specifically before but I would go about it in the following manner:

  • If you are looking to pick up files that are already on the system, fire an intent to pick (ACTION_PICK) and supply the right Intent.data element
  • If you are supplying the options:
    • Create a Dialog with a list view (of image view / text view (with compound drawables and some text)) to select the background
    • Store the value returned into a preference file
    • Read from preference and apply Background

Hope this helps


What I ended up using:

    public static void setWidgetBackground(Context context, AppWidgetManager appWidgetManager, int mAppWidgetId, int newBackground){
            // theLayout is a global int in my case

            switch (newBackground){
            case R.id.brown:
                theLayout = R.layout.brown_widget;
                break;
        case R.id.darkblue:
            theLayout = R.layout.darkblue_widget;
                break;
            case R.id.darkpurple:
                theLayout = R.layout.darkpurple_widget;
                break;
            // ... etc.
            }


            // save the settings so we can load and apply again next time the widget is updated
            WidgetConfigure.saveLayout(context,mAppWidgetId,theLayout);

            // call the actual update so that the right background is applied
            this.updateAppWidget(context, appWidgetManager, mAppWidgetId, theLayout);

    }

Hope this is helpful to others.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号