Friday, March 10, 2017

Android Bottom Sheet Example - Android Support Library Tutorial

Bottom Sheets is excellent user experience design component when comparing long press dialog pop-up box, context menu where the user can consume options easily from the bottom with attractive and meaningful user interface. So, let us dive into the implementation of Bottom Sheets in the Android application using official android support library.

Android Bottom Sheet Example - Android Support Library Tutorial


  1. Android Studio and SDK installed - How to install android studio
  2. Bottom Navigation Drawer Tutorial - If you're interested check out this article

Getting Started:

Add support library dependency in build.gradle file and sync it via android studio

compile ''
compile ''    
Use gradle sync to download all the support libraries and import to your project.

Adding Layout: 

In order to add Bottom sheets to the activity, NestedScrollView is used with additionally bottom sheet behaviour with other options. check out the layout xml file below.

<?xml version="1.0" encoding="utf-8"?>
< xmlns:android=""





Bottom Sheet behavior is adopted for the nested scroll view with attribute named as app:layout_behavior which implements the bottom sheet behavior.

Java Implementation:

We'll try to add Bottom Sheet dialog box behavior in this tutorial, simply extend your fragment class with BottomSheetDialogFragment and start implementing the onCreateView method for building the fragment user interface, check the code below for understanding.

public class BottomDialogFragment extends BottomSheetDialogFragment {
    public static BottomDialogFragment getInstance() {
        return new BottomDialogFragment();

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.bottom_sheet_fragment, container, false);
        ((TextView)view.findViewById("Elon Musk");
        return view;

Thus this will help in creating fragment and inflating into the bottom sheet view, now let us see how to control the bottom sheet from our activity by responding to user clicks. Since i've used Recyclerview added click event and binding the bottom sheet behavior with recyclerview click event.

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private BottomSheetBehavior mBottomSheetBehavior;

    protected void onCreate(Bundle savedInstanceState) {
        mRecyclerView = (RecyclerView) findViewById(;
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

        View bottomSheet = findViewById( );
        mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
        mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            public void onStateChanged(View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_COLLAPSED) {

            public void onSlide(View bottomSheet, float slideOffset) {

        ContactsAdapter contactsAdapter = new ContactsAdapter(generateData(),mBottomSheetBehavior,getSupportFragmentManager());


Bottom Sheet behavior based on Recyclerview click Event:

Now let us map recyclerview click event with bottom sheet behavior event, so that whenever it is clicked bottom sheet opens up with transition from down. check the Recyclerview adapter code for detailed understanding.

    public void onBindViewHolder(ContactsHolder holder, int position) {
        ContactsModal contact = mContactsModals.get(position);

        holder.mLinearLayout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                BottomDialogFragment bottomSheetDialog = BottomDialogFragment.getInstance().getInstance();
      , "Custom Bottom Sheet");

Bottom Sheet Useful Methods:

  1. - To open the Bottom sheet dialog from hidden state to expanded state
  2.  BottomSheetBehavior.setState() - To set the state of the bottom sheet behavior from java code based on events.
  3. BottomSheetBehavior.setPeekHeight(int) - To set the Peek Height of the bottom sheet from the bottom.  
For full working sample, follow the Github repo. Just clone and open in android studio to execute the bottom navigation drawer view android example application. This Bottom Sheet view is bundled with Android support library officially and it's worth trying out in your application.

Hope you have enjoyed the post, post you own thoughts, additional points as comments. For complete code, projects, hugs/bugs just drop me mail/ chat in Facebook/Google+. share is care.


Post a Comment

feel free to post your comments! Don't Spam here!