I have NavigationView
with menus.
I am able to get MenuItem
from navigationView
by using
MenuItem menuItem = navigationView.getMenu().getItem(0);
LinearLayout linearLayout = (LinearLayout) menuItem;
I want to cast menuItem
to LinearLayout
or View.
I have set android:actionViewClass
to menu
in menu.xml :
<item
android:id="@+id/nav_backup"
android:icon="@drawable/ic_sd_card_black_24dp"
android:actionViewClass="android.widget.LinearLayout"
android:title="@string/backup_configuration" />
While casting I am facing NullPointerException
. How can I cast this?
Try this first set custom layout to your
MenuItem
than cast it toLinearLayout
like below codeEDIT
setActionView()
andgetActionView()
method was deprecated in API level 26.1.0.Direct use
MenuItem.setActionView()
andMenuItem.getActionView()
SAMPLE CODE