How to align custom dialog centre in android ?
Asked Answered
P

6

8

I am working on the application where i wanted to display the dialog to be screen size. So i used code below.I got the solution through here Alert message is not displaying in alert dialog box?

 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 dialog to be appeared in center aligned. Now it is display in top of the window. I tried with lp.garvity = Gravity.center, but didn't work. and If the device orientation changes, i need to press ok button more time to close the dialog. How to fix this?

Thanks in advance Pushpa

Pyrrhic answered 4/5, 2012 at 10:51 Comment(2)
did you try writing d.getWindow().setAttributes(lp); before d.show(); ?Cost
I tried but it will not display the alert box to window sizePyrrhic
E
9

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...

Exterritorial answered 2/9, 2013 at 22:26 Comment(1)
Nice answer, I use window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) instead of 'window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_CONTENT)',it works fine,thanks.Theophylline
R
4

This works for me:

lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
Rhetorician answered 4/5, 2012 at 11:12 Comment(3)
@pushpa: hey try gravity center by setting height to wrap_contentRhetorician
@unreachable: I tried this will display the dialog in center, I have view with 3 buttons in background and want to hide that view. height wrap content will not hide the background view.Pyrrhic
@pushpa: hey in that case you can make the background of your popup not transparent you can use getWindow().setBackgroundDrawableResource(android.R.color.black);Rhetorician
D
2

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

Delogu answered 4/5, 2012 at 10:56 Comment(2)
For custom dialog you can do one thing, Make your whole screen like Activity screen, and add the Theme:@dialog in AndroidManifest.xml.. that your whole content will display as a dialog.Delogu
I had the mAlertDialog?.window?.setLayout stretched to match parent but i wanted to centre the AlertDialog itself in the screen. using layout_gravity instead of gravity worked for me!Ceric
C
1

Maybe this works:

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

WMLP.gravity = Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL

dialog.getWindow().setAttributes(WMLP);
Cost answered 4/5, 2012 at 11:14 Comment(0)
P
0

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 :)

Precedent answered 14/5, 2012 at 5:5 Comment(0)
A
0

I used this style to show dialogs in centre android

<style name="CustomDialogue" parent="Theme.MaterialComponents.Dialog">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:layout_gravity">center</item>
    <item name="android:minWidth" type="dimen">600dp</item>
    <item name="android:layout_margin" type="dimen">8dp</item>
</style>
  • I used android:layout_gravity to show dialog in centre.
  • minWidth is set because otherwise dialog look narrower.
  • layout_margin is set so there is enough margin around dialog in smaller screens.

Before android:layout_gravity="center"

Before

After android:layout_gravity="center"

After

Aundrea answered 29/12, 2020 at 4:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.