In this article, we are going to learn how to create Persistent BottomSheet Dialog in Android using java.
In the below image you can see that what are we going to build.
Hope so you are interested to know how to make the above Persistent BootomSheet Dialog in Android using Java, so now without wasting much time let’s start.
You can also refer our video tutorial for same.
So let’s start with this tutorial :
Persistent Bottomsheet in Android
1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and provide a name to your project. choose Empty activity. By default activity name is MainActivity.java.
2. Now to In order to implement Persistent BottomSheet Dialog in our App, we need a dependency here. So open build.gradle file and add the below dependency.
implementation 'com.google.android.material:material:1.3.0-alpha02'
3. Now create a new drawable res file by navigating res => drawable => New => Drawable resource file =>button_back.xml and put the below line of codes to make the round corners of button.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#EA2323">
</solid>
<corners
android:radius="30dp"></corners>
</shape>
4. Now create a new drawable res file by navigating res => drawable => New => Drawable resource file =>=et_back.xml and put the below lines of code to make the background for editText.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#F1E8E8"></solid>
<corners
android:radius="30dp"></corners>
</shape>
5. Now go to your activity_main.xml file and paste the below lines of code.
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ActionBtn"
android:text="Action"
android:gravity="center"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="80dp"
android:background="@drawable/btn_back"
android:textColor="#fff"/>
</RelativeLayout>
<include layout="@layout/loginsheet"></include>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
6. Now create a new layout res file by navigating res => layout=> New => Layout resource file =>bottom_sheet_dialog.xml and put the below line of codes to make the design for BottomSheet Dialog.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/relative_sheet"
app:behavior_hideable="false"
app:behavior_peekHeight="30dp"
android:background="#000" app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:gravity="center"
android:layout_marginTop="30dp"
android:textColor="#fff"
android:textSize="50dp"
android:textStyle="bold"/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/et_back"
android:layout_marginTop="30dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:hint="Enter Email"
android:padding="8dp"
android:textSize="14dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/et_back"
android:layout_marginTop="20dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:hint="Enter password"
android:padding="8dp"
android:textSize="14dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/btn_back"
android:layout_marginTop="30dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:text="Login"
android:padding="8dp"
android:textColor="#fff"
android:textSize="14dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:textStyle="italic"
android:textColor="#fff"
android:layout_marginTop="20dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:text="Forget password"
android:padding="8dp"
android:textSize="14dp"
android:layout_marginBottom="40dp"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
7. Now finally open your MainActivity.java file and paste the below lines of code to define the functionality of Persistent BottomSheet Dialog.
package com.thecodingshef.persistentbottomsheet;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
public class MainActivity extends AppCompatActivity {
Button actionbtn;
RelativeLayout relativeSheet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionbtn=findViewById(R.id.ActionBtn);
relativeSheet=findViewById(R.id.relative_sheet);
final BottomSheetBehavior bottomSheetBehavior=BottomSheetBehavior.from(relativeSheet);
actionbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
}
}
8. Finally, run the app and you will get the output as shown in the demo image.
If you found this post useful, don’t forget to share this with your friends, and if you have any query feel free to comment it in the comment section.
Thank you 🙂 Keep Learning !