you can try to create the bottom sheet as bottomsheetdialogfragment
here is the working example ,you can set the state for the bottom sheet as collpsed, expended, half expended according to your requirement.
public class WebViewDialog extends BottomSheetDialogFragment implements View.OnClickListener {
public UserSessions mUserSessions;
BottomSheetBehavior bottomSheetBehavior;
WebviewLayoutBinding binding;
String title;
String url;
public WebViewDialog(String url, String title) {
this.title = title;
this.url = url;
}
public WebViewDialog() {
}
BottomSheetDialog bottomSheet;
View mView = null;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
bottomSheet = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
View view = View.inflate(getActivity(), R.layout.webview_layout, null);
mView = view;
binding = DataBindingUtil.bind(view);
bottomSheet.setContentView(view);
bottomSheet.setCancelable(false);
bottomSheet.setCanceledOnTouchOutside(false);
bottomSheetBehavior = BottomSheetBehavior.from((View) (view.getParent()));
bottomSheetBehavior.setPeekHeight(BottomSheetBehavior.PEEK_HEIGHT_AUTO);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View view, int i) {
if (BottomSheetBehavior.STATE_EXPANDED == i) {
}
if (BottomSheetBehavior.STATE_COLLAPSED == i) {
}
if (BottomSheetBehavior.STATE_HIDDEN == i) {
dismiss();
}
}
@Override
public void onSlide(@NonNull View view, float v) {
}
});
mUserSessions = new UserSessions(getActivity());
binding.imgCLose.setOnClickListener(this);
if (url != null && !url.isEmpty()) {
BBProgress.showProgressDialog(getActivity());
binding.mWebView.setWebViewClient(new MyBrowserClient());
binding.mWebView.getSettings().setJavaScriptEnabled(true);
binding.mWebView.loadUrl(url);
}
return bottomSheet;
}
@Override
public void onDismiss(@NonNull DialogInterface dialog) {
super.onDismiss(dialog);
}
@Override
public void onStart() {
super.onStart();
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
int mStep = 0;
public void onBackClick() {
dismiss();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.imgCLose) {
onBackClick();
}
}
public boolean lg;
public class MyBrowserClient extends WebViewClient {
public MyBrowserClient() {
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
NetworkInfo nf = ((ConnectivityManager) getActivity().getSystemService(CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (nf == null || !nf.isConnected()) {
CommonMethods.errorToast(getActivity(), getActivity().getString(R.string.error_internet));
return true;
} else if (url.startsWith("http:") || url.startsWith("https:")) {
view.getSettings().setBuiltInZoomControls(false);
view.loadUrl(url);
return true;
} else {
view.getSettings().setBuiltInZoomControls(false);
view.loadData(url, "text/html", "UTF-8");
return true;
}
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (!lg) {
boolean unused = lg = true;
return;
}
}
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
BBProgress.hideProgressDialog(getActivity());
}
}
}
And thew xml file for the layout is
webview_layout.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/app_bg"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorAccent">
<RelativeLayout
android:id="@+id/rlTopHeader"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@color/white"
android:padding="@dimen/_10dp">
<ImageView
android:id="@+id/imgCLose"
android:layout_width="@dimen/_44dp"
android:layout_height="@dimen/_44dp"
android:layout_centerVertical="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:padding="@dimen/_5dp"
android:src="@drawable/ic_back"
app:tint="@color/black" />
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="@dimen/_10dp"
android:layout_toEndOf="@+id/imgCLose"
android:text="@string/app_name_inside"
android:textColor="@color/black"
android:textSize="@dimen/_18dp" />
</RelativeLayout>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/appBarLayout"
android:background="@color/white"
android:minHeight="@dimen/_250dp"
android:orientation="vertical">
<WebView
android:id="@+id/mWebView"
android:nestedScrollingEnabled="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></WebView>
</LinearLayout>
</RelativeLayout>
I hope this example will help and solve your problem as per I am using the same and working fine for me.