2

i tried to save the Visibility state of my cardviews, at first i have the views as gone, but if the user pres the button the cardview will show up. Could someone help me and tell me how i could store the visibility, I can't find anything on this topic

Thank you so much.

I tried shardPrefs but is does not work

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cv1= (CardView) findViewById(R.id.cv1);
    cv2= (CardView) findViewById(R.id.cv2);
    cv3= (CardView) findViewById(R.id.cv3);
    cv4= (CardView) findViewById(R.id.cv4);
    cv5= (CardView) findViewById(R.id.cv5);
    cv6= (CardView) findViewById(R.id.cv6);
    cv7= (CardView) findViewById(R.id.cv7);
    cv8= (CardView) findViewById(R.id.cv8);
    cv9= (CardView) findViewById(R.id.cv9);

    AddBar = findViewById(R.id.AddBar);
    AddVoorraad =findViewById(R.id.AddVoorraad);

    AddBar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            clickcount = clickcount + 1;



            if (clickcount == 1) {

                cv1.setVisibility(View.VISIBLE);




            }

            if (clickcount == 2) {
                cv2.setVisibility(View.VISIBLE);
            }


            if (clickcount == 3) {
                cv3.setVisibility(View.VISIBLE);


            }
            if (clickcount == 4) {
                cv4.setVisibility(View.VISIBLE);


            }

            if (clickcount == 5) {
                cv5.setVisibility(View.VISIBLE);
            }


            if (clickcount == 6) {
                cv6.setVisibility(View.VISIBLE);


            }

            if (clickcount==7 ){
                cv7.setVisibility(View.VISIBLE);


            }

        }

    });

    AddVoorraad.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            clickcountV=clickcountV+1;

            if (clickcountV==1) {
                cv8.setVisibility(View.VISIBLE);


            }

            if (clickcount==2) {
                cv9.setVisibility(View.VISIBLE);
            }

        }
    });

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

        }
    });

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

        }
    });

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

        }
    });


}

}`

  • So you want to save which buttons are visible so you can restore it the next time the app runs? – Chooven Oct 29 '19 at 18:56

3 Answers3

1

Try to store visibility for each CardView inside SharedPrefs on condition. And retrieve those and set during initialization of CardView. Check below code:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

int isVisible = sharedPreferences.getInt("cv1", View.GONE);
cv1.setVisibility(isVisible);
isVisible = sharedPreferences.getInt("cv2", View.GONE);
cv2.setVisibility(isVisible);
isVisible = sharedPreferences.getInt("cv3", View.GONE);
cv3.setVisibility(isVisible);
isVisible = sharedPreferences.getInt("cv4", View.GONE);
cv4.setVisibility(isVisible);
isVisible = sharedPreferences.getInt("cv5", View.GONE);
cv5.setVisibility(isVisible);
isVisible = sharedPreferences.getInt("cv6", View.GONE);
cv6.setVisibility(isVisible);
isVisible = sharedPreferences.getInt("cv7", View.GONE);
cv7.setVisibility(isVisible);
isVisible = sharedPreferences.getInt("cv8", View.GONE);
cv8.setVisibility(isVisible);
isVisible = sharedPreferences.getInt("cv9", View.GONE);
cv9.setVisibility(isVisible);

AddBar.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        clickcount = clickcount + 1;

        if (clickcount == 1) {

            cv1.setVisibility(View.VISIBLE);
            sharedPreferences.edit().putInt("cv1", View.VISIBLE).commit();
        }

        if (clickcount == 2) {
            cv2.setVisibility(View.VISIBLE);
            sharedPreferences.edit().putInt("cv2", View.VISIBLE).commit();
        }

        if (clickcount == 3) {
            cv3.setVisibility(View.VISIBLE);
            sharedPreferences.edit().putInt("cv3", View.VISIBLE).commit();
        }

        if (clickcount == 4) {
            cv4.setVisibility(View.VISIBLE);
            sharedPreferences.edit().putInt("cv4", View.VISIBLE).commit();
        }

        if (clickcount == 5) {
            cv5.setVisibility(View.VISIBLE);
            sharedPreferences.edit().putInt("cv5", View.VISIBLE).commit();
        }

        if (clickcount == 6) {
            cv6.setVisibility(View.VISIBLE);
            sharedPreferences.edit().putInt("cv6", View.VISIBLE).commit();
        }

        if (clickcount==7 ){
            cv7.setVisibility(View.VISIBLE);
            sharedPreferences.edit().putInt("cv7", View.VISIBLE).commit();
        }
    }
});

AddVoorraad.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        clickcountV=clickcountV+1;

        if (clickcountV==1) {
            cv8.setVisibility(View.VISIBLE);
            sharedPreferences.edit().putInt("cv8", View.VISIBLE).commit();
        }

        if (clickcountV==2) {
            cv9.setVisibility(View.VISIBLE);
            sharedPreferences.edit().putInt("cv9", View.VISIBLE).commit();
        }

    }
});
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
  • Very nice! Thanks a lot! It works, I made a little adjustment cause I ren into an little error. So if someone has the same error follow this: https://stackoverflow.com/questions/28515049/android-content-context-getpackagename-on-a-null-object-reference – Bas Hogeveen Oct 29 '19 at 23:23
0

I don't know what you tried exactly, but if all you want to do is store the state for later use you can certainly accomplish that with SharedPreferences. You can do something like this to save the state as a string representing the state:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
SharedPreferences.Editor editor = prefs.edit();  
editor.putString("cv1_visibility", "VISIBLE");
editor.putString("cv2_visibility", "INVISIBLE");
editor.putString("cv3_visivility", "GONE");
...
editor.commit();

and then you can read these values like this:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
String cv1_state = prefs.getString("cv1_visibility", "NOT_FOUND");
String cv2_state = prefs.getString("cv2_visibility", "NOT_FOUND");
String cv3_state = prefs.getString("cv3_visibility", "NOT_FOUND");
...

and then handle it appropriately based on the state at the start of your activity. Note that getString() has a defValue parameter (the second one) which is the value to return if the preference does not exist.

EDIT: You can choose to store them as int, but be mindful that some of these constants are not int. Take a look at this to see what I mean: https://developer.android.com/reference/android/view/View

JavierCastro
  • 318
  • 2
  • 8
  • First of all thanks for your reply! I want to create an inventory app. The user will be able to add serval bars with a max of 7, same for "Add voorraad (means stock) but with the max of 3. – Bas Hogeveen Oct 29 '19 at 23:24
  • You are welcome and best of luck with your project! – JavierCastro Oct 30 '19 at 00:27
0

You can retrieve the value from onSaveInstanceState when the configuration changes ( rotation) but when you close your app (back button) then you need to retrieve the state from the persistence storage like shared preferences.