Reputation: 312
I have an activity with FragmentDialog
. In onResume
of this dialog I set height and weight of it to 80% and 90% by code:
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(getDialog().getWindow().getAttributes());
layoutParams.width = (int)(screenWidth * 0.9);
layoutParams.height = (int)(screenHeight * 0.8);
getDialog().getWindow().setAttributes(layoutParams);
It works perfectly, background has shadow, foreground FragmentDialog
has proper dimensions. Question is - how can I show SnackBar
at the bottom of the screen not affected by FragmentDialog
(one that has shadow, Activity view) without shadow? Is there any way to disable shadow for specific view at activity that is in background of FragmentDialog
?
Upvotes: 11
Views: 7202
Reputation: 875
It's late but there is a solution I think may be useful for others.
To disable the dialog fragment shadow (in fact it is called DIM), add below code to your dialog fragment onResume
method.
For Kotlin:
override fun onResume() {
super.onResume()
dialog?.window!!.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
}
For Java:
@Override
public void onResume() {
super.onResume();
if(getActivity()!=null)
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
by the this is not exactly what question is asking but this will omit all the shadow behind dialog fragment.
To show snackbar in you parent fragment rather than your dialog fragment, you can pass parent fragment reference to dialog fragment constructor and instantiate snackbar with parent's view. this will show the snackbar at the bottom of parent fragment.
showSnackbar
method would be like this:
For Kotlin:
private fun showSnackbar(messege: String) =
Snackbar.make(parent.view!!, messege, Snackbar.LENGTH_SHORT).show()
For Java:
private void showSnackBar(String messege) {
if (parent.getView() != null)
Snackbar.make(parent.getView(), messege, Snackbar.LENGTH_SHORT).show();
}
dialog fragment complete code would be like:
For kotlin:
class MyDialogFramgent(parent: Fragment) : DialogFragment() {
// class code ...
override fun onResume() {
super.onResume()
dialog?.window!!.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
}
private fun showSnackbar(messege: String) =
Snackbar.make(parent.view!!, messege, Snackbar.LENGTH_SHORT).show()
}
For Java:
public class MyDialogFragment extends DialogFragment {
private Fragment parent;
public MyDialogFragment(Fragment parent) {
this.parent = parent;
}
@Override
public void onResume() {
super.onResume();
if (getActivity() != null)
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
private void showSnackBar(String messege) {
if (parent.getView() != null)
Snackbar.make(parent.getView(), messege, Snackbar.LENGTH_SHORT).show();
}
}
Upvotes: 0
Reputation: 131
Material design documentation says "Snackbars appear above most elements on screen, and they are equal in elevation to the floating action button. However, they are lower in elevation than dialogs, bottom sheets, and navigation drawers". From there, I think you should consider displaying the Snackbar
inside the DialogFragment
or just display a small dialog on top of it.
If you want to display Snackbar
inside dialog fragment, you could do something like this:
public void showSnackBar(final View parent, final String text) {
Snackbar sb = Snackbar.make(parent, text, Snackbar.LENGTH_LONG);
sb.show();
}
parent
is the entire dialog's view. You could improve it by making the root view of the fragment a CoordinatorLayout
and showing the Snackbar
on that view.Upvotes: 6