Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How To Use BottomSheetDialog In Android

BottomSheetDialog is one of the important aspect of material design in Android which comes with support library. This is really an alternative to a normal Dialog in your Android app, which pops up from the bottom with animation. Implementation of this dialog is not as straight forward as AlertDialog. So let’s have a look on the implementation –

BottomSheetDialog Source Code

Java class is similar to the below code –

public class SampleBottomSheetDialog extends AppCompatDialogFragment {

    public static SampleBottomSheetDialog newInstance() {
        SampleBottomSheetDialog dialog = new SampleBottomSheetDialog();
        return dialog;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final Activity activity = getActivity();

        View promptsView = LayoutInflater.from(activity).inflate(R.layout.sample_bottom_sheet_dialog, null, false);
        final BottomSheetDialog dialog = new BottomSheetDialog(activity, getTheme());
        dialog.setContentView(promptsView);

        TextView submit = (TextView) promptsView.findViewById(R.id.submit);

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!activity.isFinishing()) {
                    dialog.dismiss();
                }
            }
        });
        return dialog;
    }
}

The above is code is referring sample_bottom_sheet_dialog.xml layout, it is a plain normal layout i.e.,



Now once you are done with these, you are all set to call this dialog. To display the Samplebottomsheetdialog you have to call the show method –

SampleBottomSheetDialog bottomSheetDialog = SampleBottomSheetDialog.newInstance();
        bottomSheetDialog.show(getFragmentManager(), "SAMPLE_TAG");

As it is a form of DialogFragment you have to call this dialog from an AppCompatActivity / FragmentActivity or from a Fragment inside your app, and your app need to be visible. If you are still interested to learn more about this dialog check out this android developers page

Make sure to share this article with your friends and colleagues so they can also learn.

Happy coding!!!

The post How To Use BottomSheetDialog In Android appeared first on { OneTouchCode.com }.



This post first appeared on Onetouchcode, please read the originial post: here

Share the post

How To Use BottomSheetDialog In Android

×

Subscribe to Onetouchcode

Get updates delivered right to your inbox!

Thank you for your subscription

×