Skip to content Skip to sidebar Skip to footer

How To Save My App Background In Sharedpreferences?

I have a fragment with a button that sets a background theme for the whole app. I have set up an interface so the fragment can tell the main activity to set the background or remov

Solution 1:

Use SharedPrefence to store the value for theme like-:

Global Variable

SharedPreferences pref;
SharedPreferences.Editor editor;

In OnCreateView()

pref = getActivity().getSharedPreferences("Theme", Context.MODE_PRIVATE);editor = pref.edit();

Now, store preferences on Button click

enable = (Button) rootView.findViewById(R.id.enable);
enable.setOnClickListener(newView.OnClickListener() {
    @OverridepublicvoidonClick(View v) {

      editor.putInt("yourTheme", 2);
      editor.commit();
        listener.themechanged(2);
        enable.setVisibility(View.GONE);
        disable.setVisibility(View.VISIBLE);

    }
});
disable = (Button) rootView.findViewById(R.id.disable);
disable.setOnClickListener(newView.OnClickListener() {
    @OverridepublicvoidonClick(View v) {
        editor.putInt("yourTheme", 1);
        editor.commit();
        listener.themechanged(1);
        disable.setVisibility(View.GONE);
        enable.setVisibility(View.VISIBLE);
    }
});

and then, In OnCreate() of MainActivity you can check like

SharedPreferencespref= getSharedPreferences("Theme", MODE_PRIVATE);
value= pref.getInt("yourTheme", 1);//1 is default valueif(value==2) {
    if (isDarkTheme) {
        appbackground.setVisibility(View.GONE);
        shade.setVisibility(View.GONE);
    } else {
        appbackground.setVisibility(View.VISIBLE);
        shade.setVisibility(View.VISIBLE);
    }
}elseif(value==1){
        appbackground.setVisibility(View.GONE);
        shade.setVisibility(View.GONE);
}

Done, it may be helpful

Solution 2:

In the onClick() you should do 2 things:

  • Sent the value to the listener (you're already doing this)
  • Save this value to the preferences (already posted how to do that here)

Then, in the onCreate() of your MainActivity you should check for that preference and do the same you are doing on themechanged(int)

Actually, you could use only one onClickListener(), this way:

// Not need to cast to `Button`, since all views can have an onClickListener
rootView.findViewById(R.id.enable).setOnClickListener(clickListener)
rootView.findViewById(R.id.enable).setOnClickListener(clickListener)

// Put this as a member of your Fragment class.
View.OnClickListenerclickListener=newView.OnClickListener() {
    @OverridepublicvoidonClick(View v) {
        if (v.getId() == R.id.enable) {
            // Save your preference here// ... 
            listener.themechanged(2);
            enable.setVisibility(View.GONE);
            disable.setVisibility(View.VISIBLE);            
        }

        if (v.getId() == R.id.R.id.disable) {
            // Save your preference here// ... 
            listener.themechanged(2);
            disable.setVisibility(View.GONE);
            enable.setVisibility(View.VISIBLE);
        }
    }
}

Solution 3:

Let me share this more complex example which can cover this and future needs: https://gist.github.com/walterpalladino/4f5509cbc8fc3ecf1497f05e37675111 The PersistenceManager class is generic, all your app data should be included in the Settings class. I hope it helps.

Post a Comment for "How To Save My App Background In Sharedpreferences?"