0

I want some of the menu items to have a different background. For example, I want nav_hesabim's background to be red. I want nav_usersss to be yellow in the background. So I want to use the background of the items in the menu in different colors. I am using NavigationView. How can I do that?

activity_main2_drawer

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single"
        android:id="@+id/group1">
        <item
            android:id="@+id/nav_hesabim"
            android:icon="@mipmap/avatar"
            android:title="@string/menu_hesabim" />

    </group>

    <group android:checkableBehavior="single"
        android:id="@+id/groupsss">
    <item
            android:id="@+id/nav_usersss"
            android:icon="@mipmap/sss3"

            android:title="@string/menu_userSss" />
    </group>
</menu>

Activity_main2

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    android:layoutDirection="rtl">

    <include
        layout="@layout/app_bar_main2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layoutDirection="rtl"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:itemTextColor="@color/white"
        app:itemTextAppearance="@style/NavDrawerTextStyle2"
        android:layout_gravity="start"
        android:background="@color/NavItem"
        android:fitsSystemWindows="true"
        android:theme="@style/NavigationView"
        app:headerLayout="@layout/nav_header_main2"
        app:menu="@menu/activity_main2_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

MainActivity

public class Main2Activity extends AppCompatActivity  {
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
  DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_hesabim, R.id.nav_usersss,R.id.nav_passs, R.id.nav_mutlakaoku,
                R.id.nav_productusage, R.id.nav_message, R.id.nav_productprices,R.id.nav_antrenmanpr,R.id.nav_beslenmepr,R.id.nav_buyinglist,R.id.nav_gelisimupload,R.id.nav_duyuru,R.id.nav_makaleNew,R.id.nav_instagram,R.id.nav_kafein,R.id.nav_BMI)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
}
Giorgos Neokleous
  • 1,709
  • 1
  • 14
  • 25
stakbeko
  • 31
  • 7

1 Answers1

0

Below code works fine for me My NavigationView:-

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    android:background="@color/nav.background"
    app:menu="@menu/menu_drawer"
    app:itemBackground="@drawable/nav_item_background"
    app:itemTextColor="@drawable/nav_item_text"/>

drawable/nav_item_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true" android:drawable="@color/nav.item.background" />
  <item android:state_checked="true" android:drawable="@color/nav.item.background" />
  <item android:state_focused="true" android:drawable="@color/nav.item.background" />
  <item android:state_activated="true" android:drawable="@color/nav.item.background" />
  <item android:drawable="@color/nav.item.background.default" />
</selector>

drawable/nav_item_text.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@android:color/white" android:state_checked="true" />
  <item android:color="#bdbdbd" />
</selector>

Thank you.

  • I do not want the background to change when I select the menu item. I want the background menu of group1 to be red, I want the groupsss to be yellow. I want to use different backgrounds – stakbeko Jan 21 '20 at 09:53
  • Ok you have to use this way. Check below link:- https://stackoverflow.com/questions/31221637/android-navigation-view-item-menu-background-color – Pritesh Parmar Jan 21 '20 at 10:01