Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 700
roroinpho21 Avatar asked Jul 12 '16 08:07

roroinpho21


2 Answers

You are parsing Dialog view as

fragment.getDialog().getWindow().findViewById(android.R.id.content)

I suggest you to parse fragment layout root view instead.

like image 200
Dehan Wjiesekara Avatar answered Nov 20 '22 07:11

Dehan Wjiesekara


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.

like image 4
Dhruvi Avatar answered Nov 20 '22 08:11

Dhruvi