Design

Modal Bottom Sheet in Androidx with Examples - Working with BottomSheet

A user can view the full Bottom Sheet by dragging the sheet up vertically. Bottom Sheets are a lesser known part of the Design support library.

2022-04-21
Share this

For our Demo app, we are going to create bottom sheets in a single activity. We will create a fragment for Bottomsheet where you can code for bottomsheet xml file. I have a random pic for design in mainactivity. You will get the full source code if you download the project from the download button below. Let's start with the coding. Here we will code for simple activity_main. 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:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:layout_weight="1">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/randompic"/>


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/content1"
            android:gravity="center"
            android:textSize="@dimen/text18"
            android:padding="@dimen/distance10"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/content3"
            android:gravity="center"
            android:letterSpacing="0.1"
            android:textSize="@dimen/text18"
            android:padding="@dimen/distance10"/>

        <Button
            android:id="@+id/bottm_sheet_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/black"
            android:text="@string/content2"
            android:textSize="@dimen/text18"
            android:textColor="@color/white"/>




    </LinearLayout>

</LinearLayout>

Now create another xml file named layout_bottom_sheet.xml Here you will design for bottom sheet.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="@dimen/distance20"
        android:gravity="center_horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_margin="@dimen/distance20">

            <ImageView
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:src="@drawable/randompic"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/content1"
                android:gravity="left"
                android:textSize="@dimen/text18"
                android:padding="@dimen/distance10"/>

        </LinearLayout>

        <Button
            android:id="@+id/bottm_sheet_close_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/black"
            android:text="@string/content4"
            android:textSize="@dimen/text18"
            android:textColor="@color/white"/>


    </LinearLayout>

</LinearLayout>

For our Demo app, we are going to create bottom sheets in a single activity. We will create a fragment for Bottomsheet where you can code for bottomsheet xml file. I have a random pic for design in mainactivity. You will get the full source code if you download the project from the download button below. Let's start with the coding. Here we will code for simple activity_main. xml

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.google.android.material.bottomsheet.BottomSheetBehavior;

public class MainActivity extends AppCompatActivity {

    Button bottm_sheet_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottm_sheet_btn=findViewById(R.id.bottm_sheet_btn);
        bottm_sheet_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BottomSheetLayoutDialog bottomSheetLayoutDialog = new BottomSheetLayoutDialog();
                bottomSheetLayoutDialog.show(getSupportFragmentManager(),"ModelOpen");
            }
        });
    }
}

For our Demo app, we are going to create bottom sheets in a single activity. We will create a fragment for Bottomsheet where you can code for bottomsheet xml file. I have a random pic for design in mainactivity. You will get the full source code if you download the project from the download button below. Let's start with the coding. Here we will code for simple activity_main. xml

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

public class BottomSheetLayoutDialog extends BottomSheetDialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable
            ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.layout_bottom_sheet,
                container, false);


        Button bottm_sheet_close_btn = v.findViewById(R.id.bottm_sheet_close_btn);



        bottm_sheet_close_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Toast.makeText(getActivity(),
                        "Close", Toast.LENGTH_SHORT)
                        .show();
                dismiss();
            }
        });
        return v;
    }
}

For our Demo app, we are going to create bottom sheets in a single activity. We will create a fragment for Bottomsheet where you can code for bottomsheet xml file. I have a random pic for design in mainactivity. You will get the full source code if you download the project from the download button below. Let's start with the coding. Here we will code for simple activity_main. xml

Read next

Login Screen design modern UI UX android Android Studio 2022 for beginners

Latest login screen for your upcoming project. This section explains, how to create a login screen and how to manage screens.

Sun, 27 Mar 2022

Using Custom and Downloadable Fonts in Android

To work with Custom Font, you need to install the latest version of Android Studio. A font resource defines a custom font that you can use in your app. Fonts can be individual font files or a collection of font files
Thu, 14 Apr 2022

Android Custom RecyclerView with Text using AndroidX

RecyclerView was introduced in Material Design in API level 21 (Android 5.0 i.e Lollipop). Material Design brings lot of new features in Android that changed a lot the visual design patterns regarding the designing of modern Android applications. Here you can see my YouTube video. You can get visual view from that. I resound again and again that if you have any query comment me in myandroidmaster.com comment section or on YouTube Comment Section.
Mon, 11 Apr 2022