Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8710729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:41:54+00:00 2026-06-13T04:41:54+00:00

I’ve created a widget which displays a simple textview, which is editable as an

  • 0

I’ve created a widget which displays a simple textview, which is editable as an Edittext in a configuration activity. I save the inputted text with shared preferences, so the user can tap the widget to edit the text, and the already inputted text appears in the edittextfield.
My problem is this. I would like the user to be able to add multiple widgets, but when a seccond widget is added, the same text as in the other widget is loaded from shared preferences. And, when on widget is edited so is the other one. Hope i’m being clear. I kinda have an idea it has something to do with appWidgetIds but i cannot figure it out.

Here’s 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 nothings different, still doesn’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();   

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:41:56+00:00Added an answer on June 13, 2026 at 4:41 am

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

    I had the same situation in my own app, so I used a custom preference file for each widget. You can name the preference file with the widgetID, and then each widget will always get it’s own unique set of preferences.

    In the configuration PreferenceActivity:

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

    This will store all of the PreferenceActivity settings into a preference file named after the widget id.

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

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

    And retrieve preferences like normal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a text area in my form which accepts all possible characters from
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.