Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align custom dialog centre in android ?

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

like image 530
pushpa Avatar asked May 04 '12 10:05

pushpa


3 Answers

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

like image 63
Woody Avatar answered Nov 05 '22 09:11

Woody


This works for me:

lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
like image 4
Niraj Gupta Avatar answered Nov 05 '22 08:11

Niraj Gupta


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

like image 2
Vishesh Chandra Avatar answered Nov 05 '22 08:11

Vishesh Chandra