Search code examples
javaandroidandroid-fragmentsandroid-bottomsheetdialog

how to show website in Bottonsheetdialog android studio


how to show a web page with webview in BottonSheetDialog ! like this

click here for see example image


Solution

  • To do you have to create a class that extends BottomSheetDialogFragment, you have to create a layout that contains a webview, then call oncreateview() method where you inflate layout and set up the webview with its reference from the layout, then in your activity show the bottomsheetdialog

    *First create a layout:

    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <WebView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/webview"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    • Secondly create a class that extends BottomSheetDialogFragment
    //Extend Your class with BottomSheetDialogFragment
    public class WebViewBottomSheet extends BottomSheetDialogFragment{
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            
            //Inflate Your layout which contains the webView
            View view = inflater.inflate(R.layout.weblayout,container,false);
    
            WebView webView = view.findViewById(R.id.webview);
            webView.loadUrl("Put your URL here");
            return view;
        }
    }
    
    
    • And finally just show your BottomSheet in your activity
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            
            //Here you show your BottomSheetDialog
            WebViewBottomSheet webViewBottomSheet = new WebViewBottomSheet();
            webViewBottomSheet.show(getSupportFragmentManager(),"Show Bottom Sheet");
        }
    }
    
    

    PS: Don't forget to add the design library to your project