How to align user dialog center in android?

I am working on an application where I wanted to display a dialog box for screen size. So, I used the code below. I got the solution here. Does the warning message not appear in the warning dialog?

AlertDialog.Builder builder = new AlertDialog.Builder(this); TextView title = new TextView(this); title.setText("DM2"); title.setBackgroundColor(Color.DKGRAY); title.setPadding(10, 10, 10, 10); title.setGravity(Gravity.CENTER); title.setTextColor(Color.WHITE); title.setTextSize(20); TextView text = new TextView(this); text.setText("Hello This text"); text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); text.setTextSize(20); text.setGravity(Gravity.CENTER); //Creates a linearlayout layout and sets it with initial params LinearLayout ll = new LinearLayout(this); ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); ll.setGravity(Gravity.CENTER); ll.addView(text); builder.setCustomTitle(title); builder.setPositiveButton( "Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); Dialog d = builder.setView(ll).create(); //Fills up the entire Screen WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); lp.copyFrom(d.getWindow().getAttributes()); lp.width = WindowManager.LayoutParams.FILL_PARENT; lp.height = WindowManager.LayoutParams.FILL_PARENT; d.show(); d.getWindow().setAttributes(lp); 

But I want the dialogue displayed in the center. Now this is the display at the top of the window. I tried with lp.garvity = Gravity.center but did not work. And if the device orientation changes, I need to click ok again to close the dialog box. How to fix it?

Thanks in advance Pushpa

+6
source share
5 answers

I'm a bit late, but better late, and then never :-) you can use the following code: (I also saw here )

 dialog = new Dialog(activity, android.R.style.Theme_Translucent_NoTitleBar); dialog.setContentView(R.layout.activity_upload_photo_dialog); Window window = dialog.getWindow(); window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); window.setGravity(Gravity.CENTER); //The below code is EXTRA - to dim the parent view by 70% LayoutParams lp = window.getAttributes(); lp.dimAmount = 0.7f; lp.flags = LayoutParams.FLAG_DIM_BEHIND; dialog.getWindow().setAttributes(lp); //Show the dialog dialog.show(); 

I hope I helped ...

+6
source

This works for me:

 lp.height = WindowManager.LayoutParams.WRAP_CONTENT; 
+3
source

Maybe this works:

 WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes(); WMLP.gravity = Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL dialog.getWindow().setAttributes(WMLP); 
+1
source

Try using android: layout_gravity = "center" instead of android: gravity = "center" ... This may be useful for you.

 android:layout_gravity="center" 

or

you can try this.

  <activity android:theme="@android:style/Theme.Dialog" android:name=".Splash"> </activity> 

Write the full xml code and do a simple activation and add one attribute to AndroidManifest.xml when you register your activity in AndroidManifest.xml

0
source

Note : - This is for a custom layout "... :)

I struggled with the same, and after long hours I came to this result

I used to create a dialogue like this: -

  final Dialog dialog = new Dialog(PopupforeditActivity.this); 

After adding the "No titleBar" style, he began to get the layout as I created.

To do this, you need to create a layout with the parameters "fillParent" and create a dialog like this

  final Dialog dialog = new Dialog(PopupforeditActivity.this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen); dialog.setContentView(R.layout.dialog_billing_cart); dialog.setCancelable(true); 

The above coding part

In the Layout section, put your entire layout in a RelativeLayout

and specify android:layout_centerInParent="true" layout property android:layout_centerInParent="true"

Example: -

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/transparent" > <!--Your Layout--> <ScrollView android:id="@+id/scrlView_FormHolder" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" > </ScrollView> </RelativeLayout> 

And if you don't want to skip the "title", you can have this in your custom layout as TextView ..

Hope this helps .. How did it help me :)

0
source

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


All Articles