Fragment Activity that contains RadioGroup having 5 RadioButtons and a Button.RadioButton5 have an EditText with it.
public void onCreate(Bundle savedInstanceState) {
radio1 = PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("radio1", false);
radio2 = PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("radio2", false);
radio3 = PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("radio3", false);
radio4 = PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("radio4", false);
radio5 = PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("radio5", false);
str_rbText = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("Selected_rb_msg", "nomi");
msgToSend = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("msgToSend", "");
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
viewRoot = inflater.inflate(R.layout.fragment_messages, container, false);
radioGroup = (RadioGroup) viewRoot.findViewById(R.id.radio_grp);
rb1 = (RadioButton) viewRoot.findViewById(R.id.rb_msg1);
rb2 = (RadioButton) viewRoot.findViewById(R.id.rb_msg2);
rb3 = (RadioButton) viewRoot.findViewById(R.id.rb_msg3);
rb4 = (RadioButton) viewRoot.findViewById(R.id.rb_msg4);
rb5 = (RadioButton) viewRoot.findViewById(R.id.rb_customMsg);
editText = (EditText) viewRoot.findViewById(R.id.et_customMsg);
if(radio1){
rb1.setChecked(true);
}
else if(radio2){
rb2.setChecked(true);
}
else if(radio3){
rb3.setChecked(true);
}
else if(radio4){
rb4.setChecked(true);
}
else if(radio5){
rb5.setChecked(true);
editText.setVisibility(View.VISIBLE);
editText.setHint(PreferenceManager.getDefaultSharedPreferences(getActivity()).ge
tString("Selected_rb_msg", ""));
}
function();
return viewRoot;
}
public void function(){
SharedPreferences sp =
PreferenceManager.getDefaultSharedPreferences(getActivity());
final SharedPreferences.Editor editor = sp.edit();
if(rb1.isChecked() && !rb5.isChecked() && !rb2.isChecked() &&
!rb3.isChecked() && !rb4.isChecked()){
editText.setVisibility(View.INVISIBLE);
}
radioGroup.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.rb_msg1){
str_rbText = rb1.getText().toString();
editText.setVisibility(View.INVISIBLE);
editText.setText("");
//editor.putBoolean("radio1", true).apply();
} else if (checkedId == R.id.rb_msg2){
str_rbText = rb2.getText().toString();
editText.setVisibility(View.INVISIBLE);
editText.setText("");
//editor.putBoolean("radio2", true).apply();
} else if (checkedId == R.id.rb_msg3){
str_rbText = rb3.getText().toString();
editText.setVisibility(View.INVISIBLE);
editText.setText("");
//editor.putBoolean("radio3", true).apply();
} else if (checkedId == R.id.rb_msg4){
str_rbText = rb4.getText().toString();
editText.setVisibility(View.INVISIBLE);
editText.setText("");
//editor.putBoolean("radio4", true).apply();
} else if (checkedId == R.id.rb_customMsg){
editText.setVisibility(View.VISIBLE);
editText.setHint("Enter your custom message");
str_rbText = editText.getEditableText().toString().trim();
}
});
onClicking button the selected radio button state should save in SharedPreferences and load on starting the activity
Button btnSet = (Button) viewRoot.findViewById(R.id.btn_set_msgFrag);
btnSet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("SelectedRadioButton",str_rbText);
int id = radioGroup.getCheckedRadioButtonId();
if(id == R.id.rb_msg1){
editor.putBoolean("radio1", true).apply();
}else if(id == R.id.rb_msg2){
editor.putBoolean("radio2", true).apply();
}else if(id == R.id.rb_msg3){
editor.putBoolean("radio3", true).apply();
}else if(id == R.id.rb_msg4){
editor.putBoolean("radio4", true).apply();
}else if(id == R.id.rb_customMsg){
editor.putBoolean("radio5", true).apply();
}
}
});
}
}
RadioButton state is not stored correctly. The state of 1st RadioButton is always true on getting when calling this activity.
When putting one state of the selected radioButton put false in other radioButtons.