android - How to get the checked state of Switch from menu items -
i have added switch in menu items , trying access checked state.but not able access checked state.can 1 me out.
this menu_crime.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/crime_toggle" android:title="crime" android:enabled="true" app:showasaction="always" app:actionlayout="@layout/toolbar_crime_toggle" /> </menu>
this toolbar_crime_toggle.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <android.support.v7.widget.switchcompat android:id="@+id/crime_switch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:theme="@style/scbswitch" android:padding="10dp" android:text="crime" android:switchpadding="50dp"/> </relativelayout>
i tried access checked state in oncreat() this
mswitchshowsecure = (switchcompat) menu.finditem(r.id.crime_toggle).getactionview().findviewbyid(r.id.crime_switch); mswitchshowsecure.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { log.d("ischecked", string.valueof(ischecked)); } });
you can find checked state of switchcompat anywhere using switchcompat.ischecked() method.
if gives true means switch on otherwise off.
Comments
Post a Comment