I get an error when trying to set SwitchCompat to action bar.
Here is my abswitch.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/abswitch"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<android.support.v7.widget.SwitchCompat
android:id="@+id/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="" />
</RelativeLayout>
Here my profile_menu.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/myswitch"
android:title=""
app:showAsAction="always"
android:actionLayout="@layout/abswitch"/>
</menu>
And here onCreateOptionsMenu from fragment
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.profile_menu, menu);
MenuItem item = menu.findItem(R.id.myswitch);
item.setActionView(R.layout.abswitch);
SwitchCompat switchCompat = (SwitchCompat) item.getActionView();
super.onCreateOptionsMenu(menu, inflater);
}
Here is error i get everytime i try to launch my app
Process: ru.dennes544.vkapp, PID: 15974
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.support.v7.widget.SwitchCompat
at ru.dennes544.vkapp.Profile.onCreateOptionsMenu(Profile.java:216)
at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:1868)
at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1989)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:276)
at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
at android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
at android.support.v7.widget.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:49)
at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:459)
at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:69)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Thanks in advance.
P.S. sorry for pastebin links, stackoverflow doesnt correctly formatting xml's
EDIT: figured out.
Changed in profile_menu.xml
<item
android:id="@+id/toggleservice"
app:actionViewClass="android.support.v7.widget.SwitchCompat"
app:showAsAction="always"
android:title=""/>
and in Profile.class
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.profile_menu, menu);
MenuItem item = menu.findItem(R.id.toggleservice);
SwitchCompat switchCompat = (SwitchCompat) MenuItemCompat.getActionView(item);
super.onCreateOptionsMenu(menu, inflater);
}
Now its working. Thanks everybody!
item.getActionView()
is a relative layout. Probably, you need to do anotherfindViewById()
on the action view. – Salomie