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
Maybe this works:
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 : -
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
The above is coding part
You in
Layout
part put your whole layout inRelativeLayout
and give your layout property
android:layout_centerInParent="true"
Ex:-
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 :)
This works for me:
I'm little late, but better late then never :-) you can use the code below: (I saw also here)
Hope I helped...
Try to use android:layout_gravity="center" instead of android:gravity="center"... It may be helpful for you..
or
you can try this..