Reputation: 166
I have a relative layout R and an alertdialog A. I want to set A's position and dimension same as R like this:
A.position_x = R.get_position_x()
A.position_y = R.get_position_y()
A.width = R.get_width()
A.height = R.get_height()
Upvotes: 0
Views: 2464
Reputation: 479
Get height,width and position x and y of relativelayout from below code
rel1 = (RelativeLayout)findViewById(R.id.rel1);
rel1.post(new Runnable()
{
@Override
public void run()
{
widthRel1 = rel1.getWidth();
heightRel1 = rel1.getHeight();
xRel1 = (int) rel1.getX();
yRel1 = (int) rel1.getY();
}
});
Then apply it to Dialog
final Dialog alertDialog = new Dialog(MainActivity.this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.template_menu);
Window window = alertDialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.x = xRel1; //x position
wlp.y = yRel1; //y position
wlp.height = heightRel1;
wlp.width = widthRel1;
wlp.gravity = Gravity.TOP | Gravity. LEFT;
window.setAttributes(wlp);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
alertDialog.show();
Upvotes: 0
Reputation: 3420
Use below code for set AlertDialog
at top of screen:
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setTitle("Title:");
builder.setMessage("Are you sure to Exit?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Set ypu positive code on OK button
finish();
}
});
// Setting Negative "NO" Btn
builder.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Dialog
AlertDialog dialog = builder.create();
dialog.getWindow().setGravity(Gravity.TOP);
dialog.show();
Upvotes: 2