OK, so if I understand correctly you want to have two buttons in your widget configuration activity (Info
). One button will switch an image in the widget itself (between R.drawable.widgetdial
and R.drawable.widgetdial2
). The other button will save the choice and exit the Info
activity. Correct?
Ofcourse, you will need to add a button in the layout for your Info
activity. I've referenced this below as R.id.change_button
as an example.
I have tested this and it works. I have added some code to your code and have marked new pieces of code with comments:
public class Info extends Activity {
//next three lines is new code
private int drawableSrc;
private TextView statusText;
private Button changeButton;
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
public Info() {
super();
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setResult(RESULT_CANCELED);
setContentView(R.layout.widget_activity_info);
findViewById(R.id.save_button).setOnClickListener(mOnClickListener);
//next six lines is new code
statusText = (TextView)findViewById(R.id.text);
statusText.setText("Now showing widgetdial image");
changeButton = (Button)findViewById(R.id.change_button);
changeButton.setText("Change to widgetdial2 image");
drawableSrc = R.drawable.widgetdial;
changeButton.setOnClickListener(changeOnClickListener);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
}
View.OnClickListener mOnClickListener = new View.OnClickListener() {
public void onClick(View v) {
final Context context = Info.this;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_activity);
//next line is new code, needs to come before updateWidgetApp
views.setImageViewResource(R.id.viewid, drawableSrc);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
};
//everything below this is new code
View.OnClickListener changeOnClickListener = new View.OnClickListener() {
public void onClick(View v) {
if(drawableSrc == R.drawable.widgetdial) {
drawableSrc = R.drawable.widgetdial2;
statusText.setText("Now showing widgetdial2 image");
changeButton.setText("Change to widgetdial image");
}
else {
drawableSrc = R.drawable.widgetdial;
statusText.setText("Now showing widgetdial image");
changeButton.setText("Change to widgetdial2 image");
}
}
};
}
I've referenced the thing you want to change as R.id.viewid
, but this is ofcourse just an example ID reference.
Depending on what you want to change exactly (widget background or widget imageview?), you can have a look here: Other SO question
Here is the example layout I used for the Info
activity (the file that you referenced as R.id.widget_activity_info
):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/change_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/text" />
<Button
android:id="@+id/save_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Save"
android:layout_below="@id/change_button" />
</RelativeLayout>
If it still doesn't work for you somehow, please check out my example I have uploaded for you.
The zip file contains the workspace you can import into eclipse. You can run this in the emulator.
I hope this helps you.