Search code examples
androidconfigurationwidgettransferinfo

Transfer a string from widget configuration screen to the AppWidget itself?


I am creating a Weather Widget.The first time I drag the widget to the home screen a configuration screen should appear with three buttons representing three cities. When the user clicks on a button (say Copenhagen) I want the string "Copenhagen"" to be transfered to the Widget itself, so It can go and fetech weather data of the Copenhagen and show it to the user. How do I actually acomplish that?

AppWidget Configuration screen

Configuration screen

Widget Screen

Widget screen


Solution

  • I will explain the solution which uses SharedPreferences.

    1. You do need two java files. An App Widget Configuration Activity (I've called my Configure), and an AppWidgetProvider (I've called my WeatherWidget).

    2. You will need two xml files, defining the layouts for the Configuration activity and the widget (I've called mine activity_configure.xml and weather_widget.xml) .

    3. You will need a manifest file and also a widget info file (Ive called mine weather_widget_info

    enter image description here

    Here is the relevant code from the Configure

    public class Configure extends AppCompatActivity {
        private int widgetID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_configure);
    
        // if the user hits back button
        setResult(RESULT_CANCELED);
    
        // get widgetID for this widget instance
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            widgetID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        }
    
        Button copenhagenButton = (Button) findViewById(R.id.copenhagenButton);
        copenhagenButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveData("Copenhagen");
                resultOK();
            }
        });
    
        Button stockholmButton = (Button) findViewById(R.id.stockholmButton);
        stockholmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveData("Stockholm");
                resultOK();
            }
        });
    
        Button osloButton = (Button) findViewById(R.id.osloButton);
        osloButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveData("Oslo");
                resultOK();
            }
        });
    
    }
    
    private void saveData(String cityName) {
        String widgetIDString = Integer.toString(widgetID);
        SharedPreferences prefs = this.getSharedPreferences(widgetIDString, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = prefs.edit();
        edit.putString("city", cityName);
        edit.commit();
    }
    
    private void resultOK() {
    
        // send an onUpdate() message to the widget via a broadcast
        Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, WeatherWidget.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{widgetID});
        sendBroadcast(intent);
    
        // signal OK
        Intent resultValue = new Intent();
        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
        setResult(RESULT_OK, resultValue);
        finish();
    }
    

    }

    Here is the relevant code from the WeatherWidget

    public class WeatherWidget extends AppWidgetProvider {
    final static String TAG = "stille";
    private int widgetID;
    private String city="N/A";
    RemoteViews views;
    
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        final int N = appWidgetIds.length;
        for (int i = 0; i < N; i++) {
            widgetID = appWidgetIds[i];
            loadCity(context);
            views = new RemoteViews(context.getPackageName(), R.layout.weather_widget);
            views.setTextViewText(R.id.location, city);
            appWidgetManager.updateAppWidget(widgetID, views);
        }
    }
    
    private void loadCity(Context context) {
        String widgetIDString = Integer.toString(widgetID);
        SharedPreferences prefs = context.getSharedPreferences(widgetIDString, Context.MODE_PRIVATE);
        String cityName = prefs.getString("city", "N/A ");
        city = cityName;
    }
    

    }