Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show AlertDialog in any position of the screen

When we show an AlertDialog in android it shows in the center of the screen. Is there any way to change the position?

like image 422
gypsicoder Avatar asked Mar 29 '11 07:03

gypsicoder


1 Answers

After searching in various post I have found the solution.

The code is posted below:

private CharSequence[] items = {"Set as Ringtone", "Set as Alarm"};     AlertDialog.Builder builder = new AlertDialog.Builder(this);     builder.setItems(items, new DialogInterface.OnClickListener() {         public void onClick(DialogInterface dialog, int item) {              if(item == 0) {              } else if(item == 1) {              } else if(item == 2) {              }         }     });       AlertDialog dialog = builder.create();      dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);      WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();   wmlp.gravity = Gravity.TOP | Gravity.LEFT;  wmlp.x = 100;   //x position  wmlp.y = 100;   //y position   dialog.show(); 

Here x position's value is pixels from left to right. For y position value is from bottom to top.

like image 62
gypsicoder Avatar answered Sep 28 '22 08:09

gypsicoder