How to move a DialogFragment out of the center?
Asked Answered
S

3

6

Is it possible by using a DialogFragment to be able to move it out of the center and place it anywhere on the screen?

Spent answered 22/5, 2013 at 10:22 Comment(0)
Z
7

I have success using

 Window window = dialog.getWindow();
 WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
 lp.gravity = Gravity.TOP | Gravity.RIGHT;
 lp.x = 100;
 lp.y = 100;
 window.setAttributes(lp);

puts my dialog in the Top Right slightly down from the corner. this code is in onCreateDialog().

Zildjian answered 22/5, 2013 at 10:30 Comment(0)
S
8

You can use

getDialog().getWindow().setAttributes(param);

in

onCreateView()

like this

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    getDialog().getWindow().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
    WindowManager.LayoutParams param = getDialog().getWindow().getAttributes();
    param.width = LayoutParams.MATCH_PARENT;
    param.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;
    param.x = 100;
    param.y = 100;
   .
   .
    getDialog().getWindow().setAttributes(p);
   .
   .
}
Secretory answered 22/5, 2013 at 10:30 Comment(0)
Z
7

I have success using

 Window window = dialog.getWindow();
 WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
 lp.gravity = Gravity.TOP | Gravity.RIGHT;
 lp.x = 100;
 lp.y = 100;
 window.setAttributes(lp);

puts my dialog in the Top Right slightly down from the corner. this code is in onCreateDialog().

Zildjian answered 22/5, 2013 at 10:30 Comment(0)
C
1

I suffered a lot trying all the programmatically solutions with no results. Finally I've done it from the XML file without any extra code within the Java class.

All I've done is Make the parent height match_parent and set a gravity for it with value center

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
<!--The following two lines-->
android:layout_height="match_parent"
android:gravity=“center"
android:orientation="vertical">

<Button
    android:id="@+id/button_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:background="@android:color/white"
    android:gravity="center"
    android:padding="10dp"
    android:text="@string/hide"
    android:textColor="@android:color/holo_blue_dark" />

<Button
    android:id="@+id/button_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:background="@android:color/white"
    android:gravity="center"
    android:padding="10dp"
    android:text="@string/cancel"
    android:textColor="@android:color/holo_blue_dark" />

Coycoyle answered 6/5, 2015 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.