Parth Gargava
Parth Gargava

Reputation: 43

Implement bottom drawer in android Java

I am unable to implement a bottom drawer in android (java) and can not find any working example/tutorial on its usage. Can you write sample code for using a bottom drawer? (https://material.io/components/navigation-drawer/#bottom-drawer)

Alternatively, I tried using a drop down menu but my app needs a bottom drawer only

<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navbottom"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:layout_gravity="bottom"
        app:menu="@menu/menu_nav">    </com.google.android.material.bottomnavigation.BottomNavigationView>```

Here's the code to my bottom navigation view

This is how it should look like1

Upvotes: 0

Views: 2217

Answers (1)

Blind Kai
Blind Kai

Reputation: 524

In the screenshot that you added, I saw something that looks like BottomSheet. To get this look of dialog you probably want to use BottomSheetDialogFragment so below I will explain how to implement it inside your Activity.

1) First of all, you need to create a class that will extend from BottomSheetDialogFragment and inflate the layout that will be used by this fragment.

public class ExampleBottomSheetDialog extends BottomSheetDialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
savedInstanceState) {
        return inflater.inflate(*R.layout.bottom_sheet_layout*, container, false);
    }
}

2) Then you need to create the *R.layout.bottom_sheet_layout* layout file that will hold needed views and provide logic for them if it's needed.

3) After that, you can programmatically set Dialog logic. So for example, you could open this dialog by pressing the button.

Button buttonDialogBottomSheet = findViewById(R.id.btn_sh_dialog);
buttonDialogBottomSheet.setOnClickListener((v) -> {
    ExampleBottomSheetDialog bottomSheetDialog = new ExampleBottomSheetDialog();
    bottomSheetDialog.show(getSupportFragmentManager(), "simple tag");
});

If you're looking for standard Bottom Sheet just let me know, I will update the answer. Result of code written above: link

Upvotes: 1

Related Questions