Reputation: 2763
I am trying a very simple app where I'd like to display a DialogFragment
. However I would like to align the DialogFragment
's right margin with the Activity
's right edge. I used layout gravity to align the Dialog's window to the right and top:
public class MyDialogFragment extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.activity_main, container, false);
getDialog().getWindow().setGravity(Gravity.RIGHT | Gravity.TOP);
return view;
}
}
However there is 'gap' between the DialogFragment
's right edge and the Activity
's right by default. I've tried a few SO posts but none seem to work for me:
Changing position of the Dialog on screen android
Show AlertDialog in any position of the screen
I am trying to anchor my dialog to a particular coordinate pair but I am not successful with this approach. What I'd like to do is simply align the dialog's right margin with the Activity
's right edge.
Here is an image of my issue. Any direction or a solution would be most appreciated.
Upvotes: 1
Views: 782
Reputation: 2763
Unbelievable problem at first. First, the dialog is indeed aligned to the top right corner of the window. The reason it appears with an offset is because of a shadow around it (note that the width of the shadow various between pre-lollipop devices and devices running lollipop and above, similar to the way a dialog ui varies between Android versions).
This is very evident when the background of the DialogFragment
is made transparent using this line in onCreateView(..)
:
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
An option would be to apply your own shadow to the DialogFragment's window after setting the background colour to transparent.
Upvotes: 1