Question

I have a little program(Android APP), When I change the ImageButton's image with a Dialog method, Is there any way to save these changes/settings even after closing my application, I tried to use SharedPreferences, but I do not understand how to do, exist any solution to save my setting?? Thanks!

I post my code:

 import android.widget.Button;
 import android.widget.ImageButton;
 import android.widget.ImageView;
 import android.widget.TextView;
 import android.app.Activity;
 import android.app.Dialog;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;


public class MainActivity extends Activity {

private ImageButton buttonClick;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttonClick = (ImageButton) findViewById(R.id.imageButton1);
    buttonClick.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {


            final Dialog dialog = new Dialog(MainActivity.this);

            dialog.setContentView(R.layout.dialog); 

            dialog.setTitle("ChangeIcon");


            TextView text = (TextView) dialog.findViewById(R.id.textDialog);
            text.setText("Choose the element concerned ");


           ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
           image.setImageResource(R.drawable.default);

            dialog.show();

            Button declineButton = (Button) dialog.findViewById(R.id.buttonGas);

            declineButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    buttonClick.setImageResource(R.drawable.first_possible_choice);
                   dialog.dismiss(); 
                    } });

            Button secondo = (Button)dialog.findViewById(R.id.buttonFinestra);
            secondo.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v){buttonClick.setImageResource(R.drawable.second_possible choice);
                    dialog.dismiss();
                }

            });
        }
    });
} }
Was it helpful?

Solution

Use SharedPreferences to persist changes even after your App is closed.
Save resource image path to a key in SharedPrefernces. Use it at the image load time(when onCreate() gets called).

SharedPreferences sharedPreferences = getSharedPreferences("Name", <Mode>);
int imageResource = sharedPreferences.getInt("KEY_NAME", <default value>);
image.setImageResource(imageResource);

On Button click, save the changes in SharedPreferences:

SharedPreferences.Editor spEditor = sharedPreferences.edit();
spEditor.putInt("KEY_NAME", <image resource>).commit();

OTHER TIPS

You should use SharedPreferences to do that. http://developer.android.com/guide/topics/data/data-storage.html

In your onCreate set last choosed image

private static final String PREFS_LAST_IMG = "prefs_last_img";
private SharedPreferences mPreferences; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPreferences = getPreferences();
    buttonClick.setImageResource(mPreferences.getInt(PREFS_LAST_IMG, R.drawable.first_possible_choice));  // 2nd argument is default option, when nothing was selected before
}

Then when user selects image, just save it:

SharedPreferences.Editor editor = mPreferences.edit();
editor.putInt(PREFS_LAST_IMG, R.drawable.what_user_choose);
editor.commit();

As you said, the easiest way to go is through SharedPreferences. Don't expect this will be a "forever data", though, users may clean any data related to your app (e.g., from the Android App Management itself) and you have to be prepared for such circumstance.

In this example you'll see how to store a value within your SharedPreferences:

SharedPreferences stordata = getSharedPreferences("a-string-that-identifies-your-app", Context.MODE_PRIVATE);
// Here you say you're going to edit the preferences
final SharedPreferences.Editor prefs = stordata.edit();

prefs.putString("my_planet", "Saturn");

// This line is important: it will store the changes
prefs.commit();

Then, once you exit your app and want to recover the saved preferences, simply call:

SharedPreferences stordata = getSharedPreferences("a-string-that-identifies-your-app", Context.MODE_PRIVATE);
final String storedPlanet = stordata.getString("my_planet", "");

---- EDIT ----

Here's a nice example on how to handle SharedPreferences, and here you'll find the Android's reference to see what are you capable to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top