android studio save last selected background image

Viewed 293

I created a simple code to change the image background on button click, but the problem is after changing the background image and closing the app and open it again the background image return to the default one, and I want to save the changed image background and display it for the next launch of my app.

here is my code:

 button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            int bg = 0;
            if (x[0] == 5) x[0] = 1;
            if (x[0] == 1) bg = R.drawable.bg1;
            if (x[0] == 2) bg = R.drawable.bg2;
            if (x[0] == 3) bg = R.drawable.bg3;
            if (x[0] == 4) bg = R.drawable.bg4;
            if (x[0] == 5) bg = R.drawable.bg5;

            layout.setBackgroundResource(bg);
            x[0]++;
        }


});
3 Answers

You can use SharedPreference to save and store the id of the Image file.. when you launch your app get this int value from the shared preference in onCreate() of the activity, and set it whenever you change it

SharedPreferences mPreferences
int currentBackground;

onCreate() {

    mPreferences = getSharedPreferences("MySahredPrefs", MODE_PRIVATE);

    int currentBackground = sharedPrefs.getInt("MyBackground", R.drawable.bg1); // <<< getting the background from the shared preference

    layout.setBackgroundResource(currentBackground);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int bg = 0;
            if (x[0] == 5) x[0] = 1;
            if (x[0] == 1) bg = R.drawable.bg1;
            if (x[0] == 2) bg = R.drawable.bg2;
            if (x[0] == 3) bg = R.drawable.bg3;
            if (x[0] == 4) bg = R.drawable.bg4;
            if (x[0] == 5) bg = R.drawable.bg5;

            layout.setBackgroundResource(bg);

            SharedPreferences.Editor preferencesEditor = mPreferences.edit();
            preferencesEditor.putInt("MyBackground", bg); // <<<<<<<< setting the background to the shared preference
            preferencesEditor.apply();

            x[0]++;
        }
    });
}

I recommend that you create an xml or json file to store array of You images and on change rearrange this list the example below shows a list of cars and every time when I change the order of the list in main car it is the first in the list

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="list_sorted_data">[3,1,2]</string>
</map>

Working example You make see in action https://www.youtube.com/watch?v=l7H0YbBvVQ4&t=16s if You don whant see all go to 2 min and 40 sec.

From what I understand you should be able to do this pretty simply with shared preferences.

Then on startup retrieve the int and flip the background

  SharedPreferences preferences;
  SharedPreferences.Editor editor;
        @Override
        protected void onCreate(bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myactivity);

        preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        editor = preferences.edit();

        //I used a separate method to keep clutter out of onCreate but its up to you, doesn't matter. 
        setBackground();
        button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setBackground();
                }

        });
    }
    //Here we just grab the id, and assign the background. 
        public void setBackground(){
             int id = preferences.getInt("backgroundid",1);
             int bg = 0;
             if (id == 1) bg = R.drawable.bg1;
             if (id == 2) bg = R.drawable.bg2;
             if (id == 3) bg = R.drawable.bg3;
             if (id == 4) bg = R.drawable.bg4;
             if (id == 5) bg = R.drawable.bg5;

             layout.setBackgroundResource(bg);
             editor.putInt("backgroundid",id++);
             editor.commit();


    }
Related