Android: Display custom dialog in center of the container - android

How to display custom dialog as a center of the container?
Dialog customdialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar);
Window window = customdialog.getWindow();
window.setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
window.setGravity(Gravity.CENTER);
R.style.Theme_Translucent_NoTitleBar - is used to remove border for cutomdialog. Once i used this line the dialog will appear at the topleft corner of the tablet?
Can anyone help me to solve this issue?

Change the fill_parent to wrap_content.I hope this will be the issue the dialog appear at the corner of the activity.It takes the space of whole layout.So changing this may help you to get what you realy wanted.
Window window = customdialog.getWindow();
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
Use above code after dialog.show() statement

I added this to the dialog's custom style and it worked fine.
<item name="android:layout_gravity">center</item>
My dialog's width and height are set to wrap_content. The style's parent is
parent="#android:style/Theme.Light"

I will go for this piece of code:
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = dialog.getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(lp);
Where dialog, is the dialog object to be shown. Inside of the layout of the dialog you can define the view of the layout as you wish: centered or not.

I use this code and the problem solved
Window window = alertDialog.getWindow();
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);

Related

Android 11 dialog in fullscreen doesn't work

When I set a dialog in full screen it isn't really fullscreen.
see here
How can I fix that?
Window window = getWindow();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);
This code is in a custom dialog class that extends Dialog.
That's why I am able to write only getWindow().
Actually I don't need it to be fullscreen but I need atleast the pixel difference between the screen and the dialog box.
you can add the following theme to dialog's second parameter android.R.style.Theme_Black_NoTitleBar_Fullscreen
Dialog(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen)

Is there a way when displaying a dialog in Android to tell the app to show the pop up?

I have a full screen app that has buttons that want to have a custom drop down when they are clicked. I was thinking of having a custom dialog show up that will have the options. Is there a way to tell the dialog where to show up?
If you want to create a Dialog window you can change the position of the screen in which it will be displayed by doing something like this:
//Dialog reference
Dialog dialog;
//Instantiate here your dialog
dialog = ... ;
//Get window layout parameters reference
Window window = dialog.getWindow();
WindowManager.LayoutParams windowLayoutParams = window.getAttributes();
//Set the parameters you want
windowLayoutParams.gravity = Gravity.CENTER; //e.g. dialog's will be centered
window.setAttributes(windowLayoutParams);
You can instead use Gravity.BOTTOM or Gravity.TOP to display the dialog accordingly. Alternatively, for more specific positioning you can try setting attributes "windowLayoutParams.x" & "windowLayoutParams.y".

Android CustomDialog minWidth

I have a CustomDialog (extending Dialog) which is currently set to wrap_content for both width and heigh. Problem is if I set the main text as something really short (say "hello") the dialog gets very small in width. I would like the dialog to always take all the space available, but still have the default margin from the edges of the screen.
I already tried various methods:
setting:
android:layout_width="match_parent"
on the dialog layout, doesn't have any effect.
doing:
int width = (int)(getContext().getResources().getDisplayMetrics().widthPixels*0.90);
getWindow().setLayout(width, LinearLayout.LayoutParams.WRAP_CONTENT);
causes the dialog to go at the start of the screen and doesn't change anything regarding the width
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.horizontalMargin = 10;
params.width = LinearLayout.LayoutParams.MATCH_PARENT;
window.setAttributes(params);
makes the dialog take the whole screen but without any horizonthal margin
putting
<item name="windowMinWidthMajor">100%</item>
<item name="windowMinWidthMinor">65%</item>
in the dialog style has no effect.
how do I accomplish that? Mind that the dialog is used all around the app, so putting any code outside of the dialog class itself is not an option.

Anchor Android Dialog to a View

I wanted to know if it is possible to anchor a Dialog to a View in Android, or in general: just to display the dialog at a certain location on the screen.
P.S.
I don't want to use a PopupMenu because it is my understanding that one cannot customize the items displayed in the menu-- I'm ultimately trying to have text and put an image next to it to alert the user that they have a message or something new to see here.
Thanks for your time-
Use Window params.
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.copyFrom(getDialog().getWindow().getAttributes());
params.gravity = Gravity.BOTTOM;
params.y = YOUR_ANCHOR_VIEW.getHeight();
getDialog().getWindow().setAttributes(params);
I used Gravity.BOTTOM and view height to anchor dialog on top of anchor view.
Gravity.TOP will make y to apply from top and vise versa.
I used this code on onActivityCreated().

Maximizing an AlertDialog?

I'd like to show a Dialog that occupies as much screen space as possible.
So, here's a sample:
AlertDialog dialog = new AlertDialog.Builder(ctx)......create();
Window w = dialog.getWindow();
WindowManager.LayoutParams lp = w.getAttributes();
lp.width = 320;
lp.height = 480;
w.setAttributes(lp);
Problem is, this doesn't change a thing. Why?
TIA.
I don't know how to do exactly what you're wanting, but isn't this mostly defeating the purpose of a Dialog? Why not just create a custom Activity and use the Dialog theme if you're just wanting it to look dialog-ey.
You need to change the window layout parameters AFTER setContentView. Anything you do to the window before then (size/location-wise) will be tossed out when you call setContentView.
http://devstream.stefanklumpp.com/2010/07/android-display-dialogs-in-fullscreen.html
You need to add minWidth and minHeight attributes in the Dialog's xml layout file. E.g. your screen size is 800 * 480, then set android:minWidth="800dip", android:minHeight="400dip"

Categories

Resources