1

So far I have:

public class Info extends Activity {

    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);

        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);
                    appWidgetManager.updateAppWidget(mAppWidgetId, views);

            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
            setResult(RESULT_OK, resultValue);
            finish();
        }
    };

}

Based on this I would like the save button to be the default image and to make another button that would change android:dial="@drawable/widgetdial" to android:dial="@drawable/widgetdial2". Is there any way of doing this?

Geobits
  • 22,218
  • 6
  • 59
  • 103

1 Answers1

0

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.

Community
  • 1
  • 1
MarchingHome
  • 1,184
  • 9
  • 15
  • Almost perfect. The only thing is after setting R.id.viewid to R.drawable.widgetdial the only thing that doesnt work is the switching of the images. Its as if its happening but not visible – Adrian Orta Oct 23 '12 at 21:07
  • I see why yours works but im using android's AnalogClock. How would i make it so that android:dial="@drawable/widgetdial" is the object changed? – Adrian Orta Oct 24 '12 at 00:11
  • Ah, yes. I think I have it. This guy found the answer: [SO question](http://stackoverflow.com/questions/9398362/set-drawable-dynamically-for-analogclock-widget) – MarchingHome Oct 24 '12 at 04:52
  • I have everything working except that the image doesnt switch after placing the widget. As in, i can set it up the first time but once its on the screen and i try to configure it again it wont switch the image. i still have: views.setImageViewResource(R.id.viewid, drawableSrc); Also when the image is switched to widgetdial2 and placed on the homescreen i am unable to bring up the configuration page when clicking it. – Adrian Orta Oct 24 '12 at 12:57
  • Pff, I'm sorry. I am out of ideas here. Good luck anyway! – MarchingHome Oct 24 '12 at 12:59