Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Drawer in bottomappbar android

enter image description here

class BottomNavigationDrawerFragment: BottomSheetDialogFragment(), 
   NavigationView.OnNavigationItemSelectedListener {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_bottomsheet, container, false)
    }

    override fun onNavigationItemSelected(item : MenuItem): Boolean {
        // Bottom Navigation Drawer menu item clicks
        when (item.itemId) {
            R.id.nav1 -> context!!.toast("oneeeeee")
            R.id.nav2 -> context!!.toast("twoooooo")
            R.id.nav3 -> context!!.toast("threeeee")

            return true
        }
        // Add code here to update the UI based on the item selected
        // For example, swap
    }
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        navigation_view.setNavigationItemSelectedListener(this)

        // Add code here to update the UI based on the item selected
        // For example, swap
    }
}

// This is an extension method for easy Toast call
fun Context.toast(message: CharSequence) {
    val toast = Toast.makeText(this, message, Toast.LENGTH_SHORT)
    toast.setGravity(Gravity.BOTTOM, 0, 600)
    toast.show()
}

What I want to achieve is something given in image. I want to make a navigation drawer in bottom app bar. Above code doesn't work and it tells unresolved reference type setNavigationItemSelectedListener. What is the error in my code?

like image 361
Vipin Dubey Avatar asked Sep 27 '18 05:09

Vipin Dubey


People also ask

Where is the navigation drawer?

The user can view the navigation drawer when the user swipes a finger from the left edge of the activity. They can also find it from the home activity by tapping the app icon in the action bar. The drawer icon is displayed on all top-level destinations that use a DrawerLayout.

What is navigation drawer activity in Android?

The navigation drawer is a UI panel that shows your app's main navigation menu. The drawer appears when the user touches the drawer icon in the app bar or when the user swipes a finger from the left edge of the screen.

Why do we use the navigation drawer in Android app?

Android Navigation Drawer is a sliding left menu that is used to display the important links in the application. Navigation drawer makes it easy to navigate to and fro between those links. It's not visible by default and it needs to opened either by sliding from left or clicking its icon in the ActionBar.

What is Android drawer layout?

DrawerLayout acts as a top-level container for window content that allows for interactive "drawer" views to be pulled out from one or both vertical edges of the window.


Video Answer


2 Answers

you should add a drawer icon in your bottomAppbar, then use a bottomsheet for the drawer.

for your drawer you have two choices:

1- go with google standards and add drawer items in menu folder (seems you dont want this)

2- replace a fragment in your bottom sheet, in this way you can customize your fragment and do whatever you like

------------------- replace a fragment in your bottom sheet -------------

your activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:custom="http://schemas.android.com/tools"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layoutDirection="ltr"
        android:background="@color/white"
        android:orientation="vertical">

            <FrameLayout
                android:id="@+id/bottom_sheet"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="false"
                android:elevation="6dp"
                android:visibility="visible"
                app:layout_behavior="@string/bottom_sheet_behavior">

                 <FrameLayout
                    android:id="@+id/menu"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" />
            </FrameLayout>

  </androidx.coordinatorlayout.widget.CoordinatorLayout>

your Activity.java

public class Activity extends AppCompatActivity implements 
FragmentNavigation.OnFragmentInteractionListener {
    private CoordinatorLayout coordinatorLayout;
    private View bottomSheet;
    private BottomSheetBehavior<View> behavior;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        FrameLayout bottomSheetLayout = (FrameLayout) 
        findViewById(R.id.menu);

        FragmentNavigation fragmentNavigation = new FragmentNavigation();

        androidx.fragment.app.FragmentTransaction fragmentTransaction = 
        getSupportFragmentManager().beginTransaction();

        fragmentTransaction.replace(bottomSheetLayout.getId(), 
        fragmentNavigation, "k");

        fragmentTransaction.commit();


        coordinatorLayout = (CoordinatorLayout) 
        findViewById(R.id.main_content);
        bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet);
        behavior = BottomSheetBehavior.from(bottomSheet);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}

your fragment navigation

    public class FragmentNavigation extends androidx.fragment.app.Fragment {
    private String descriptions;

    public FragmentNavigation () {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view = inflater.inflate(R.layout.fragment_navigation, container, false);

        return view;
    }
}

your fragment_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:custom="http://schemas.android.com/tools"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical">

                <TextView
                    android:id="@+id/bottom_sheet"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:clipToPadding="false"
                    android:elevation="6dp"
                    android:visibility="visible"
                    android:text="here is the navigation menu"
                    app:layout_behavior="@string/bottom_sheet_behavior"/>



      </LinearLayout>
like image 133
Mohamad Rostami Avatar answered Oct 10 '22 18:10

Mohamad Rostami


See this code it has a navigationIcon attribute but you can use as a bottom app bar. If you need navigation drawer on this click then you have to customize by our own.

<com.google.android.material.bottomappbar.BottomAppBar
  android:id="@+id/bottom_app_bar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="bottom"
  app:elevation="5dp"
  android:elevation="5dp"
  app:fabAttached="true"
  app:fabCradleDiameter="0dp"
  app:backgroundTint="@color/colorPrimary"
  app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
  app:fabAlignmentMode="center"
  app:menu="@menu/bottom_bar_menu"/>

In res>menu>bottom_bar_menu, change showAsAction to always or ifRoom, put an icon for action_settings and remove orderInCategory

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:showAsAction="always"
        android:icon="" />
    <item
        android:title="@string/search"
        android:id="@+id/search"
        android:icon="@drawable/ic_search_black_24dp"
        android:showAsAction="always" />

    <item
        android:id="@+id/app_bar_archieve"
        android:icon="@drawable/ic_bottom_bar_hamburger" // navigation icon
        android:title="@string/action_archieve"
        app:showAsAction="ifRoom"/>
</menu>

in java :

BottomAppBar bar = (BottomAppBar) findViewById(R.id.bar);
setSupportActionBar(bar);

bar.setNavigationOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // Handle the navigation click by showing a BottomDrawer etc.
    }
});

bar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        // Handle actions based on the menu item
        return true;
    }
});

Reference Link : https://material.io/develop/android/components/bottom-app-bar/

like image 42
Abhinav Gupta Avatar answered Oct 10 '22 18:10

Abhinav Gupta