简体   繁体   中英

Android listpreference and widget background

I created a widget (HelloWidget.java), an activity for it (MainActivity.java) and a listpreference (EditPreferences.java).

XML files:

  • Main.xml: this has the widget
  • Config.xml: this has the activity: buttons
  • preferences.xml: this has the listpreference

I created the preferences to let the user change the background image of the widget. I have 4 image files for this in the drawable-hdpi folder. Default background is set like android:background="@drawable/goldgreenbg"

In MainActivity.java i have this code to set the background image if user clicks the first or the second element of the listpreference:

preferences = PreferenceManager.getDefaultSharedPreferences(this);
  String listpref = preferences.getString("listPref", "n/a");              

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

Unfortunately this results in changing the activity, not the widget. So now i see the background image instead of the buttons in the activity while the widget is unchanged. I tried to put this in the onCreate() method of UpdateService.java but it doesn't enable setContentView() to use. Any ideas?

Update: main.xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/goldgreenbg"
    android:id="@+id/widgetlayout">
<TextView android:id="@+id/widget_textview"
    android:text="@string/widget_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:gravity="center_horizontal"
    android:layout_marginTop="0dip"
    android:padding="0dip"
    android:textColor="#0B3B0B"
    android:textSize="11sp"/>

<TextView android:id="@+id/widget_textview2"
    android:text="@string/widget_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:gravity="center_horizontal"
    android:layout_marginTop="0dip"
    android:padding="0dip"
    android:textSize="12sp"
    android:textColor="@android:color/white"/>
<TextView android:id="@+id/widget_textview3"
    android:text="@string/widget_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:layout_marginTop="0dip"
    android:padding="0dip"
    android:textSize="9sp"
    android:textColor="#0B3B0B"/>
</LinearLayout>

Solved: The "If" part should be in the preferences.java file and insted of linearlayout use this code:

 RemoteViews updateViews = new RemoteViews(EditPreferences.this.getPackageName(), R.layout.main);
              updateViews.setTextColor(R.id.widget_textview, Color.rgb(215, 215, 215));
              updateViews.setTextColor(R.id.widget_textview2, Color.WHITE);
              updateViews.setTextColor(R.id.widget_textview3, Color.rgb(155, 155, 155));
              updateViews.setImageViewBitmap(R.id.ImageView01, ((BitmapDrawable)EditPreferences.this.getResources().getDrawable(R.drawable.blackbg)).getBitmap());
              ComponentName thisWidget = new ComponentName(EditPreferences.this, HelloWidget.class);
              AppWidgetManager manager = AppWidgetManager.getInstance(EditPreferences.this);
              manager.updateAppWidget(thisWidget, updateViews);

Your problem is right here:

LinearLayout ll = (LinearLayout) findViewById(R.id.widgetlayout);
ll.setBackgroundDrawable(getResources().getDrawable(R.drawable.blackbg));

Your setting the whole layout (or at least whatever is inside LinearLayout). You should be using the widget instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM