0

In android, I want to set nav menu dynamically according to user. some user has 3 menus and another usr has only one menu and so on.... how can I set navigation menu dynamically? the setting of the nav menu is written in xml file. I want to set the nav menu in class file and use show and hide the menu

class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var navController: NavController
    private lateinit var drawerLayout: DrawerLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setupNavigationDrawer()
        setSupportActionBar(toolbar)

        navController = Navigation.findNavController(this,
            R.id.nav_fragment
        )
        appBarConfiguration = AppBarConfiguration
            .Builder(R.id.homeFragment, R.id.secondFragment)
            .setDrawerLayout(drawerLayout)
            .build()
        nav_fragment.navGraphViewModels<>()

        setupActionBarWithNavController(navController, appBarConfiguration)

        navigation_view.setupWithNavController(navController)

    }
    override fun onSupportNavigateUp(): Boolean {
        return findNavController(R.id.nav_fragment).navigateUp(appBarConfiguration)
                || super.onSupportNavigateUp()
    }

    private fun setupNavigationDrawer() {
        drawerLayout = (findViewById<DrawerLayout>(R.id.drawer_layout))
            .apply {
                setStatusBarBackground(R.color.colorPrimaryDark)
            }
    }
}
<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"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/AppTheme.AppBarOverlay">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

        </com.google.android.material.appbar.AppBarLayout>

        <fragment
            android:id="@+id/nav_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/nav_graph"/>

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation_view"

        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/nav_menu"/>

</androidx.drawerlayout.widget.DrawerLayout>
jakchang
  • 402
  • 5
  • 13

1 Answers1

0

Make a custom navigation view like below and change Visibility of view according to your condition.

 <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true" >

        <LinearLayout
            android:background="#FFFFFF"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

                        <RelativeLayout
                            android:id="@+id/layout_home"
                            android:foreground="?android:attr/selectableItemBackground"
                            android:orientation="horizontal"
                            android:layout_width="match_parent"
                            android:layout_height="@dimen/dimen_48dp">

                            <ImageView
                                android:id="@+id/nav_home"
                                app:srcCompat="@drawable/ic_home"
                                android:layout_width="@dimen/setting_icon"
                                android:layout_height="@dimen/setting_icon"
                                android:layout_marginLeft="@dimen/padding_double"
                                android:layout_marginRight="@dimen/padding_normal"
                                android:layout_centerVertical="true"
                                android:clickable="false"
                                android:focusable="false"
                                android:tint="@color/color_f85200"
                                android:layout_gravity="center_vertical"/>
                            <TextView
                                android:layout_centerVertical="true"
                                android:layout_toRightOf="@+id/nav_home"
                                android:text="@string/title_activity_home"
                                android:textSize="16sp"
                                android:clickable="false"
                                android:focusable="false"
                                android:fontFamily="@font/sarabun_medium"
                                android:layout_marginLeft="@dimen/padding_double"
                                android:layout_gravity="center_vertical"
                                android:textColor="@color/DrawerTextColor"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content" />
                        </RelativeLayout>
               </LinearLayout>
    </com.google.android.material.navigation.NavigationView>
Praful Korat
  • 374
  • 4
  • 22