android - On clicking button the selected RadioButton state in RadioGroup should saved in SharedPreferences using Fragments -
fragment activity contains radiogroup having 5 radiobuttons , button.radiobutton5 have edittext 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 layout 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 custom message"); str_rbtext = edittext.geteditabletext().tostring().trim(); } });
onclicking button selected radio button state should save in sharedpreferences , load on starting 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 not stored correctly. state of 1st radiobutton true on getting when calling activity.
when putting 1 state of selected radiobutton put false in other radiobuttons.
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(); editor.putboolean("radio2", false).apply(); editor.putboolean("radio3", false).apply(); editor.putboolean("radio4", false).apply(); editor.putboolean("radio5", false).apply(); } else if (checkedid == r.id.rb_msg2){ str_rbtext = rb2.gettext().tostring(); edittext.setvisibility(view.invisible); edittext.settext(""); editor.putboolean("radio2", true).apply(); editor.putboolean("radio1", false).apply(); editor.putboolean("radio3", false).apply(); editor.putboolean("radio4", false).apply(); editor.putboolean("radio5", false).apply(); } else if (checkedid == r.id.rb_msg3){ str_rbtext = rb3.gettext().tostring(); edittext.setvisibility(view.invisible); edittext.settext(""); editor.putboolean("radio3", true).apply(); editor.putboolean("radio1", false).apply(); editor.putboolean("radio2", false).apply(); editor.putboolean("radio4", false).apply(); editor.putboolean("radio5", false).apply(); } else if (checkedid == r.id.rb_msg4){ str_rbtext = rb4.gettext().tostring(); edittext.setvisibility(view.invisible); edittext.settext(""); editor.putboolean("radio4", true).apply(); editor.putboolean("radio1", false).apply(); editor.putboolean("radio2", false).apply(); editor.putboolean("radio3", false).apply(); editor.putboolean("radio5", false).apply(); } else if (checkedid == r.id.rb_custommsg){ edittext.setvisibility(view.visible); edittext.sethint("enter custom message"); //if(edittext.length() == 0){ // toast.maketext(getcontext(), "visible", toast.length_short).show(); //} str_rbtext = edittext.geteditabletext().tostring().trim(); editor.putboolean("radio5", true).apply(); editor.putboolean("radio1", false).apply(); editor.putboolean("radio2", false).apply(); editor.putboolean("radio3", false).apply(); editor.putboolean("radio4", false).apply(); //edittext.setbackgrounddrawable(null); //to remove underline of edittext } editor.putstring("msg2send", str_rbtext).apply(); } });
Comments
Post a Comment