Changing position of the Dialog on screen android
AndroidAndroid LayoutAndroid IntentAndroid EmulatorAndroid WidgetAndroid Problem Overview
I made a simple AlertDialog
in my Activity:
View view = layoutInflater.inflate(R.layout.my_dialog, null);
AlertDialog infoDialog = new AlertDialog.Builder(MyActivity.this)
.setView(view)
.create();
infoDialog.show();
With above code, the dialog shows at the (about) the center of the screen.
I am wondering, how can I customize the dialog position to make it showing just under the top Action Bar ? (Is there anyway to change the gravity or something of the dialog?) and how to do it based on my code??
Android Solutions
Solution 1 - 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
Solution 2 - Android
private void showPictureialog() {
final Dialog dialog = new Dialog(this,
android.R.style.Theme_Translucent_NoTitleBar);
// Setting dialogview
Window window = dialog.getWindow();
window.setGravity(Gravity.CENTER);
window.setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
dialog.setTitle(null);
dialog.setContentView(R.layout.selectpic_dialog);
dialog.setCancelable(true);
dialog.show();
}
you can customize you dialog based on gravity and layout parameters change gravity and layout parameter on the basis of your requirenment
Solution 3 - Android
I found this code snippet from @gypsicoder code here
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.
Solution 4 - Android
New BottomSheetDialog
:
BottomSheetDialog dialog = new BottomSheetDialog(YourActivity.this);
dialog.setContentView(YourView);
dialog.show();
Solution 5 - Android
For me, this worked out pretty well where I was trying to position my dialog somewhere exactly at the bottom of the textview where it gets selected.
public void setPosition(int yValue) {
Window window = getWindow();
WindowManager.LayoutParams param = window.getAttributes();
param.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
param.y = yValue;
window.setAttributes(param);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
Solution 6 - Android
just add this to your code:
dialog.getWindow().setGravity(Gravity.BOTTOM);
Solution 7 - Android
You can use this snippet to place the AlertDialog at the bottom of the screen.
AlertDialog dialogPopup;
dialogPopup = mBuilder.create();
dialogPopup.getWindow().getAttributes().gravity = Gravity.BOTTOM;
Solution 8 - Android
dialog.getWindow().getAttributes().gravity = Gravity.BOTTOM;
Solution 9 - Android
i use this method
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
dialog.setContentView(R.layout.dialog_droppoint);
dialog.show();
window.setAttributes(wlp);
return dialog;
}
Solution 10 - Android
use bottomSHeet :
BottomSheetDialog dialog = new BottomSheetDialog(YourActivity.this);
dialog.setContentView(YourView);
dialog.show();
Solution 11 - Android
kotlin:
val dialog = Dialog(context)
//set graviy bottom
dialog.window.setGravity(Gravity.BOTTOM)
Solution 12 - Android
For my Dialog activity i used this one:
WindowManager.LayoutParams lp = this.getWindow().getAttributes();
lp.gravity = Gravity.BOTTOM;
Solution 13 - Android
The easiest way to set Dialog gravity in kotlin:
> dialog.window?.setGravity(Gravity.TOP) // Set the gravity here
private fun searchDialog() {
val dialog = Dialog(this)
dialog.window?.setGravity(Gravity.TOP) // Set the gravity here
val binding = DialogSearchHomeBinding.inflate(layoutInflater)
dialog.setContentView(binding.root)
dialog.show()
}
Solution 14 - Android
public class MyDialogFragment extends DialogFragment{
protected void setDialogGravity(int gravity) {
Dialog dialog = getDialog();
if (dialog != null) {
Window window = dialog.getWindow();
if (window != null) {
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
params.horizontalMargin = 0;
params.gravity = gravity;
params.dimAmount = 0;
params.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(params);
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater,container,savedInstanceState);
return inflater.inflate(R.layout.my_dialog, null);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setDialogGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
}
}
Solution 15 - Android
I have coded a custom Dialog, with a custom layout.
It has a cancel
and a save
button and you can set up also gravity on the device's screen (bottom) and define the width and height of the dialog.
private void showDialog(final String scanContent, final String currentTime, final String currentDate) { LayoutInflater linf = LayoutInflater.from(this); final View inflator = linf.inflate(R.layout.dialog_barcode_result_dialog, null);
final Dialog dialog = new Dialog(this, android.R.style.Theme_DeviceDefault_Light_Dialog);
// Setting dialogview
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM);
dialog.getWindow().setLayout(375, 350);
window.setLayout(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT);
dialog.setTitle(null);
dialog.setContentView(R.layout.dialog_barcode_result_dialog);
dialog.setTitle(getResources().getString(R.string.dialog_title));
dialog.setContentView(inflator);
final Button save = inflator.findViewById(R.id.saveBtn);
final Button cancel = inflator.findViewById(R.id.cancelBtn);
final TextView message = inflator.findViewById(R.id.dialog_message_text);
message.setText(scanContent);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();
}
});
dialog.show();
}
the dialog layout xml file is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:minWidth="350dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:layout_marginBottom="10dp"
android:id="@+id/dialog_message_text"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal">
<Button
android:id="@+id/cancelBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel" />
<Button
android:id="@+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_save" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Solution 16 - Android
in Java
AlertDialog.Builder builder = new AlertDialog.Builder(ConnectActivity.this);
AlertDialog alertDialog = builder.create();
// set the gravity
alertDialog.getWindow().setGravity(Gravity.TOP);
// set the margin
alertDialog.getWindow().getAttributes().verticalMargin = 0.1F;
alertDialog.show();
Solution 17 - Android
Java version of kotlin code suggested by A.I.Shakil :
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.setView(v)
.setCancelable(false).create();
dialog.getWindow().setGravity(Gravity.TOP);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.show();