I have a drawer menu like this. It has more than one item but we can assume all the same. Each one is an activity. I can change the activity from there. But I want to highlight background of the current activity. How can I do that?
Thanks for your help.
Example screenshot:
Imgur link
<?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"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="@+id/miHome"
android:title="@string/menu_home" />
</menu>
activity_main.xml:
<com.google.android.material.navigation.NavigationView
android:id="@+id/navView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_drawer_menu" />
2
Answers
This is activity_main.xml:
This is MainActivity.kt:
This is app_bar_menu.xml:
nav_drawer_menu.xml:
If you are trying to select an Item of the Navigation drawer then you can try out using this:
The getItem(int index) method gets the MenuItem then you can call the
setChecked(true);
on the menu item.You can also highlight the item using this:
Here is the reference