How to implement a Bottom Sheet Dialog i ...

How to implement a Bottom Sheet Dialog in Kotlin

Apr 29, 2022
249

Inthis tutorial I’ll show you, how to add a Bottom Sheet Dialog to our app. This UI component may be found in everyday apps like Google Drive, Maps, and the Music Player App. The Bottom sheet appears at the bottom of the screen and is dismissed if the user clicks on the outside content. It may be dragged vertically and dismissed by sliding it down the screen.

What exactly is a bottom sheet?

The Android design support library’s Bottom Sheet component is used to display different activities in an expanding UI design. It’s an expandable widget that appears at the bottom of an Android device when a specific Button or View is pressed.

Types of Bottom Sheet?

There are two different types of Bottom Sheet as

  1. Persistent Bottom Sheet

  2. Modal Bottom Sheet

Persistent Bottom Sheet: This bottom sheet shows in-application content. It will be shown at the base of the screen with a portion of its part noticeable (we can characterize measurements in dp), it shows full substance in the wake of growing it. It has a littler height.

Modal Bottom Sheet: This bottom sheet functions as a menu or discourse with choices, it implies this replaces menu or exchange. It has a higher height than the determined base sheet. For the most part, they are utilized for incorporating profound connecting picker activities.

Let’s start :)

Step by Step Implementation

Step 1 — Create a Layout for Dialog

create a sampledialogone.xml file and add the following code.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

<Button
    android:id="@+id/btnSampleOne"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sample 1"
    android:layout_marginTop="50dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

Step 3— Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code.

import android.os.Bundle
import android.widget.Button
import android.widget.RelativeLayout
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.ezatpanah.bottomsheet_youtube.databinding.ActivityMainBinding
import com.google.android.material.bottomsheet.BottomSheetDialog

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.apply {
            btnSampleOne.setOnClickListener {
                showDialogOne()
            }
        }
    }


    fun showDialogOne() {

        val dialog = BottomSheetDialog(this)
        dialog.setContentView(R.layout.sample_dialog_one)
        val btnEdit= dialog.findViewById<RelativeLayout>(R.id.rl_edit)
        val btnDelete= dialog.findViewById<RelativeLayout>(R.id.rl_delete)
        val btnAdd= dialog.findViewById<RelativeLayout>(R.id.rl_add)

        btnEdit?.setOnClickListener {
            Toast.makeText(this, "Clicked on Edit", Toast.LENGTH_SHORT).show()
        }
        btnDelete?.setOnClickListener {
            Toast.makeText(this, "Clicked on Delete", Toast.LENGTH_SHORT).show()
        }
        btnAdd?.setOnClickListener {
            Toast.makeText(this, "Clicked on Add", Toast.LENGTH_SHORT).show()
        }
        dialog.show()
    }

}

Now run your app and see the output of the app :)

it’s Done :)

Github Source :

https://github.com/Ezatpanah/BottomSheet-Youtube

Also, you can check my video about this tutorial on YouTube :

https://youtu.be/Our9IRb7nrs

Vous aimez cette publication ?

Achetez un café à Reyhaneh (AndroidGeek.co)

Plus de Reyhaneh (AndroidGeek.co)