Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7701165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:55:10+00:00 2026-05-31T22:55:10+00:00

I have been trying to use/save the boolean value of a checkbox in other

  • 0

I have been trying to use/save the boolean value of a checkbox in other activites but haven’t had much luck.

I know you have to use SharedPreferences however I can’t set it up right. I have made a preferences class which has

    private static final String OPTION_PREF = "my.main.project";
    private SharedPreferences optionPreferences;
    private Editor optionEditor;
    private boolean checkbox;

    public Preferences(Context context)
    {
        this.optionPreferences = context.getSharedPreferences(OPTION_PREF, Activity.MODE_PRIVATE);
        this.optionEditor = optionPreferences.edit();
    }

    public boolean getChecked()
    {
        return optionPreferences.getBoolean("is_checked", checkbox);
    }
    public void saveChecked(boolean checkBox)
    {
        optionEditor.putBoolean("save_check_pref", checkBox);
        optionEditor.commit();
    }

And then in an options menu for example,

    boolean veggieChecked;

    super.onCreate(savedInstanceState);
    setContentView(R.layout.options);

    Preferences pref;
    pref = new Preferences(getApplicationContext());

    veggieChecked = pref.getChecked();

    final CheckBox checkBox = (CheckBox) findViewById(R.id.vegetarian);
    if(checkBox.isChecked())
        veggieChecked = true;

    pref.saveChecked(veggieChecked);

I cannot really see what I am doing wrong as I am new to Android and have not used sharedpreferences before.. any help would be appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:55:12+00:00Added an answer on May 31, 2026 at 10:55 pm

    You can use something like this:

    CheckBox Equities = (CheckBox) findViewById(R.id.Equities);
      CheckBox FixedIncome = (CheckBox) findViewById(R.id.FixedIncome);
      CheckBox Currencies = (CheckBox) findViewById(R.id.Currencies);
      CheckBox Commodities = (CheckBox) findViewById(R.id.Commodities);
      CheckBox Derivatives = (CheckBox) findViewById(R.id.Derivatives);
    
    
    
        myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
        prefsEditor = myPrefs.edit();
    
    
       int prefEq = myPrefs.getInt("Equities", 0);
       int prefFI = myPrefs.getInt("FixedIncome", 0);
       int prefCu = myPrefs.getInt("Currencies", 0);
       int prefCo = myPrefs.getInt("Commodities", 0);
       int prefDe = myPrefs.getInt("Derivatives", 0);
    
         if(prefEq == 1)
         {
           Equities.setChecked(true);
         }
         else
         {
           Equities.setChecked(false);
         }
    
    
         if(prefFI == 1)
         {
           FixedIncome.setChecked(true);
         }
         else
         {
           FixedIncome.setChecked(false);
         }
         if(prefCu == 1)
         {
           Currencies.setChecked(true);
         }
         else
         {
           Currencies.setChecked(false);
         }
         if(prefCo == 1)
         {
           Commodities.setChecked(true);
         }
         else
         {
           Commodities.setChecked(false);
         }
         if(prefDe == 1)
         {
           Derivatives.setChecked(true);
         }
         else
         {
           Derivatives.setChecked(false);
         }
    
    
    
    
    
    
      Equities.setOnCheckedChangeListener(new OnCheckedChangeListener()
      {
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
          {
              if ( isChecked )
              {
                prefsEditor.putInt("Equities", 1); 
                prefsEditor.commit(); 
              }
              else
              {
                myArray[0] = false;
                prefsEditor.putInt("Equities", 0); 
                prefsEditor.commit(); 
              }
    
          }
      });
    
    
      FixedIncome.setOnCheckedChangeListener(new OnCheckedChangeListener()
      {
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
          {
              if ( isChecked )
              {
    
                prefsEditor.putInt("FixedIncome", 1); 
                prefsEditor.commit(); 
              }
              else
              {
    
                prefsEditor.putInt("FixedIncome", 0); 
                prefsEditor.commit();
              }
    
          }
      });
    
    
      Currencies.setOnCheckedChangeListener(new OnCheckedChangeListener()
      {
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
          {
              if ( isChecked )
              {
    
                prefsEditor.putInt("Currencies", 1); 
                prefsEditor.commit(); 
              }
              else
              {
    
                prefsEditor.putInt("Currencies", 0); 
                prefsEditor.commit();
              }
    
          }
      });
    
    
    
    
      Commodities.setOnCheckedChangeListener(new OnCheckedChangeListener()
      {
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
          {
              if ( isChecked )
              {
    
                prefsEditor.putInt("Commodities", 1); 
                prefsEditor.commit(); 
              }
              else
              {
    
                prefsEditor.putInt("Commodities", 0); 
                prefsEditor.commit();
              }
    
          }
      });
    
    
      Derivatives.setOnCheckedChangeListener(new OnCheckedChangeListener()
      {
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
          {
              if ( isChecked )
              {
    
                prefsEditor.putInt("Derivatives", 1); 
                prefsEditor.commit(); 
              }
              else
              {
    
                prefsEditor.putInt("Derivatives", 0); 
                prefsEditor.commit();
              }
    
          }
      });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have been trying to use some ajax to save venue location in my
I have been trying to use localstorage to store Jquery-min but I am unable
I have been trying to use the UISaveVideoAtPathToSavedPhotosAlbum to save my video (stored locally
I have been trying to use django-allauth to provide Social registration, but I am
I have been trying to use Server Side Includes on my website to save
I have been trying to use date_format , however as far as I know,
I have been trying to use array_unique to remove duplicates from the search results
I have been trying to use routes.rb for creating a URL /similar-to-:product (where product
I have been trying to use the hibernate dialect for SQLite from http://code.google.com/p/hibernate-sqlite/ in
I have been trying to use 3rd party tool called visiblox for rendering charts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.