Output:
1/ Modify 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:id="@+id/main"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnBottomSheet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Bottom Sheet"
android:gravity="center"/>
</LinearLayout>
2/ Create and add on bottomsheet_xml.xml.
<?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"
android:orientation="vertical"
android:padding="16dp"
android:background="@android:color/white">
<TextView
android:layout_gravity="center"
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Sheet Title"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginBottom="8dp" />
<Button
android:layout_marginTop="24dp"
android:id="@+id/actionButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Close Bottom Sheet" />
</LinearLayout>
3/ Modify MainActivity.kt
package com.zissofworks.bottomsheetdialog
import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.view.Window
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class MainActivity : AppCompatActivity() {
private lateinit var btnBottomSheet: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
btnBottomSheet = findViewById(R.id.btnBottomSheet)
btnBottomSheet.setOnClickListener { showDialog()}
}
private fun showDialog() {
val dialog = Dialog(this@MainActivity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.botttomsheet_xml);
val actionButton = dialog.findViewById<Button>(R.id.actionButton)
actionButton.setOnClickListener {dialog.dismiss()}
dialog.show();
dialog.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT));
dialog.window?.setGravity(Gravity.BOTTOM);
}
}