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 7820925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:28:48+00:00 2026-06-02T07:28:48+00:00

I am very new and would appreciate if someone could demonstrate the code required

  • 0

I am very new and would appreciate if someone could demonstrate the code required to save a number of checkbox states in java inside of an android application.

Say i have a list of tools (Ten or more) a user needs to complete a task and would like them to be able to check off each one and have that data saved (within the app, not sQlite) so that it is recorded when they return to the application.

I have some idea of how this is done but really feel like i need to see the code to understand correctly.

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;

public class CheckBoxTest extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.checkboxtest);
    CheckBox cb1,cb2,cb3,cb4;

    cb1 = (CheckBox)findViewById(R.id.checkBox1);
    cb2 = (CheckBox)findViewById(R.id.checkBox2);
    cb3 = (CheckBox)findViewById(R.id.checkBox3);
    cb4 = (CheckBox)findViewById(R.id.checkBox4);   
     }
 }
  • 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-06-02T07:28:51+00:00Added an answer on June 2, 2026 at 7:28 am

    Use the below code to store and retrive the data in SharedPreference. Your save each check box state in SharedPreference

    //To get value from SharedPreference

    SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
    boolean value = preferences.getBoolean("KEY", false);
    String value = preferences.getString("KEY");
    

    //To Save value in SharedPreference

    SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("KEY", value);
    editor.putBoolean("KEY", value);
    editor.commit();
    

    look into it few minor changes::::

    public class CheckBoxTest extends Activity implements OnCheckedChangeListener {
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.favorites_add_button);
                CheckBox cb1,cb2,cb3,cb4;
    
                cb1 = (CheckBox)findViewById(R.id.checkBox1);
                cb1.setChecked(getFromSP("cb1"));
                cb1.setOnCheckedChangeListener(this);
                cb2 = (CheckBox)findViewById(R.id.checkBox2);
                cb2.setChecked(getFromSP("cb2"));
                cb2.setOnCheckedChangeListener(this);
                cb3 = (CheckBox)findViewById(R.id.checkBox3);
                cb3.setChecked(getFromSP("cb3"));
                cb3.setOnCheckedChangeListener(this);
                cb4 = (CheckBox)findViewById(R.id.checkBox4);
                cb4.setChecked(getFromSP("cb4"));
                cb4.setOnCheckedChangeListener(this);
    
            }
            private boolean getFromSP(String key){
            SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
            return preferences.getBoolean(key, false);
            }
            private void saveInSp(String key,boolean value){
            SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean(key, value);
            editor.commit();
            }
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                switch(buttonView.getId()){
                case R.id.checkBox1:
                saveInSp("cb1",isChecked);
                break;
                case R.id.checkBox2:
                saveInSp("cb2",isChecked);
                break;
    
                case R.id.checkBox3:
                saveInSp("cb3",isChecked);
                break;
    
                case R.id.checkBox4:
                saveInSp("cb4",isChecked);
                break;
                }
    
            }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to css3 and would greatly appreciate if someone could give me the
I'm very new to C++ and would really appreciate any and all help. I
I would be very glad if someone could show me a good example of
I am very new to Visual Studio but i would like to use it
I'm very new to web app development and I thought I would start with
Im very new to all coding including jquery. I though this would have been
I am very new to php and simplepie. I would like to be able
Please excuse my limited understanding of Java Webstart as I'm very new into this.
I don't really understand the use of 'this' in Java. If someone could help
Hey Guys I would very much appreciate some help with the following. We're using

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.