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 position dialog to the bottom
You can try like this :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.promotion_layout)// use your layout in place of this.
window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT));
val windowlp = window.attributes
windowlp.gravity = Gravity.BOTTOM
window.attributes = windowlp
}
Access your dialog view in your Activity
PromotionDialog(this).show();
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();`
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.
How Do I Position Custom Dialog at specific Coordinate?
Override onTouch() of your view
AlertDialog dialog;
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
showDialog(); // display dialog
break;
case MotionEvent.ACTION_MOVE:
if(dialog!=null)
dialog.dismiss();
// do something
break;
case MotionEvent.ACTION_UP:
// do somethig
break;
}
return true;
}
public void showDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(FingerPaintActivity.this);
dialog = builder.create();
dialog.setTitle("my dialog");
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();
}
Even to draw user touches the screen then also dialog is displayed. so dismiss dialog in on move.
Related Topics
Making the Android Emulator Run Faster
Email Address Validation in Android on Edittext
Match_Parent Width Does Not Work in Recyclerview
Fusedlocationapi.Getlastlocation Always Null
How to Display Progress Dialog Before Starting an Activity in Android
Error Java.Lang.Classnotfoundexception: Com.Google.Android.Gms.Maps.Mapfragment in Google Map V2
Was Preferencefragment Intentionally Excluded from the Compatibility Package
Change Toolbar Color in Appcompat 21
Monodroid: Error When Calling Constructor of Custom View - Twodscrollview
Error - Android Resource Linking Failed (Aapt2 27.0.3 Daemon #0)
How to Free a Component in Android/Ios
Flinging with Recyclerview + Appbarlayout
Blurry Svg Images in Chrome for Android
Is There a Real Solution to Debug Cordova Apps
Passing Data of a Non-Primitive Type Between Activities in Android