开发者

Android set widget background

开发者 https://www.devze.com 2023-02-08 15:52 出处:网络
In my drawable-hdpi folder i have 4 image files (.png) to serve as the background of awidget. By default android:background=\"@drawable/goldgreenbg\" is set for the LinearLayout. I created a preferenc

In my drawable-hdpi folder i have 4 image files (.png) to serve as the background of awidget. By default android:background="@drawable/goldgreenbg" is set for the LinearLayout. I created a preferences screen to let the user change the background. How to do that? I would like to use this code for it:

      if (listpref.equals("color1"))
          {
          Toast.makeText(EditPreferences.this,  "Black" + listpref, Toast.LENGTH_LONG).show();

          }

else if (listpref.equals("color2"))
          {
              Toast.m开发者_JS百科akeText(EditPreferences.this,  "Brown" + listpref, Toast.LENGTH_LONG).show();
          }

Update: Where shall i put this code to? MainActivity.java: for the activity UpdateService.java: for the widget EditPreferences.java: for the preferences Main.xml includes the listview and widgetlayout is id of it.

  setContentView(R.layout.main);
  preferences = PreferenceManager.getDefaultSharedPreferences(this);
  String listpref = preferences.getString("listPref", "n/a");              
  LinearLayout ll = (LinearLayout) findViewById(R.id.widgetlayout);
  if (listpref.equals("color1"))
  {
      Toast.makeText(MainActivity.this, "Black" + listpref, Toast.LENGTH_LONG).show();
      ll.setBackgroundDrawable(getResources().getDrawable(R.drawable.blackbg));
  }
  else if (listpref.equals("color2"))
  {
      Toast.makeText(MainActivity.this, "Brown" + listpref, Toast.LENGTH_LONG).show();
      ll.setBackgroundDrawable(getResources().getDrawable(R.drawable.brownbg));
  }


Assuming you allready have the LinearLayout on your screen (using setContentView), you can change the background quite easily like so:

yourLinearLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.blackbg));

(and get that layout using findViewById() ofcourse )


I found the solution.

EditPreferences.java:

final Preference listpref = getPreferenceScreen().findPreference("listPref");
        listpref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() 
        {
         public boolean onPreferenceChange(Preference p, Object newValue) 
         {
          String color = (String) newValue;

          if (color.equals("color1"))
          {
              RemoteViews updateViews = new RemoteViews(EditPreferences.this.getPackageName(), R.layout.main);
              updateViews.setTextColor(R.id.widget_textview, Color.rgb(208, 202, 202));
              updateViews.setTextColor(R.id.widget_textview2, Color.WHITE);
              updateViews.setTextColor(R.id.widget_textview3, Color.rgb(176, 175, 175));
             // updateViews.setImageViewBitmap(R.id.ImageView01, ((BitmapDrawable)EditPreferences.this.getResources().getDrawable(R.drawable.forestbg)).getBitmap());
              updateViews.setImageViewResource(R.id.ImageView01, R.drawable.blacktrans);            
              ComponentName thisWidget = new ComponentName(EditPreferences.this, HelloWidget.class);
              AppWidgetManager manager = AppWidgetManager.getInstance(EditPreferences.this);
              manager.updateAppWidget(thisWidget, updateViews);
 }
          else if (color.equals("color2"))
          {
              RemoteViews updateViews = new RemoteViews(EditPreferences.this.getPackageName(), R.layout.main);
              updateViews.setTextColor(R.id.widget_textview, Color.rgb(23, 81, 11));
              updateViews.setTextColor(R.id.widget_textview2, Color.rgb(232, 232, 107));
              updateViews.setTextColor(R.id.widget_textview3, Color.rgb(23, 81, 11));
              updateViews.setImageViewBitmap(R.id.ImageView01, ((BitmapDrawable)EditPreferences.this.getResources().getDrawable(R.drawable.goldgreenbg)).getBitmap());
              // updateViews.setImageViewResource(R.id.ImageView01, R.drawable.goldgreenbgf);           
              ComponentName thisWidget = new ComponentName(EditPreferences.this, HelloWidget.class);
              AppWidgetManager manager = AppWidgetManager.getInstance(EditPreferences.this);
              manager.updateAppWidget(thisWidget, updateViews);
  }
  return true;
         }
        });
public void onStart(Intent intent, int startId) {
          getPrefs();
}
     private void getPrefs() {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
            ListPreference = prefs.getString("listPref", "nr1");
     }

This way it is working perfectly.

0

精彩评论

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