How to set the position of AlertDialog from the top screen in Android?

I want to set the position of AlertDialog behind the status bar, when the content in my dialog will increase, how to do it? I am creating a custom AlertDialog using my own layout .... Please help me ....


Below is my code, I set the height and position of xy alertDialog, but still it does not show its effect.

AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inf = getLayoutInflater(); View layout = inf.inflate(R.layout.main, null); builder.setView(layout); builder.setTitle("Add to Home screen"); AlertDialog dialog = builder.create(); WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes(); int dialogOriginalHeight = WMLP.height; WMLP.height += 750; Log.i("XnY", "x="+WMLP.x+", y="+WMLP.y); WMLP.x = -10; //x position WMLP.y = -10; //y position Log.i("XnY", "x="+WMLP.x+", y="+WMLP.y); dialog.getWindow().setAttributes(WMLP); Log.i("POSITION", "POS::HEIGHT:"+WMLP.height); dialog.show(); 
+4
source share
2 answers

I know this is really old, but for those who see it later:

x and y are ignored because you are using gravity by default.

From docs :

X position for this window. With gravity, it is ignored by default. When using LEFT or START or RIGHT or END, it provides an offset from a given edge.

I needed me to be offset from the top right, so I set my gravity to 0x00000035. This is the top and right. (the top is 0x00000030 and the right is 0x00000005). This will cause x and y to not be ignored.

 alert.getWindow().setGravity(0x00000035); 
+1
source
 WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes(); wmlp.gravity = Gravity.BOTTOM | Gravity.RIGHT; wmlp.x = 50; //x position wmlp.y = 50; //y position dialog.show(); 

may be a good option

+1
source

Source: https://habr.com/ru/post/1314476/


All Articles