Android: Enable button if checkbox in TableRow is checked -
i'm having 1 button
add_connection
having checkbox
created dynamically on tablerow
.
i want enable add_connection
if checkbox
checked, when using code button
enable disable works fine,but if checked cb1 , cb2 button enabled if unchecked cb2 , cb1 still selected disables button
click done.
here
cb: check box
add_connection: button
cb.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { add_connections.setenabled(ischecked); } });
import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.util.log; import android.widget.button; import android.widget.checkbox; import android.widget.compoundbutton; import java.util.arraylist; public class mainactivity extends appcompatactivity { checkbox cb1; checkbox cb2; checkbox cb3; button button; arraylist<checkbox> checkboxeslist = new arraylist<>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); cb1 = (checkbox) findviewbyid(r.id.cb1); cb2 = (checkbox) findviewbyid(r.id.cb2); cb3 = (checkbox) findviewbyid(r.id.cb3); button = (button) findviewbyid(r.id.button); button.setenabled(false); cb1.settag("cb1"); cb2.settag("cb2"); cb3.settag("cb3"); checkboxeslist.add(cb1); checkboxeslist.add(cb2); checkboxeslist.add(cb3); checkboxlistener listener = new checkboxlistener(checkboxeslist, button); (checkbox checkbox : checkboxeslist) { checkbox.setoncheckedchangelistener(listener); } } public static class checkboxlistener implements compoundbutton.oncheckedchangelistener { arraylist<checkbox> list; button button; public checkboxlistener(arraylist<checkbox> checkboxeslist, button btn) { list = checkboxeslist; button = btn; } private boolean checkstate() { (checkbox checkbox : list) { if (checkbox.ischecked()) { return true; } } return false; } @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { button.setenabled(checkstate()); log.d("tag", "button is: " + checkstate()); } } } , layout <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.thomsonreuters.alexandrugoja.myapplication.mainactivity"> <checkbox android:id="@+id/cb1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <checkbox android:id="@+id/cb2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <checkbox android:id="@+id/cb3" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout>
Comments
Post a Comment