Radiogroup value doesn't save in sharedpreference in android - android

I have want to checkble radiobutton value in my second activity.
i have also use RadioGroup and sharedpreference.
but first Radiobutton value get in my second activity. so sharedpreference not saving the radiobutton value.
please show this code and help me how can i get?
rg = (RadioGroup) findViewById(R.id.radiotype);
rbtn=((RadioButton)rg.findViewById(getSelectedValue()));
if(rbtn!=null){
rbtn.setChecked(true);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
savePreferences(RemainderType_Toggle, checkedId);
}
});
}
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
TypeToggleValue = sharedPreferences.getInt(RemainderType_Toggle, R.id.radionoti);
if (TypeToggleValue == R.id.radionoti)
{
rbtn.setChecked(true);
}
else
{
rbtn.setChecked(false);
}
}
private int getSelectedValue(){
SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(this);
return pref.getInt(RemainderType_Toggle, -1);
}
private void savePreferences(String key, int value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.commit();
}
SecondAcityvity.java
here i want the selected radiobutton value.
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
TypeToggleValue = sharedPreferences.getInt(RemainderType_Toggle, R.id.radioalarm);
Log.d("TypeToggleValue", String.valueOf(TypeToggleValue));

You do not need to refer your RadioButton in reference to your RadioGroup. Simply you can access it without reference.
Change your below line
rbtn=((RadioButton)rg.findViewById(getSelectedValue()));
as below
rbtn=(RadioButton)findViewById(R.id.radioButton);

Related

Sharedpreferences doesnt work, checkbox should stay in the same state when I am closing/opening the app

Hey guys I want to make my checkbox stay in the same state every time I open my app.. I get this with the 'ja/nein' string, the string states when i close and open again my application... but my checkbox.setchecked(true/false) doesnt work.. please help
public void changeVisitStatus(){
SharedPreferences visitStatus = mData.getVisitStatus();
SharedPreferences.Editor editor = visitStatus.edit();
if(visitStatus.getString(mData.getVisitKey(), "nein").equals("nein")){
editor.putString(mData.mVisitKey, "ja");
editor.commit();
mGUI.mBtnVisit.setChecked(true);
}
else{
editor.putString(mData.mVisitKey, "nein");
editor.commit();
mGUI.mBtnVisit.setChecked(false);
}
mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
}
EDIT: I tried it another way.. I thought it would be better but doesnt work as well..
public void changeVisitStatus(){
SharedPreferences visitStatus = mData.getVisitStatus();
SharedPreferences.Editor editor = visitStatus.edit();
if(visitStatus.getString(mData.getVisitKey(), "nein").equals("nein")){
editor.putString(mData.mVisitKey, "ja");
editor.putBoolean("isChecked", true);
editor.commit();
}
else{
editor.putString(mData.mVisitKey, "nein");
editor.putBoolean("isChecked", false);
editor.commit();
}
mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
}
and put this one into my onCreate(Bundle savedInstanceState) in my Activity
mGUI.mBtnVisit.setChecked(mData.getVisitStatus().getBoolean("isChecked", false));
Try like this:
public void putBooleanInPreferences(boolean isChecked,String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, isChecked);
editor.commit();
}
public boolean getBooleanFromPreferences(String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
Boolean isChecked = sharedPreferences.getBoolean(key, false);
return isChecked;
}
and in onCreate()
CheckBox checkBox = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkBox = (CheckBox) findViewById(R.id.my_check_box);
boolean isChecked = getBooleanFromPreferences("isChecked");
Log.i("start",""+isChecked);
checkBox.setChecked(isChecked);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Log.i("boolean",""+isChecked);
TestActivity.this.putBooleanInPreferences(isChecked,"isChecked");
}
});
}
Hope this may help you!
You're showing us only the code for changing the status, probably called from OnClick listener for the check box.
You should also add code that only reads status from SharedPreferences and sets check box state according to that (could be same code, but the if condition negated).
You need to call that code from OnCreate event.
public void setVisitStatus(){
SharedPreferences visitStatus = mData.getVisitStatus();
mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
}

Android CheckBox in Listview

I have a listview populated from the database, and a checkbox for each row. Using putExtras to pass values ​​to a TextView to another Activity. Now when you restart the app I want to display in TextView the last value selected with checkboxes. I need SharedPreferences or is there a method? Thanks
Save your checkbox in preference as below:
//method to load the sharedpreferences.
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
boolean checkBoxValue = sharedPreferences.getBoolean("CheckBox_Value", false);
String name = sharedPreferences.getString("storedName", "YourName");
if (checkBoxValue) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
textview.setText(name);
}
//store boolean value of checkbox in sharedpreferences.
private void savePreferences(String key, boolean value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
//store the string sharedpreference.
private void savePreferences(String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
savePreferences("CheckBox_Value", checkBox.isChecked());
savePreferences("storedName", textview.getText().toString());
}

RadioGroup is not saving preferences

I have following code:
This is in onCreate - I am trying to set default value of a radio button here, which will be the pre selected value on radio button.
pref = getSharedPreferences(Constants.PREF_SETTINGS, 0);
RadioGroup rg_numberOfQuestions = (RadioGroup) findViewById(R.id.radioGroupQuestions);
int selectedOption = rg_numberOfQuestions.getCheckedRadioButtonId();
rb_NumberOfQuestions = (RadioButton) findViewById(selectedOption);
rg_numberOfQuestions.setOnCheckedChangeListener(this);
final RadioGroup rg_numbersBetween = (RadioGroup) findViewById(R.id.radioGroupNumbersBetween);
selectedOption = rg_numbersBetween.getCheckedRadioButtonId();
rb_NumbersBetween = (RadioButton) findViewById(selectedOption);
rg_numbersBetween.setOnCheckedChangeListener(this);
#Override
public void onCheckedChanged(RadioGroup rg, int i) {
switch (rg.getCheckedRadioButtonId())
{
case R.id.rb_0to10:
savePref("rb_0to10", rg.getCheckedRadioButtonId());
break;
case R.id.rb_0to25:
savePref("rb_0to25", rg.getCheckedRadioButtonId());
break;
case R.id.rb_0to50:
savePref("rb_0to50", rg.getCheckedRadioButtonId());
break;
case R.id.rb_15:
savePref("rb_15", rg.getCheckedRadioButtonId());
break;
case R.id.rb_25:
savePref("rb_25", rg.getCheckedRadioButtonId());
break;
case R.id.rb_50:
savePref("rb_50", rg.getCheckedRadioButtonId());
break;
}
}
private void savePref(String key, int value) {
pref = getSharedPreferences(Constants.PREF_SETTINGS, 0);
editor = pref.edit();
editor.putInt(key, value);
editor.commit();
}
Above code works first time it gets the default value of first radio button. But no matter what other radio button i select it is not working it is not saving the button click value it just gets first radio button always.
You can see the value itself in DDMS :
Here is the image for Debugging and see the values in Shared Pref :
And
you can pull and push files also in any location... To see Values in
Shared pref click that file
on Top right side there is 2 icon of mobile with pink Arrow from there
u can push and pull Shared pref Files
Try this code of snippet.. It works perfect...
OnCreate Method code :
pref = getSharedPreferences(Constants.PREF_SETTINGS, 0);
rg_numberOfQuestions = (RadioGroup) findViewById(R.id.radioGroup1);
rb_1 = (RadioButton) findViewById(R.id.radioOne);
rb_1.setOnCheckedChangeListener(this);
rb_2 = (RadioButton) findViewById(R.id.radioTwo);
rb_2.setOnCheckedChangeListener(this);
rb_3 = (RadioButton) findViewById(R.id.radioThree);
rb_3.setOnCheckedChangeListener(this);
int selected_radio_button = pref.getInt("radio", 0);
if(selected_radio_button!=0)
{
RadioButton button = (RadioButton) findViewById(selected_radio_button);
button.setChecked(true);
}
And the listener code :
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
Editor editor = pref.edit();
editor.putInt("radio", buttonView.getId());
editor.commit();
}
}
It seem Initialization of share-preference is not correct. can you just try below code#
private void savePref(String key, int value) {
pref = getSharedPreferences("ANYNAME",Context.MODE_PRIVATE);
editor = pref.edit();
editor.putInt(key, value);
editor.commit();
}

Android store radiogroup id in sharedpreferences and load it

In my App i want to open a dialog window with a radiogroup with some items (each item should be an activity ) the user can choose from. The chosen item/ID should get stored in the sharedpreferences. The ID load every App start and open the chosen item/activity.
Can someone tell me how to do that Please ?
There are many samples but ok, I'll give an example:
You can define 2 methods under your activity:
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int selectedRadioID = sharedPreferences.getInt("SELECTED_RADIO", 0);
if(selectedRadioID > 0) {
// you got previously selected radio
RadioButton rb = (RadioButton)findViewById(selectedRadioID);
rb.setSelected(true);
}
}
private void savePreferences(String key, int radioId) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putInt(key, radioId);
editor.commit();
}
Use this methods on your activities onCreate method.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadSavedPreferences();
RadioGroup rg = findViewById(R.id.your_radio_group_over_your_radios);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
savePreferences("SELECTED_RADIO", checkedId);
}
});
}
You should improve this code, but this will give you the idea.

how to get sharedpreferences from a radiogroup to anotrher activitys

I have two activities (settings, prayers)
in the settings activity I put three radio buttons inside a radio group every radio button will let the colors changed in the prayers activity
settings.class
public class SettingsActivity extends Activity {
RadioGroup rg;
TextView textCheckedID, textCheckedIndex;
final String KEY_SAVED_RADIO_BUTTON_INDEX = "SAVED_RADIO_BUTTON_INDEX";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
rg = (RadioGroup) findViewById(R.id.radios);
rg.setOnCheckedChangeListener(rgOnCheckedChangeListener);
textCheckedID = (TextView) findViewById(R.id.checkedid);
textCheckedIndex = (TextView) findViewById (R.id.checkedindex);
LoadPreferences();
}
OnCheckedChangeListener rgOnCheckedChangeListener = new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton checkedRadioButton = (RadioButton)rg.findViewById(checkedId);
int checkedIndex = rg.indexOfChild(checkedRadioButton);
textCheckedID.setText("checkedID = " + checkedId);
textCheckedIndex.setText("checkedIndex = " + checkedIndex);
SavePreferences(KEY_SAVED_RADIO_BUTTON_INDEX, checkedIndex);
}
};
private void SavePreferences(String key, int value) {
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.commit();
}
private void LoadPreferences(){
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
int savedRadioIndex = sharedPreferences.getInt(KEY_SAVED_RADIO_BUTTON_INDEX, 0);
RadioButton savedCheckedRadioButton = (RadioButton) rg.getChildAt(savedRadioIndex);
savedCheckedRadioButton.setChecked(true);
}
}
and in the prayers activity the textcolor and the background of the layout should change colors to one of the selected choice in the settings activity
prayers.class
SharedPreferences sharedPreferences = getSharedPreferences(
"com.e_orthodoxy.orthodox_prayers", MODE_PRIVATE);
int colors = sharedPreferences
.getInt("KEY_SAVED_RADIO_BUTTON_INDEX", 0);
if (colors == 0) {
textview.setTextColor(getResources().getColor(R.color.Vanilla));
linear.setBackgroundColor(getResources().getColor(R.color.Maroon));
textview.setShadowLayer(0, 0, 0,
(getResources().getColor(R.color.Maroon)));
} else if (colors == 1) {
textview.setTextColor(Color.BLACK);
linear.setBackgroundColor(Color.WHITE);
textview.setShadowLayer(0, 0, 0, Color.BLACK);
} else if (colors == 2) {
textview.setTextColor(Color.WHITE);
linear.setBackgroundColor(Color.BLACK);
textview.setShadowLayer((float) 1.5, 2, 2, Color.WHITE);
}
where is my fault
any help???
You are writing and reading from two different SharedPreferences:
In SettingsActivity:
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
In prayers:
SharedPreferences sharedPreferences = getSharedPreferences(
"com.e_orthodoxy.orthodox_prayers", MODE_PRIVATE);
Notice the first parameter i.e., the name of the SharedPreference file.

Categories

Resources