To save visibility as Shared Preferences

Asked 2 years ago, Updated 2 years ago, 98 views

I tried to save visibility as shared preference by referring to stack overflow, but only split_two state is saved and the rest is not saved. Where should I modify to save the visibility of all buttons?

@Override
    public View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container, @NonNull Bundle savedInstanceState) {
        View v=  inflater. inflate(R.layout.calender, container, false);

        //Saving Visible
        final SharedPreferences prefs = this.getActivity().getSharedPreferences("visible", Context.MODE_PRIVATE);

        //Pop-up menu
       final Button buttonPopup = v.findViewById(R.id.btn_split);
       buttonPopup.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               PopupMenu popupMenu = new PopupMenu(getContext(), buttonPopup);
               popupMenu.getMenuInflater().inflate(R.menu.popup_split,popupMenu.getMenu());
               popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                   @Override
                   public boolean onMenuItemClick(MenuItem item) {
                       switch (item.getItemId()){
                           case R.id.split_two:

                               prefs.edit().putBoolean("three", false).apply();
                               prefs.edit().putBoolean("four", false).apply();
                               prefs.edit().putBoolean("five", false).apply();
                               prefs.edit().putBoolean("six", false).apply();
                               frame_three.setVisibility(View.GONE);
                               frame_four.setVisibility(View.GONE);
                               frame_five.setVisibility(View.GONE);
                               frame_six.setVisibility(View.GONE);
                               break;
                           case R.id.split_three:
                               prefs.edit().putBoolean("three", false).apply();
                               prefs.edit().putBoolean("four", false).apply();
                               prefs.edit().putBoolean("five", false).apply();
                               prefs.edit().putBoolean("six", false).apply();
                               frame_three.setVisibility(View.VISIBLE);
                               frame_four.setVisibility(View.GONE);
                               frame_five.setVisibility(View.GONE);
                               frame_six.setVisibility(View.GONE);
                               break;
                           case R.id.split_four:
                               prefs.edit().putBoolean("three", false).apply();
                               prefs.edit().putBoolean("four", false).apply();
                               prefs.edit().putBoolean("five", false).apply();
                               prefs.edit().putBoolean("six", false).apply();
                               frame_three.setVisibility(View.VISIBLE);
                               frame_four.setVisibility(View.VISIBLE);
                               frame_five.setVisibility(View.GONE);
                               frame_six.setVisibility(View.GONE);
                               break;
                           case R.id.split_five:
                               prefs.edit().putBoolean("three", false).apply();
                               prefs.edit().putBoolean("four", false).apply();
                               prefs.edit().putBoolean("five", false).apply();
                               prefs.edit().putBoolean("six", false).apply();
                               frame_three.setVisibility(View.VISIBLE);
                               frame_four.setVisibility(View.VISIBLE);
                               frame_five.setVisibility(View.VISIBLE);
                               frame_six.setVisibility(View.GONE);
                               break;
                           case R.id.split_six:
                               prefs.edit().putBoolean("three", false).apply();
                               prefs.edit().putBoolean("four", false).apply();
                               prefs.edit().putBoolean("five", false).apply();
                               prefs.edit().putBoolean("six", false).apply();
                               frame_three.setVisibility(View.VISIBLE);
                               frame_four.setVisibility(View.VISIBLE);
                               frame_five.setVisibility(View.VISIBLE);
                               frame_six.setVisibility(View.VISIBLE);
                               break;
                       }
                       return true;
                   }
               });
               popupMenu.show();
           }
       });
        //Pop-up menu
        frame_three.setVisibility(prefs.getBoolean("three", true) ? View.VISIBLE : View.GONE);
        frame_four.setVisibility(prefs.getBoolean("four", true) ? View.VISIBLE : View.GONE);
        frame_five.setVisibility(prefs.getBoolean("five", true) ? View.VISIBLE : View.GONE);
        frame_six.setVisibility(prefs.getBoolean("six", true) ? View.VISIBLE : View.GONE);

java android sharedpreference visibility

2022-09-20 18:58

1 Answers

Looking at the code, when split_two, frame_three~size is GONE and false is specified in the preference, but when split_three,

prefs.edit().putBoolean("three", true).apply();

Shouldn't you put it in true?`

And make it simpler

frame_three.setVisibility(View.VISIBLE);
prefs.edit().putInteger("three", frame_three.getVisibility()).apply();

...

frame_three.setVisibility(prefs.getInteger("three", VISIBLE);

I recommend you change it like this. It seems that you made a mistake while manually changing visibility to true and false.


2022-09-20 18:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.