java - Toggle between a RadioGroup of Toggle Buttons -
so, have buttons lined below:
here's image of i'm trying achieve:
in image, first row row of buttons no button pressed. in sec row, first button pressed. in 3rd row, clicked on 4 , 4th button pressed (not first 1 anymore).
<radiogroup android:id="@+id/togglegroup" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:usedefaultmargins="true" android:layout_column="0" android:columncount="4" android:rowcount="1" android:orientation="horizontal" > <togglebutton android:id="@+id/number_zero" android:layout_width="34sp" android:layout_height="40sp" android:texton="@string/number_zero" android:textoff="@string/number_zero" android:onclick="ontoggle" android:checked="true" android:background="@drawable/psc_number_color" android:layout_margin="5sp" /> <togglebutton android:id="@+id/number_one" android:layout_width="34sp" android:layout_height="40sp" android:texton="@string/number_one" android:textoff="@string/number_one" android:onclick="ontoggle" android:checked="true" android:background="@drawable/psc_number_color" android:layout_margin="5sp" /> <togglebutton android:id="@+id/number_two" android:layout_width="34sp" android:layout_height="40sp" android:texton="@string/number_two" android:textoff="@string/number_two" android:background="@drawable/psc_number_color" android:layout_margin="5sp" /> </radiogroup>
i wondering if possible toggle between these 5 buttons in such way if pressed 1 down, button remain pressed. if click on button in group, button clicked on before depress , new button clicked on pressed down.
trying within pageradapter:
public void ontoggle(view view) { ((radiogroup)view.getparent()).check(view.getid()); // app specific stuff .. } public object instantiateitem(viewgroup parent, int position) { 1 = (togglebutton) view.findviewbyid(r.id.number_one); 2 = (togglebutton) view.findviewbyid(r.id.number_two); 3 = (togglebutton) view.findviewbyid(r.id.number_three); 4 = (togglebutton) view.findviewbyid(r.id.number_four); final radiogroup.oncheckedchangelistener togglelistener = new radiogroup.oncheckedchangelistener() { @override public void oncheckedchanged(final radiogroup radiogroup, final int i) { (int j = 0; j < radiogroup.getchildcount(); j++) { final togglebutton view = (togglebutton) radiogroup.getchildat(j); view.setchecked(view.getid() == i); } } }; one.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { one.toggle(); } }); ((radiogroup) view.findviewbyid(r.id.togglegroup)).setoncheckedchangelistener(togglelistener); }
try this: have done same things checkbox [ custom checkbox ].
@override public void onclick(view view) { checkbox cb = (checkbox) view; if(cb.ischecked()){ linearlayout lllayout = (linearlayout) cb.getparent(); for(int i=0; i<((viewgroup)lllayout).getchildcount(); ++i) { view nextchild = ((viewgroup)lllayout).getchildat(i); if(nextchild instanceof checkbox && nextchild.getid()==cb.getid() ){ }else if (nextchild instanceof checkbox && nextchild.getid()!=cb.getid() ){ checkbox cb2=(checkbox) nextchild; cb2.setchecked(false); } } } else{ linearlayout lllayout = (linearlayout) cb.getparent(); for(int i=0; i<((viewgroup)lllayout).getchildcount(); ++i) { view nextchild = ((viewgroup)lllayout).getchildat(i); if(nextchild instanceof checkbox && nextchild.getid()==cb.getid() ){ checkbox cb2=(checkbox) nextchild; cb2.setchecked(false); }else if (nextchild instanceof checkbox && nextchild.getid()!=cb.getid() ){ checkbox cb2=(checkbox) nextchild; cb2.setchecked(false); } } } }
my xml:
<linearlayout android:id="@+id/llshift" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="4" android:gravity="right" android:orientation="horizontal" > <checkbox android:id="@+id/cbmorning" android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@drawable/custom_radio_button_morning" android:paddingbottom="5dp" android:paddingright="3dp" android:paddingtop="5dp" /> <checkbox android:id="@+id/cbevning" android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@drawable/custom_radio_button_evening" android:paddingbottom="5dp" android:paddingright="5dp" android:paddingtop="5dp" /> </linearlayout>
custom_radio_button_morning.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/morning_chk" android:state_checked="true"/> <item android:drawable="@drawable/morning_unchk" android:state_checked="false"/> </selector>
java android button toggle
No comments:
Post a Comment