-1

Its like a fan regulator everytime image (src should be of ImageButton)should change on click and retain its value if app closes. What should be the best way ? i want to use just one java file

GLOBAL VAR count = 0;

In onCreate

aButton4 = (ImageButton) findViewById(R.id.imageButton4);
SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
int f = sharedPreferences.getInt("count_key", count);
if (count == 0) {
        aButton4.setImageResource(R.drawable.reg0);
    }
    if (count == 1) {
        aButton4.setImageResource(R.drawable.reg1);
    }
    if (count == 2) {
        aButton4.setImageResource(R.drawable.reg2);
    }
    if (count == 3) {
        aButton4.setImageResource(R.drawable.reg3);
    }
    if (count == 4) {
        aButton4.setImageResource(R.drawable.reg4);
    }

**Then **

    aButton4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            count++;
            SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
            int f = sharedPreferences.getInt("clicked4", count);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor = sharedPreferences.edit();
            if (count<5) {
                if (count == 1) {
                    toggleSound.start();
                    aButton4.setImageResource(R.drawable.reg1);
                    editor.putInt("clicked4", count);
                    editor.commit();
                }
                if (count == 2) {
                    toggleSound.start();
                    aButton4.setImageResource(R.drawable.reg2);
                    editor.putInt("clicked4", count);
                    editor.commit();
                }
                if (count == 3) {
                    toggleSound.start();
                    aButton4.setImageResource(R.drawable.reg3);
                    editor.putInt("clicked4", count);
                    editor.commit();
                }
                if (count == 4) {
                    toggleSound.start();
                    aButton4.setImageResource(R.drawable.reg4);
                    editor.putInt("clicked4", count);
                    editor.commit();
                }
            }
            else {
                count = 0;
                toggleSound.start();
                aButton4.setImageResource(R.drawable.reg0);
                editor.putInt("clicked4", count);
                editor.commit();
            }
        }
    });

}
Swarnveer
  • 490
  • 5
  • 23

3 Answers3

0

Set an onClickListener. Have the onClickListener set a new source. Write the value of the source to shared preferences so its saved. In your Activity's onCreate, read the shared preference to find out what to set it to initially.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • can you please elaborate a bit ? I am new to android – Swarnveer Mar 02 '16 at 13:59
  • Then I'd google for a few of those terms and learn. Try OnClickListener, sharedPreferences, and the Activity lifecycle (which onCreate is part of). – Gabe Sechan Mar 02 '16 at 14:00
  • i wrote the whole code and now just one problem is occurring. When i restart the app it image resets to its default src which is reg0. Please checkout the updated program – Swarnveer Mar 03 '16 at 10:36
0

This is logic kind of question, and below answer will not work if you simply copy and paste, you have to write code to get below code executed.

int count_Global_Declar =0;

SharedPreferences.Editor soteCount= getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();

button.setOnclickList(.... View view){
bla bla...


if(count <=5){
count = count+1;
soteCount= .putInt("count_key", count_Global_Declar ).commit();
storeCount.
    ifcount_Global_Declar  == 1){
    //First image source
    } 
    if(count_Global_Declar  ==2){
    //Secound image source
    }

    if(count_Global_Declar  ==3){
    // Third image source
    }

    .... So on .,
}else{
count_Global_Declar = 0;
soteCount= .putInt("count_key", count_Global_Declar );
}
)};

onStart(){
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
count_Global_Declar =  prefs.getInt("count_key", 0);
}
RAAAAM
  • 3,378
  • 19
  • 59
  • 108
  • what's that soteCount doing in below if(<=5). sytntax for putINT ? – Swarnveer Mar 02 '16 at 14:34
  • Try it yourself and post your error if you have, and this bolg is meant for sharing the knowledge not to spoon feed the developers. Thanks. – RAAAAM Mar 02 '16 at 15:46
  • i wrote the whole code and now just one problem is occurring. When i restart the app it image resets to its default src which is reg0. Please checkout the updated program – Swarnveer Mar 03 '16 at 10:36
  • get the stored sharedP values from onStart method, i just modified the code. accept the answer if it works fine. Thanks. – RAAAAM Mar 03 '16 at 13:25
  • you assigned 0 to "count_key" in onStart() that will select default image that is reg0 – Swarnveer Mar 03 '16 at 16:14
0

i'd do it this way :

private int Clicks;

public class .... extends .... implements View.OnClickListener

Then in your OnCreate

aButton4.setOnClickListener(this);

Inside the onClickListener method

@Override
    public void onClick(View v) {
        Clicks++;
        switch (Clicks)
        {
            case 1:
                aButton4.setImageResource(R.drawable.reg0);
                toggleSound.start();
                sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putInt("clicked4", count);
                editor.commit();
            case 2:
                aButton4.setImageResource(R.drawable.reg1);
                toggleSound.start();
                sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putInt("clicked4", count);
                editor.commit();
            case 3:
                ....
            case 4:
                ....
                Clicks =0;
       }

EDIT: In your onCreate code, do next

aButton4 = (ImageButton) findViewById(R.id.imageButton4);
SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
int f = sharedPreferences.getInt("count_key", count);
if (f== 0) {
        aButton4.setImageResource(R.drawable.reg0);
    }
    if (f== 1) {
        aButton4.setImageResource(R.drawable.reg1);
    }
    if (f== 2) {
        aButton4.setImageResource(R.drawable.reg2);
    }
    if (f== 3) {
        aButton4.setImageResource(R.drawable.reg3);
    }
    if (f== 4) {
        aButton4.setImageResource(R.drawable.reg4);
    }
sander338
  • 240
  • 2
  • 14
  • the problem is its not saving its state, when the app closes it resets to default image....i am updating my code kindly check it – Swarnveer Mar 03 '16 at 10:27
  • i think i see the mistake, you define count as an int and call it in your onCreate for showing image, so it will always show the default given integer. change this : 'int f = sharedPreferences.getInt("count_key", count); if (count == 0) { aButton4.setImageResource(R.drawable.reg0); }' **to this** 'int f = sharedPreferences.getInt("count_key", 1); "the value you want, default int value" if (f == 0) { aButton4.setImageResource(R.drawable.reg0); }' – sander338 Mar 03 '16 at 14:50
  • but why are you assigning 1 to count_key and this int count is just for condition checking, so why it doesn't work ? – Swarnveer Mar 03 '16 at 16:09
  • you aren't assigning 1 to count_key, if the sharedpreferences doesn't find count_key, it will take the default value which in your case is count? So in your onCreate, the default value for count is that which you set on top (private int count = ... – sander338 Mar 03 '16 at 16:19
  • From android developers, [link](http://developer.android.com/reference/android/content/SharedPreferences.html#getInt%28java.lang.String,%20int%29) **public abstract int getInt (String key, int defValue) Added in API level 1 Retrieve an int value from the preferences. Parameters key String: The name of the preference to retrieve. defValue int: Value to return if this preference does not exist. Returns int Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not an int.** – sander338 Mar 03 '16 at 16:20
  • i have 5 images, which much switch after each click. take the example of fan regulator – Swarnveer Mar 03 '16 at 16:20
  • if i use 'int f = sharedPreferences.getInt("count_key", 1); "the value you want, default int value" if (f == 0) { aButton4.setImageResource(R.drawable.reg0); }' i won't be able to switch between 5 images – Swarnveer Mar 03 '16 at 16:24
  • oh, you will, because the sharedpreferences is going to seek after count_key, it finds it with an value and return that value. If the sharedpreferences doesn't find count_key, it will give the default value, in your case, count – sander338 Mar 03 '16 at 16:29
  • so what exactly i should change in my code to get proper functioning of my task – Swarnveer Mar 03 '16 at 16:31
  • so what exactly i should change in my code to get proper functioning of my task – Swarnveer Mar 03 '16 at 16:33
  • if you can help me on this another question http://stackoverflow.com/questions/35772102/usage-of-gif-in-imagebutton?noredirect=1#comment59215863_35772102 that would be great – Swarnveer Mar 03 '16 at 16:55