I am a beginner in Android and I have a problem in saving Dynamic Buttons.
The code should add a button dynamically when the user needs it.
When users click on addService this code takes (a name for the button and a value to use in the intent for this button) from a second Activity, then adds the button dynamically in this Activity with the name and intent and the user can click it for service.
when we reload the application, Theses buttons will disappear.
How to save the button dynamically added by the user?
Source Code:
package com.example.fst.miniprojetandroidn1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import java.util.ArrayList;
public class ajouter_programme extends AppCompatActivity {
ArrayList<Button> list_programme=new ArrayList<Button>();
Button Addbutton;
int count = 1,c;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ajouter_programme);
setTitle("Liste des programmes ");
Addbutton = findViewById(R.id.plusbutton);
Addbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button myButton = new Button(ajouter_programme.this);
c=count++;
myButton.setId(c);
myButton.setText("Programme " + c);
list_programme.add(myButton);
LinearLayout ll = (LinearLayout) findViewById(R.id.buttonlayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ajouterprog();
}
});
}
});
}
public void ajouterprog(){
Intent list_programmes_Intent = new Intent(ajouter_programme.this,ajouter_medicamement.class);
startActivity(list_programmes_Intent);
}
}
amd .xml is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/buttonlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="#+id/plusbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Ajouter programme"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
Thank you very much for your help.
Your ArrayList which contains your Buttons is empty because you don't store them. Instead of using that ArrayList (if you don't have any other purpose) you can use SharedPreferences. SharedPreferences uses key-value pair for storing datas. There are two options of SharedPreferences:
1.Activiy based SharedPreferences: You can only access with same Activity which you use SharedPreferences. You can't access to that with any other Activities in your App.
//Call Activity based SharedPreferences
SharedPreferences sp = getPreferences(MODE_PRIVATE);
//Calling Editor interface for transaction
SharedPreferences.Editor editor = sp.edit();
//Put primitive data types (int, String, boolean, float etc.)
editor.putInt(String key, int value);
editor.putString(String key, String value);
editor.putBoolean(String key, Boolean value);
//Apply your changes and finish editing.
editor.apply();
2.Application based SharedPreferences: You can access on any Activity in your App.
//Call Activity based SharedPreferences
SharedPreferences sp = getSharedPreferences(String name,MODE_PRIVATE);
//Calling Editor interface for transaction
SharedPreferences.Editor editor = sp.edit();
//Put primitive data types (int, String, boolean, float etc.)
editor.putInt(String key, int value);
editor.putString(String key, String value);
editor.putBoolean(String key, Boolean value);
//Apply your changes and finish editing.
editor.apply();
Note: String name is the name of xml file which your datas will be stored with SharedPreferences and you can name it as you want. Also with SharedPreferences you can only store primitive data types (if you don't use GSON). Store information about your button(s) and recreate everytime you start or resume your Activity.
Getting back datas from Shared Preferences:
//Activity based SharedPreferences
SharedPreferences sp = getPreferences(MODE_PRIVATE);
//For getting int
int value = sp.getInt(String key, defautValue);
//For getting String
String value = sp.getString(String key, defaultValue);
//For getting Boolean
Boolean value = sp.getBoolean(String key, defaultValue);
Note: int count will return same value (in this case 1) everytime you start or resume your Activity back. You need to fix it something like:
SharedPreferences sp = getPreferences(MODE_PRIVATE);
int count = sp.getInt("count", 1);
For example a method for storing information:
private void saveButton(int count) {
//Call Activity (or Application) based SharedPreferences
SharedPreferences sp = getPreferences(MODE_PRIVATE);
//Calling Editor interface for transaction
SharedPreferences.Editor editor = sp.edit();
//Put primitive data types (for this case its integer)
//Number of buttons
editor.putInt("count", (count + 1));
//Ids of each Button
editor.putInt("btn_id" + count, count);
//Apply your changes and finish editing.
editor.apply();
}
You can use this method inside on your Button's click method which you're using for creating new a Button. Everytime you click that Button for creating new Button that method helps for saving information about your Button.
For loading your Button(s) back you can use a method like:
private void loadButtons() {
//Find your layout which buttons will be added.
LinearLayout ll = (LinearLayout) findViewById(R.id.buttonlayout);
//Call Activity (or Application) based SharedPreferences
SharedPreferences sp = getPreferences(MODE_PRIVATE);
//Get number of buttons you've saved.
int count = sp.getInt("count", 1);
for (int i = 1; i < count; i++) {
//Get back required datas for creating for your Buttons again
int id = sp.getInt("btn_id" + i, 1);
//Create new button
Button btn = new Button(this);
btn.setText("Programme" + i);
btn.setId(id);
//Add button to your layout
ll.addView(btn);
//Set ClickListener for every Button
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ajouterprog();
}
});
}
You need to call this method inside onCreate() or onResume() method.
Related
I have an activity with two TextViews which show int values. These values change incrementally (1, 2, 3, and so...) when the user clicks a button. I use SharedPreferences to store that values via button click. When I close the app and open it again, the values are correctly displayed in the TextViews, but if they change, they should be added from the previous stored value. Problem is that they start to count from zero.
Here is my code:
public class Roulette1 extends ActionBarActivity {
Button button0, button1;
int click_button0, click_button1;
public static final String button0Str = "button0Key";
public static final String button1Str = "button1Key";
public static final String MyPREFERENCES = "MyPrefsRoulette1";
SharedPreferences sharedpreferences;
TextView times_0_tv;
TextView times_1_tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
times_0_tv = (TextView) findViewById(R.id.times_0);
times_1_tv = (TextView) findViewById(R.id.times_1);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
final TextView total_clicks_tv = (TextView) findViewById(R.id.total_clicks);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
click_button1 = click_button1 + 1;
int total_clicks = click_button0 + click_button1;
total_clicks_tv.setText(String.valueOf(total_clicks));
times_0_tv.setText(click_button0);
times_1_tv.setText(click_button1);
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
click_button0 = click_button0 + 1;
int total_clicks = click_button0 + click_button1;
total_clicks_tv.setText(String.valueOf(total_clicks));
times_0_tv.setText(click_button0);
times_1_tv.setText(click_button1);
}
});
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if (sharedpreferences.contains(button0Str))
{
times_0_tv.setText(sharedpreferences.getString(button0Str, ""));
}
if (sharedpreferences.contains(button1Str))
{
times_1_tv.setText(sharedpreferences.getString(button1Str, ""));
}
}
public void run1(View view) {
SharedPreferences.Editor editor = sharedpreferences.edit();
String times0string = times_0_tv.getText().toString();
String times1string = times_1_tv.getText().toString();
editor.putString(button0Str, times0string);
editor.putString(button1Str, times1string);
editor.commit();
}
Hope you have any idea. Thanks!
When you read from sharedPrefs, remember to update the field counters and not just the value of the textViews.
As suggested in the comments, a possible solution would be to use the textView value as the state, updating that directly. Otherwise you have to keep the counters updated manually, for example by updating the fields value at the same time you update the textView value. Personally, I prefer to keep the state separated from the presentation, so that it is easier to compute something with that value later (the downside is that you have to keep the view synchronized. This might change with the new data binding library).
PS I purposely did not put any code, because the solution is trivial and there are other answers with code, but more importantly because I think that the data binding library is a much cleaner way to deal with this kind of problems, even though it's still in beta stage.
Sharedpreferences stored int data also. Check this link:
http://androidexample.com/Android_SharedPreferences_Basics/index.php?view=article_discription&aid=126&aaid=146
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
Editor editor = pref.edit();
/**************** Storing data as KEY/VALUE pair *******************/
editor.putBoolean("key_name1", true); // Saving boolean - true/false
editor.putInt("key_name2", "int value"); // Saving integer
editor.putFloat("key_name3", "float value"); // Saving float
editor.putLong("key_name4", "long value"); // Saving long
editor.putString("key_name5", "string value"); // Saving string
// Save the changes in SharedPreferences
editor.commit(); // commit changes
I think there is some logical mistake in your code. Please check.
Add this code to the very beginning of your oncreate:
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
times0string = String.valueOf(sharedPreferences.getString(button0Str, 0));
times1string = String.valueOf(sharedPreferences.getString(button1Str, 0));
To solve your problem, Try below code:
Replace first two line inside button1 onClick()
click_button1 = Integer.parseInt(sharedpreferences.getString(button1Str, "")) + 1;
int total_clicks = Integer.parseInt(sharedpreferences.getString(button0Str, "")) + click_button1;
Replace first two line inside button0 onClick()
click_button0 = Integer.parseInt(sharedpreferences.getString(button0Str, "")) + 1;
int total_clicks = click_button0 + Integer.parseInt(sharedpreferences.getString(button1Str, ""));
am trying to save he color picked from the holocolorpicker and use it in another activity
before the onCreate method in the settings activity i put this lines
private String SettingsTAG0 = "backcolorValue";
private SharedPreferences backcolorprefs;
private static int backcolorValue = 0;
after the onCreate method in the settings activity i put this lines
public void onColorChanged(int color) {
ColorPicker picker0 = (ColorPicker) findViewById(R.id.backpicker);
backcolorValue = picker0.getColor();
Editor editor0 = backcolorprefs.edit();
editor0.clear();
editor0.putInt("back_colorcode", backcolorValue);
editor0.commit();
}
before the onCreate method of the other activity i put this lines
private String SettingsTAG0 = "backcolorValue";
private SharedPreferences backcolorprefs;
private static int backcolorValue = 0;
in the onCreate method of the other activity i put this lines
backcolorprefs = getSharedPreferences(SettingsTAG0, 0);
backcolorprefs.getInt("back_colorcode", backcolorValue);
View view = this.getWindow().getDecorView();
view.setBackgroundColor(backcolorValue);
i am a super newbie to android and java but i make a try but nothing is happened
any help please
This is how sharedpreference used:
// put int
SharedPreferences sharedpreferences = getSharedPreferences("MyPreference", Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putInt("back_colorcode", backcolorValue);
editor.commit();
// get int
SharedPreferences sharedpreferences = getSharedPreferences("MyPreference", Context.MODE_PRIVATE);
int backcolorValue = sharedpreferences.getInt("back_colorcode", 0)
Nothing happened because you are not assigning value read from preferences to backcolorValue. This line:
backcolorprefs.getInt("back_colorcode", backcolorValue);
simply reads it and DOESN'T store in backcolorValue. Do:
backcolorValue = backcolorprefs.getInt("back_colorcode", backcolorValue);
Also, in onColorChanged, I don't see that you're initialising backcolorprefs, so make sure you do. Btw, for getSharedPreferences, you should use constant Context.MODE_PRIVATE, instead of hardcoded value of 0.
onColorChanged method parameter is color code selected from ColorPicker. save same in SharedPreferences :
public void onColorChanged(int color) {
Editor editor0 = backcolorprefs.edit();
editor0.clear();
editor0.putInt("back_colorcode", color);
editor0.commit();
}
In second Activity get color code from preference and pass to setBackgroundColor :
int colorCode=backcolorprefs.getInt("back_colorcode", Color.BLACK);
View view = this.getWindow().getDecorView();
view.setBackgroundColor(colorCode);
I'm working on an app that's main layout consists of a row of buttons on top of a LinearLayout which hold a form made up of edittexts. I've created a Form object to hold all the values to be displayed in the edittexts for each form. Each button is clicked to change the form's data to show the values held in each Form object. I have the Form objects kept in a List so that I can access each one when the corresponding button is pressed.
EDIT: I've realized I didn't quite explain myself properly. When I click one of the buttons, I want the EditText values to change to a different set of values which correspond to that button. But when I click these buttons, the previously entered values do not show up. I referred to the column of EditTexts a form, but it's just a column of EditTexts.
When I click Next Drink, it dynamically creates a button as well as a Form object, and clears the EditTexts so that it looks like a new form has been created for the new button. I should then be able to add strings to the EditTexts, and the strings should be stored in the Form object so that if I click that button again, the Form object will fill the EditTexts with the strings stored in that object.
So, if I click button 3 after having clicked button 2, it should set the text of each EditText to the values contained in the the third Form object, which contains strings for Name, Volume, Percentage, Price, and Quantity.
My problem is the values do not show up in the EditTexts after clicking a button. So if I click Button 3, the values from Form 3 don't show up. I've been at this for a long time and cannot figure out why, so I'd really appreciate any help. Here is the activity:
package com.example.DrinkApp;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import java.util.ArrayList;
import java.util.List;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
//view variables
final Button newDrink[] = new Button[100];
LinearLayout drinkSelectionContainer;
EditText name;
EditText vol;
EditText perc;
EditText pri;
AutoCompleteTextView quant;
//counters
int numOfDrinks = 0;
int selectedForm = 0;
//array to hold all Form objects.
List<Form> formList = new ArrayList<Form>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get views
drinkSelectionContainer = (LinearLayout) findViewById(R.id.drinkSelectContainer);
name = (EditText) findViewById(R.id.etName);
vol = (EditText) findViewById(R.id.etVol);
perc = (EditText) findViewById(R.id.etPerc);
pri = (EditText) findViewById(R.id.etPrice);
quant = (AutoCompleteTextView) findViewById(R.id.etQuantity);
Button saveDrink = (Button) findViewById(R.id.bSaveForm);
Button nextDrink = (Button) findViewById(R.id.bNextDrink);
Button crunkOut = (Button) findViewById(R.id.bCalc);
///////////////////////// next drink method
addNewDrinkButton();
name.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
formAdd();
}
#Override
public void afterTextChanged(Editable editable) {
// mNames.add(selectedForm, name.getText().toString());
// newDrink[selectedForm].setText(name.getText().toString());
}
});
nextDrink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addNewDrinkButton();
}
});
}
//listener for drink select buttons. when button is clicked, it finds out which one it was,
//and then displays the correct values.
public View.OnClickListener drinkSelectListener = new View.OnClickListener() {
public void onClick(View v) {
selectedForm = v.getId();
Form form = formList.get(selectedForm);
Button v1 = (Button) findViewById(selectedForm);
v1.setText(" " + selectedForm);
// Do something depending on the value of the tag
Log.v("My Logs:", "Select Drink " + selectedForm + " button pressed.");
Log.v("My Logs:", "Drink " + selectedForm + " name: " + form.getName());
formDisplay(form);
}
};
private void addNewDrinkButton() {
formClear();
formAdd();
numOfDrinks++;
selectedForm = numOfDrinks;
Log.v("My Logs:", "Next Drink button pressed.");
newDrink[numOfDrinks] = new Button(getBaseContext());
newDrink[numOfDrinks].setOnClickListener(drinkSelectListener);
newDrink[numOfDrinks].setId(numOfDrinks);
newDrink[numOfDrinks].setText("" + selectedForm);
newDrink[numOfDrinks].setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
// name.setText("Drink " + (numOfDrinks+1));
drinkSelectionContainer.addView(newDrink[numOfDrinks]);
//TODO: save form values, clear edit texts, and create new variables for edittexts.
}
void formAdd(){
Form tempForm = new Form();
tempForm.setId(selectedForm);
tempForm.setName(name.getText().toString());
tempForm.setVolume(vol.getText().toString());
tempForm.setPercentage(perc.getText().toString());
tempForm.setPrice(pri.getText().toString());
tempForm.setQuantity(quant.getText().toString());
formList.add(selectedForm, tempForm);
}
void formDisplay(Form form){
name.setText(form.getName());
vol.setText(form.getVolume());
perc.setText(form.getPercentage());
pri.setText(form.getPrice());
quant.setText(form.getQuantity());
}
void formClear(){
name.setText("");
vol.setText("");
perc.setText("");
pri.setText("");
quant.setText("");
}
}
!Here's the main layout. So if I click button 4, I want to have the EditTexts display the values
I'd entered the last time I'd clicked on it. It's really very simple, I'm having trouble explaining myself today.
The code provided doesn't seem to coincide with your question. You want to put values into an EditText view and then hit a button that takes the values from that EditText view and places it inside of a "form"? If so, then inside of onCreate() and after you have setContentView() try:
EditText name = (EditText) findViewById(R.id."edit_text_id"); // where "edit_text_id" is your your EditText view id set in your xml
String text = name.getText().toSting();
TextView textView = (TextView) findViewById(R.id."forms_text_field"); // where "forms_text_field" is your TextView id set in your form's xml
textView.setText(text);
Repeat this for each of your EditText views (setting a corresponding TextView in your form to the desired EditText field value). This should work just fine.
I have a TextView. When the Activity is first created the Value of the textView is "", as in nothing. But the user can initiate some actions that can make the text="st". That works fine, once more that works fine. The problem is when I leave the page and come back instead of text="st" , it's " " as in nothing. So the user has to waste time and get the textView back to "st" through some actions.
I tried to save the TextView using SavePreferences but since the value of TextView is nothing when the activity starts SavePreferences does exactly what it's supposed to do and makes the TextView equal nothing. Isn't there some way for me to save the value of the TextView. I have other Views on my page I do not want to save, so how do I save just the TextView as it is when the user leaves the app or activity?
TextView s1;
s1 = (TextView)findViewById(R.id.s1);
//9 miles down the page
LoadPreferences();
SavePreferences("MEM46", s1.getText().toString());
LoadPreferences();}
private void SavePreferences(String key, String value){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();}
private void LoadPreferences(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String strSavedMem46 = sharedPreferences.getString("MEM46", "");
s1.setText(strSavedMem46);
lay1.setOnLongClickListener(new OnLongClickListener(){
public boolean onLongClick(View v){
AlertDialog.Builder alert = new AlertDialog.Builder(xxx.this);
alert.setTitle("Help"); //Set Alert dialog title here
alert.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface d, int choice){
if(choice == 0) {
d.cancel();
}
else if(choice == 1){
TextView ss1=(TextView)findViewById(R.id.s1);
ss1.setText("st");d.cancel(); }
else if(choice == 2) {
TextView ss1=(TextView)findViewById(R.id.s1);
ss1.setText("");d.cancel();}});
alert.show();
return true;
}});
At which point exactly do you call LoadPreferences/SavePreferences?
Saving should happen only when the user leaves the activity, so onPause() must be overriden to call the savePreference(). Loading should be done in onStart().
I am very new to java and android but doing my best to make an app, basicaly I want a page with 6 text boxes on it, and each allows the user to type a 3 digit unique value into each, check a confirm box and then a button to save, then when the user revisits this part of the app the data will still be there, I managed to get it working for 1 box but if i add another it just duplicated box 1s value, here is my code for the class
public class Settings extends Activity implements OnClickListener {
CheckBox cb;
EditText et, et1;
Button b;
String test;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
cb = (CheckBox) findViewById(R.id.checkBox1);
et = (EditText) findViewById(R.id.editText1);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(this);
loadPrefs();
cb.setChecked(false);
}
private void loadPrefs() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean cbValue = sp.getBoolean("CHECKBOX", false);
String name = sp.getString("NAME", "Kg");
if(cbValue){
cb.setChecked(true);
}else{
cb.setChecked(false);
}
et.setText(name + (" kg"));
}
private void savePrefs(String key, boolean value) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putBoolean(key, value);
edit.commit();
}
private void savePrefs(String key, String value) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putString(key, value);
edit.commit();
}
public void onClick(View v) {
// TODO Auto-generated method stub
savePrefs("CHECKBOX", cb.isChecked());
if (cb.isChecked())
savePrefs("NAME", et.getText().toString());
finish();
}
}
any help would be greatly appreciated as time is short :(
Read this.
What you're not coding is saving the data.
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean cbValue = sp.getBoolean("CHECKBOX", false);
What the second line does is says, is "CHECKBOX" a saved sharedpreference? No, it isn't. Okay let's get the default value of false then.
What you need to do is save it using this:
SharedPreferences.editor Editor = sp.edit();
Editor.putBoolean("CHECKBOX",true);
Editor.commit();
The first line defines the sharedpreference editor. The next line saves the boolean value true under the in effect filename (key) of CHECKBOX and then the commit line says, okay do the above and finalise it so that now whenever I call:
sp.getBoolean("CHECKBOX",false);
I will get true because I won't have to use the default value of false.
Try to make this easy for you...
First, in your proferences xml, each text box and check box needs it's own key.
Secondly, to make it easy for you to read/understand you should assign a different name for the pref save method void savePrefs(String key, String value).
For example String: void savePrefsString(String key, String value)
For example boolean: void savePrefsBoolean(String key, boolean value)
Be sure each one is called appropriately (savePrefsBoolean for boolean and savePrefsString for edittext).
Then for each edit text you will want to retrieve the key from preferences for that edittext.
Example:
String name1 = sp.getString("NAME1", "Kg");
String name2 = sp.getString("NAME2", "Kg");
String name3 = sp.getString("NAME3", "Kg");
Then:
et1.setText(name1 + (" kg"));
et2.setText(name2 + (" kg"));
et3.setText(name1 + (" kg"));
Do the same for your checkboxes (they are actually true/false booleans).
Example:
boolean cb1 = sp.getBoolean("CHECKBOX1", false); //false is default value
boolean cb2 = sp.getBoolean("CHECKBOX1", false);
boolean cb3 = sp.getBoolean("CHECKBOX1", false);
Then to set value from prefs:
if(cb1){
cb1.setChecked(true);
}else{
cb1.setChecked(false);
}
and to save what the user has pressed:
savePrefsBoolean("CHECKBOX1", cb1.isChecked()); // get check value of checkbox
savePrefsBoolean("CHECKBOX2", cb2.isChecked());
savePrefsBoolean("CHECKBOX3", cb3.isChecked());