How to move a dialog box from the center?

Is it possible using DialogFragment to bring it out of the center and place it anywhere on the screen?

+6
source share
3 answers

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 upper right corner just below the corner. this code is in onCreateDialog() .

+5
source

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); . . } 
+5
source

I suffered a lot from all software solutions without any results. Finally, I made it from an XML file without additional code in the Java class.

All I did was set the parent height match_parent and set its gravity to 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" /> 

0
source

Source: https://habr.com/ru/post/945565/


All Articles