Java – Add how many appWidgets with different configurations?

Add how many appWidgets with different configurations?… here is a solution to the problem.

Add how many appWidgets with different configurations?

I created a widget that displays a simple TextView that can be edited as Edittext in the configuration activity. I save the entered text using the sharing preferences, so the user can click on the widget to edit the text, and the text already entered appears in the Edit Text field.
My question is this. I want the user to be able to add multiple widgets, but when adding a second widget, the same text will be loaded from the sharing preferences as in the other widget. And, when one widget is edited, so is the other. Hope I made it clear. I kind of know it’s related to appWidgetIds, but I can’t figure it out.

Here is my code, a bit simplified.

public class WidgetConfig extends Activity implements OnClickListener, OnItemSelectedListener {

AppWidgetManager awm;
    int awID;
    Context c;
    EditText info;
    Button b;
    String note;
    int styleStart = -1, cursorLoc = 0;
    SharedPreferences sp;
    Spinner spinner;
    String[] paths = { "10", "20", "30" };
    File path = null;

@Override
    protected void onCreate(Bundle savedInstanceState) {
         TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.widgetconfig);

c = WidgetConfig.this;
        info = (EditText)findViewById(R.id.etwidgetconfig);

...

b = (Button)findViewById(R.id.bwidgetconfig);
        loadPrefs();
        b.setOnClickListener(this);

Getting Info about the widget that launched this activity
        Intent i = getIntent();
        Bundle extras = i.getExtras();
        if (extras != null){
            awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID );
        }

awm = AppWidgetManager.getInstance(c);

}

...

private void loadPrefs(){
        sp = PreferenceManager.getDefaultSharedPreferences(this);
        note = sp.getString("NOTE", "DEFAULT");

info.setText(note);

}

private void savePrefs(String key, String value){
        sp = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = sp.edit();        
        editor.putString(key, value);  value to store
        editor.commit();   

}

public void onClick(View v) {
         TODO Auto-generated method stub

savePrefs("NOTE", info.getText().toString());

RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget);
        views.setTextViewText(R.id.tvConfigInput, info.getText());

ComponentName thisWidget = new ComponentName(this, Widget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, views);

Intent in = new Intent(c, WidgetConfig.class);
        PendingIntent pi = PendingIntent.getActivity(c, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);

views.setOnClickPendingIntent(R.id.B_EditAgain, pi);

awm.updateAppWidget(awID, views);

Intent result = new Intent();
        result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
        setResult(RESULT_OK, result);
        finish();

}

}

Update:

So I tried this but it was no different and still didn’t work.

    private void loadPrefs(){
        sp = context.getSharedPreferences("widget" + String.valueOf(appWidgetId)
          , Context.MODE_PRIVATE);
        note = sp.getString("Note", "");

info.setText(note);

}

private void savePrefs(String key, String value){
    sp = context.getSharedPreferences("widget" + String.valueOf(appWidgetId)
              , Context.MODE_PRIVATE);
    Editor editor = sp.edit();
    editor.clear();
    editor.putString("Note", info.getText().toString());
    editor.putString(key, value);  value to store
    editor.commit();   

}

Solution

You probably don’t want to use getDefaultSharedPreferences here. All widgets share the same default sharing preferences, so they will constantly override each other.

I

ran into the same situation in my own application, so I used a custom preference file for each widget. You can name your preferences file with a widgetID, and then each widget will always get its own unique set of preferences.

In the configuration PreferenceActivity:

this.getPreferenceManager().setSharedPreferencesName(
           "widget" + String.valueOf(mAppWidgetId)
);

This stores all PreferenceActivity settings in a preferences file named after the widget ID.

Then in the widget itself, simply retrieve the file corresponding to its id:

preferences = context.getSharedPreferences(
      "widget" + String.valueOf(appWidgetId)
      , Context.MODE_PRIVATE);

And retrieve preferences as usual.

Related Problems and Solutions