SwitchCompat in actionbar, with fragment
Asked Answered
S

2

11

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!

Selinaselinda answered 22/12, 2014 at 17:55 Comment(4)
item.getActionView() is a relative layout. Probably, you need to do another findViewById() on the action view.Salomie
@S.D. if not hard for you, can u explain more? im novice in android progamming...Selinaselinda
Please post an answer as opposed to editing the question.Metasomatism
That works for me. But what if i want to add a text below switchcompat?Estovers
A
0

Instead of:

SwitchCompat switchCompat = (SwitchCompat) MenuItemCompat.getActionView(item);

You should do:

RelativeLayout relativeLayout= (RelativeLayout) MenuItemCompat.getActionView(item);
SwitchCompat switchCompat = (SwitchCompat) relativeLayout.findViewById(R.id.switchForActionBar);

Or so

Artis answered 28/10, 2015 at 12:24 Comment(0)
H
0

MenuItemCompat.getActionView(item) is deprecated hence use MenuItem.getActionView

use it like this

@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) item.getActionView();
    super.onCreateOptionsMenu(menu, inflater);
}
Halinahalite answered 19/10, 2021 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.