Show AlertDialog in any position of the screen
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.
Changing position of the Dialog on screen android
I used this code to show the dialog at the bottom of the screen:
Dialog dlg = <code to create custom dialog>;
Window window = dlg.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
This code also prevents android from dimming the background of the dialog, if you need it. You should be able to change the gravity parameter to move the dialog about
How to change position of Alert Dialog
try this
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Final Result");
alert.setMessage(msg);
alert.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
// dlgAlert.create();
AlertDialog dialog_card = alert.create();
// dlgAlert.requestWindowFeature(Window.FEATURE_NO_TITLE);
// WindowManager.LayoutParams WMLP =
dialog_card.getWindow().setGravity(Gravity.TOP);
dialog_card.show();`
Related Topics
Error:Execution Failed for Task ':Android:Transformclassesandresourceswithproguardforrelease'
Android Popup Window Dismissal
Web Colors in an Android Color Xml Resource File
Find Distance Between Two Points on Map Using Google Map API V2
Recyclerview Header and Footer
How to Use Addr2Line in Android
How to Switch Activity Without Animation in Android
How to Ignore Accent in SQLite Query (Android)
How to Set a Reminder in Android
Call Removeview() on the Child's Parent First
Get the Current Language in Device
Retrieve Incoming Call's Phone Number in Android
Streaming Video from Android Camera to Server
Android Listview Not Refreshing After Notifydatasetchanged
Android P - 'Sqlite: No Such Table Error' After Copying Database from Assets
Android Preventing Double Click on a Button