In my android project, when I set a background for a dialog in the xml code (the dialog uses a xml file for the layout), the dialog window size increases to fit the background image in it.
How can I set it so that the window size does not increase. It just ignores the background part that won't appear in the dialog. I guess the anchor of the image can be the center or the top left, doesn't matter.
Does anyone know how to do this?
Thanks.
If you are setting it in an xml, you can just set the width and height to a set amount, like this:
android:layout_width="100dp"
android:layout_height="50dp"
or, better yet, use dimensions so you can adjust with different layouts. Or you can set them manually like this below.
Here is a nifty little alorithm that Ram kiran used in this answer
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle("Title");
alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(600, 400); //Controlling width and height.
I found the solution, see here: Scale down (or crop) dialog background in Android
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:src="@drawable/back_dialog"
android:scaleType="fitXY"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignTop="@+id/dialog_layout_main"
android:layout_alignBottom="@id/dialog_layout_main"
android:layout_alignLeft="@id/dialog_layout_main"
android:layout_alignRight="@id/dialog_layout_main" />
<LinearLayout
android:id="@+id/dialog_layout_main"
android:layout_width="match_parent"
android:layout_height="wrap_content">
.........
</LinearLayout>
</RelativeLayout>
Either set layout width and height for the parent layout static or do it for the ImageView. Setting match_parent, wrap_content or fill_parent(deprecated) will make dialog to expand to ImageView.