I have a very simple solution (and I tested it - it works pretty well, as illustrated by the pictures I posted).
Imagine you have an invisible (GONE) generic View that covers the whole screen (match_parent, match_parent) and has a reddish semitransparent color.
It would become VISIBLE before showing the Dialog and GONE again after dismissing it.
Since it's GONE, you don't see it and it doesn't waste any space until it becomes VISIBLE.
This approach requires the outer container being a FrameLayout or a RelativeLayout (by setting the View properly: anchoring it to the four Parent's corners).
I used a RelativeLayout - because I really love these containers.
dlg_red_bg.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:padding="8dp"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@android:color/black"
android:padding="8dp"
>
<Button
android:id="@+id/btnTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:background="@drawable/ball"
android:text="Btn 1"
android:textColor="@android:color/white"
android:textSize="24sp"
android:textStyle="bold"
/>
<Button
android:id="@+id/btnTopRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/btnTopLeft"
android:background="@drawable/ball"
android:text="Btn 2"
android:textColor="@android:color/white"
android:textSize="24sp"
android:textStyle="bold"
/>
<Button
android:id="@+id/btnBottomLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnTopLeft"
android:layout_alignParentLeft="true"
android:background="@drawable/ball"
android:text="Btn 3"
android:textColor="@android:color/white"
android:textSize="24sp"
android:textStyle="bold"
/>
<Button
android:id="@+id/btnBottomRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnTopRight"
android:layout_toRightOf="@id/btnBottomLeft"
android:background="@drawable/ball"
android:text="Btn 4"
android:textColor="@android:color/white"
android:textSize="24sp"
android:textStyle="bold"
/>
<Button
android:id="@+id/btnDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/ball"
android:onClick="clickHandler"
android:text="Dialog"
android:textColor="@android:color/white"
android:textSize="24sp"
android:textStyle="bold"
/>
</RelativeLayout>
<View
android:id="@+id/vwRedOver"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:background="#8f00"
android:visibility="gone"
/>
</RelativeLayout>
code used to highlight the background
public void clickHandler(final View v)
{
switch(v.getId())
{
case R.id.btnDialog:
{
vwRedOver.setVisibility(View.VISIBLE);
final AlertDialog.Builder bld = new AlertDialog.Builder(this);
bld.setMessage("Some Message")
.setCancelable(true)
.setPositiveButton
(
"OK",
new DialogInterface.OnClickListener()
{
@Override
public final void onClick
(final DialogInterface dlg, final int id)
{
vwRedOver.setVisibility(View.GONE);
dlg.cancel();
}
}
)
.setNegativeButton
(
"Cancel",
new DialogInterface.OnClickListener()
{
@Override
public final void onClick
(final DialogInterface dlg, final int id)
{
vwRedOver.setVisibility(View.GONE);
dlg.cancel();
}
}
);
bld.create().show();
}
}
}
result
Before Clicking on "Dialog"
After Clicking on "Dialog"
NOTE 1: It's dark because of the black background
NOTE 2: You see a black border, because I set a padding on the outer RelativeLayout - you can remove it
After Clicking on either "OK" or "Cancel" - returns to the initial state (my Dialog doesn't do anything interesting on OK - It's only for demo purposes)