This post describes how to create and display a BottomSheetDialog on Button Click in android.
Create your layout in activity_main.xml. Add a TextView for displaying date, and a Button for displaying BottomSheetDialog.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical|center_horizontal" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" android:padding="10dp" android:layout_margin="10dp" android:textSize="30dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SHOW DIALOG" android:textSize="30dp" android:layout_margin="20dp" android:id="@+id/button1"/> </LinearLayout>
Create dialog.xml. Set fixed height for root view and add a TextView and a Button.
<?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="360dp" android:background="#ee64aa"> <TextView android:id="@+id/textView11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is a bottom sheet dialog" android:textSize="30dp" android:textColor="@color/white" app:layout_constraintBottom_toTopOf="@+id/button11" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Time" android:textSize="30dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
In MainActivity.java show BottomSheetDialog on button click.
package com.example.dialog; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import com.google.android.material.bottomsheet.BottomSheetDialog; import java.util.Date; public class MainActivity extends AppCompatActivity { TextView textView1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = findViewById(R.id.textView1); Button button1 = findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDialog(); } }); } public void showDialog(){ BottomSheetDialog mydialog = new BottomSheetDialog(MainActivity.this); mydialog.setContentView(R.layout.dialog); Button dialog_button = mydialog.findViewById(R.id.button11); dialog_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView1.setText("Date: " + new Date()); mydialog.cancel(); } }); mydialog.create(); mydialog.show(); } }
Now run the app.