Bottom sheet in android | Android Tutorial 2020 Updated

Hello world, today we are going to see how to make a
persistence bottom sheet and how to make a
modal bottom sheet in the android. The bottom sheet has two
types, one is persistence and the second is a modal sheet. In both cases, the
bottom sheet slides up. In the persistence bottom sheet, some parts of the bottom sheet showing to the user so that the user can slides up and in the
model bottom sheet you can make a trigger to show the bottom sheet and it is
fully hidden unlikely persistence bottom sheet. When a user clicks on a button
or something you can trigger the modal bottom sheet to show. Let’s see the
finished app which we are going to make below.

bottom sheet android

Before making any type of bottom sheet we must add the dependency.

Add Bottom Sheet Dependency in Android Project

Let’s first make a persistence bottom sheet.

How to make Persistence Bottom Sheet

First, we need to make our layout for the
bottom sheet
so let’s do this.

Make a new layout file in the layout folder and name it as bottom_sheet.xml.

You can make your layout as you want for the sake of simplicity we make a
simple view with textview but you can add what you like for example you can
add recyclerview or any other view you like.

If you want to make this layout to be used as a bottom sheet then you have to
add these 3 properties to your layout.

  • app:layout_behavior, set this property value
    because we want our layout to behave like a bottom sheet.
  • app:behavior_hideable, set the property value to true as we want to
    completely hide the bottom sheet if we swipe it down again when it is
  • app:behavior_peekHeight, set this property value to the value which
    you want to show the view when it is collapsed.

After making the layout for the bottom sheet we need to include this layout
in our activity layout and make activity layout root layout
to CoordinatorLayout. It is mandatory for the bottom sheet.

After changing the layout let’s add the bottom sheet layout in the activity

Our full code will look like this.

Now if you run your app you can see the persistence bottom sheet fully

Now let’s see how we can access our bottom sheet and change the opening and
closing of the bottom sheet in the java.

Open or close the Bottom Sheet in Java

To open or close the bottom sheet we will make a button in the activity layout
xml file to toggle the bottom sheet.

Now come to the activity java file and add this code.

We can use this code for the modal bottom sheet also.

Now let’s see how to make a modal bottom sheet.

How to make Modal Bottom Sheet in Android

making the modal bottom sheet is fairly simple. Add this below code in the
activity java file. We are using the same layout as we made before for the
persistence bottom sheet.

We have added a button to the activity layout to trigger the modal bottom
sheet and in the click method, we will add this code.

If you click outside of the modal bottom sheet then it will slide down hide.

Now run your app see your bottom sheet.

Implement Callback of Bottom Sheet

If you learned something new then do share this article with your friends
and batchmates and don’t forget to subscribe to our newsletter if you
haven’t joined already for the latest updates.

Thanks for reading have a nice day.


Source link