How to align custom dialog centre in android ?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 07:46:10
Woody

I'm little late, but better late then never :-) you can use the code below: (I saw also 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();

Hope I helped...

Niraj Gupta

This works for me:

lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

Maybe this works:

WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();

WMLP.gravity = Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL

dialog.getWindow().setAttributes(WMLP);
vishesh chandra

Try to use android:layout_gravity="center" instead of android:gravity="center"... It may be helpful for you..

android:layout_gravity="center" 

or

you can try this..

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

Write full xml code and make simple Activty and add one attribute in AndroidManifest.xml, when you registering your Activity into AndroidManifest.xml

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

I was struggling with the same, and after struggling for hours i came to this result

Earlier i was creating the dilaoge like this : -

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

After adding style "No titleBar" it started fetching the layout the way I had created.

For this you have to create the layout with params "fillParent" and create the dialoge 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 is coding part

You in Layout part put your whole layout in RelativeLayout

and give your layout property android:layout_centerInParent="true"

Ex:-

<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 miss "title" You can have that in your custom layout as TextView ..

Hope it will help.. As it helped me :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!