0

I have a little problem. As you know, the

     addPreferencesFromResource(R.xml.mypreference);

is deprecated. How can I store checkbox preferences so that a user can see the checkbox the next time he starts the application ?

TOTAL NOOB HERE. my activity which has the checkboxes:

    package com.example.program;

    import com.example.program.R;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.view.View.OnClickListener;

    public class JourneyActivity extends Activity {

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

    final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
    if (checkBox.isChecked()) {
        checkBox.setChecked(false);
    }

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


}

my program.xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/menu1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".JourneyActivity" >


<CheckBox

    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:text="@string/done1"
    android:textColor="@color/red" />





    </RelativeLayout>

Ignore possible errors in xml file. So, how do I store the "click" when a user clicks my checkBox1 in the activity ?

THANK YOU.

  • Possible solution: http://stackoverflow.com/questions/10016752/saving-checkbox-states – gtgaxiola Jan 10 '14 at 18:12
  • [Shared Preferences](http://developer.android.com/reference/android/content/SharedPreferences.html)? – takendarkk Jan 10 '14 at 18:12
  • [Check this out](http://stackoverflow.com/questions/6822319/what-to-use-instead-of-addpreferencesfromresource-in-a-preferenceactivity) – Tabrej Khan Jan 10 '14 at 18:14
  • None work. Trust me, I tried everything, it changed from '12 'till now. – user3182944 Jan 10 '14 at 18:25
  • So, what is the aim? showing **a PreferenceActivity for devices under 11 and a PreferenceFragment for newer devices** or to save and read SharedPrefereces via **code only** bypassing the specialized UI? – Phantômaxx Jan 10 '14 at 18:29

1 Answers1

0

on checkBox click save the preference as

//methods to set and get preference data

public void checkBoxSelected(boolean status){
       SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
       SharedPreferences.Editor editor = preferences.edit();
       editor.boolean("checked", status);
       editor.commit();
    }



public boolean getCheckBoxState(){
      SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
      preferences.getBoolean("checked", false);
    }

//on checkbox checked listener, implement it

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
             checkBoxSelected(true);
        } else {
             checkBoxSelected(false);
        }
    }

then on start of the application in onCreate() use your preference data as

   yourCheckBox.setChecked());
Saqib
  • 1,120
  • 5
  • 22
  • 40