Sms Gratis Indosat

Phone Numbers
0 -
Example : 83832867987 Without 0

Type your Messages

Question: + =

SharedPreferences.Editor for RadioButton in RadioGroup

Last post demonstrate how to "Read index of the checked RadioButton in RadioGroup" and explain why you cannot save in SharedPreferences.Editor base on IDs. This exercise demonstrate how to save the checked index of RadioButton in RadioGroup using SharedPreferences.Editor.

save the checked index of RadioButton in RadioGroup using SharedPreferences.Editor.


package com.example.androidradiogroup;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.app.Activity;
import android.content.SharedPreferences;

public class MainActivity extends Activity {

RadioGroup radioGroup;
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_main);
radioGroup = (RadioGroup)findViewById(R.id.radiogroup);
radioGroup.setOnCheckedChangeListener(radioGroupOnCheckedChangeListener);

textCheckedID = (TextView)findViewById(R.id.checkedid);
textCheckedIndex = (TextView)findViewById(R.id.checkedindex);

LoadPreferences();
}

OnCheckedChangeListener radioGroupOnCheckedChangeListener =
new OnCheckedChangeListener(){

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(checkedId);
int checkedIndex = radioGroup.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)radioGroup.getChildAt(savedRadioIndex);
savedCheckedRadioButton.setChecked(true);
}
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton
android:id="@+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
<RadioButton
android:id="@+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 3" />
</RadioGroup>

<TextView
android:id="@+id/checkedid"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/checkedindex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

Related:
- Example of using SharedPreferences.Editor; getPreferences() for a single activity.
- Use getSharedPreferences() to retrieve a preferences object shared across multiple activity.


Read index of the checked RadioButton in RadioGroup

This example demonstrate how to get the index of the checked RadioButton in RadioGroup.

Read index of the checked RadioButton in RadioGroup


package com.example.androidradiogroup;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {

RadioGroup radioGroup;
TextView textCheckedID, textCheckedIndex;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup)findViewById(R.id.radiogroup);
radioGroup.setOnCheckedChangeListener(radioGroupOnCheckedChangeListener);

textCheckedID = (TextView)findViewById(R.id.checkedid);
textCheckedIndex = (TextView)findViewById(R.id.checkedindex);

}

OnCheckedChangeListener radioGroupOnCheckedChangeListener =
new OnCheckedChangeListener(){

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(checkedId);
int checkedIndex = radioGroup.indexOfChild(checkedRadioButton);

textCheckedID.setText("checkedID = " + checkedId);
textCheckedIndex.setText("checkedIndex = " + checkedIndex);
}};

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<TextView
android:id="@+id/checkedid"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/checkedindex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton
android:id="@+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
<RadioButton
android:id="@+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 3" />
</RadioGroup>

</LinearLayout>


Please note that you cannot assume the checkedId passed to onCheckedChanged() of OnCheckedChangeListener always same for the same button. Specially when you want to set Preference for RadioButton in RadioGroup. The ADT will re-generate the IDs base on your layout. So, after you modify the layout, the IDs will be changed. I read some posts advise keep preference of RadioButton in RadioGroup base on the checkID. Yes, it work if you will not update your APK in the furture.

The IDs changed after re-layout


Next:
- SharedPreferences.Editor for RadioButton in RadioGroup


Back to Home Screen directly using ACTION_MAIN with category CATEGORY_HOME

If you want to go to Home Screen directly, using the code:


Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);



Python for Android

Python for android is a project to create your own Python distribution including the modules you want, and create an apk including python, libs, and your application.

know more: http://python-for-android.readthedocs.org/

 
Copyright © 2015. About - Web Version - Mobile Version Sitemap - Contact - Privacy