Change background color when a Popup is shown

Rajkiran picture Rajkiran · May 10, 2012 · Viewed 11.6k times · Source

I want to make the background darker when a PopupWindow is shown. Just like Dolphin Browser does like-

Before the PopupWindow

Look at the background color before the PopupWindow is shown

After the PopupWindow

And now see the background color after the PopupWindow is shown.

The background color is darker than what it was. So, how can we do this?

Answer

user2257590 picture user2257590 · Apr 8, 2013

In your xml file add something like this with width and height as 'match_parent'.

<RelativeLayout
        android:id="@+id/bac_dim_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#C0000000"
        android:visibility="gone" >
</RelativeLayout>

In your activity oncreate

//setting background dim when showing popup
back_dim_layout = (RelativeLayout) findViewById(R.id.bac_dim_layout);

Finally make visible when you show your popupwindow and make its visible gone when you exit popupwindow.

back_dim_layout.setVisibility(View.Visible);
back_dim_layout.setVisibility(View.GONE);