Android-Bottom Sheets
The modal Bottom sheet always shows at the bottom of the screen and disappears when the user clicks on any external content. It can be dismissed by sliding it down and dragging it up vertically.
Now create a layout name as bottomsheetlayout.xml and add the following code,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/choosetxt" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAlignment="center" android:text="Choose" android:textColor="@color/black" android:layout_marginTop="5dp" android:textSize="25dp" android:textStyle="bold"/> <TextView android:layout_width="wrap_content" android:id="@+id/layoutEdit" android:layout_height="wrap_content" android:text="Edit" android:layout_margin="20dp" android:textSize="16sp"/> <TextView android:layout_width="wrap_content" android:id="@+id/layoutShare" android:layout_height="wrap_content" android:text="Share" android:layout_margin="20dp" android:textSize="16sp"/> <TextView android:layout_width="wrap_content" android:id="@+id/layoutUpload" android:layout_height="wrap_content" android:text="Upload" android:layout_margin="20dp" android:textSize="16sp"/> </LinearLayout> |
Now, add the following code in activity_main.xml file. Here we add a button on activity_main layout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" android:id="@+id/botttom_sheet" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> |
Add the following code in the MainActivity.java file. Here an onClickListener is attached with the button. If the user clicks on it, it gets invoked and bottom sheet dialog displays to user.
MainActivity.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
package com.example.alertdialog; import androidx.appcompat.app.AppCompatActivity; import android.app.Dialog; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { Button bottomsheet; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bottomsheet = findViewById(R.id.botttom_sheet); bottomsheet.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDialog(); } }); } private void showDialog() { final Dialog dialog = new Dialog(this); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.bottomsheetlayout); TextView edit = dialog.findViewById(R.id.layoutEdit); TextView share = dialog.findViewById(R.id.layoutShare); TextView upload = dialog.findViewById(R.id.layoutUpload); edit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); Toast.makeText(MainActivity.this,"Edit is Clicked", Toast.LENGTH_SHORT).show(); } }); share.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); Toast.makeText(MainActivity.this,"Share is Clicked",Toast.LENGTH_SHORT).show(); } }); upload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); Toast.makeText(MainActivity.this,"Upload is Clicked",Toast.LENGTH_SHORT).show(); } }); dialog.show(); dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE)); //dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; dialog.getWindow().setGravity(Gravity.BOTTOM); } } |