How to add a SnackBar on the bottom of a dialog's overlay

roroinpho21 picture roroinpho21 · Jul 12, 2016 · Viewed 7.7k times · Source

I have an android application which has many different pop-ups. I want to add a SnackBar over dialog's overlay on the bottom of the screen.

I tried the following code

if (fragment!= null) {
    Snackbar snackbar = Snackbar.make(fragment.getDialog().getWindow().findViewById(android.R.id.content),
            message, Snackbar.LENGTH_LONG);
    View view = snackbar.getView();
    FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams();
    params.gravity = Gravity.BOTTOM;
    view.setLayoutParams(params);
    snackbar.show();
}

But the SnackBar appears on the bottom of centered Dialog not on the bottom of the screen. If I add the Snackbar on current Activity then it appears under dialog's overlay.

Android SnackBar

Answer

Dhruvi picture Dhruvi · Jul 12, 2016

Use below theme for full screen custom dialog

<style name="DialogTheme" parent="android:Theme.Dialog">

<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>


<!-- No backgrounds, titles or window float -->
<item name="android:windowBackground">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>

Use it in your dialog as below:

dialog = new Dialog(this, R.style.DialogTheme);

To use above things, you should use custom layout for your custom dialog.

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

Try above line to make your dialog background transparent.