How to keep state of RadioButton in Android? -
hi i'm trying develop application runs every interval time, lets every 1 minute display toast message
.
but problem i'm using radiobutton
functionality perfect when tap on 1 radio button green, when close , re-open activity i'll none of radio buttons selected.
here mainactivity.java
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } public void onradiobuttonclicked(view view) { // button checked? boolean checked = ((radiobutton) view).ischecked(); // check radio button clicked switch(view.getid()) { case r.id.radio_one_min: if (checked) { //some code } break; case r.id.radio_ten_min: if (checked) { //some code } break; case r.id.radio_disable: if (checked) { //some code } break; } } }
and here activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <radiogroup xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/radio"> <radiobutton android:id="@+id/radio_disable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="disable" android:onclick="onradiobuttonclicked"/> <radiobutton android:id="@+id/radio_one_min" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1 minute" android:onclick="onradiobuttonclicked"/> <radiobutton android:id="@+id/radio_ten_min" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="10 minute" android:onclick="onradiobuttonclicked"/> </radiogroup>
please me solve riddle.
thanks in advance...
this code useful store ratingbar state, when start new activity, see previous rating state..
package com.example.ratingbar; import android.os.bundle; import android.preference.preferencemanager; import android.app.activity; import android.content.sharedpreferences; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.ratingbar; import android.widget.ratingbar.onratingbarchangelistener; import android.widget.textview; import android.widget.toast; public class ratingbarmainactivity extends activity { ratingbar ratingbarclick; button sub_btn; textview textratingview , textratingviewsave; boolean val = true; float ans = (float) 0.0; //-------------------------------------------------------------------------------------------- @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_ratingbar_main); ratingbarclick = (ratingbar) findviewbyid(r.id.ratingbar1); ratingbarclick.setonratingbarchangelistener(rateobj); sharedpreferences sharepref = preferencemanager.getdefaultsharedpreferences (ratingbarmainactivity.this); ans = sharepref.getfloat("get_rating", 0.0f); system.out.println("--------------------------------------ans = " + ans); if(val) { ratingbarclick.setrating(ans); } else { ratingbarclick.setrating(ans); } textratingview = (textview) findviewbyid(r.id.ratingview); } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- ratingbar.onratingbarchangelistener rateobj = new ratingbar.onratingbarchangelistener() { @override public void onratingchanged(ratingbar ratingbar, float rating,boolean fromuser) { //textratingview.settext(string.valueof(rating)); ans = ratingbarclick.getrating(); sharedpreferences sharepref = preferencemanager.getdefaultsharedpreferences (ratingbarmainactivity.this); sharedpreferences.editor edit = sharepref.edit(); edit.putfloat("get_rating", ans); edit.commit(); val = false; } }; //-------------------------------------------------------------------------------------------- } --------------------------------------------------------------------------------------------------- activity_ratingbar_main.xml file <relativelayout 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" > <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/ratingbar1" android:layout_alignparenttop="true" android:layout_marginleft="15dp" android:layout_margintop="23dp" android:text="select rating bar here" tools:context=".ratingbarmainactivity" /> <ratingbar android:id="@+id/ratingbar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_marginleft="21dp" android:layout_margintop="63dp" /> <textview android:id="@+id/ratingview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/ratingbar1" android:text="textview" /> <button android:id="@+id/button1" style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_centervertical="true" android:text="click save rating in textbox" /> </relativelayout>
Comments
Post a Comment