Rajkiran
Rajkiran

Reputation: 16193

Change background color when a Popup is shown

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?

Upvotes: 6

Views: 13223

Answers (4)

Jun Shi Yan
Jun Shi Yan

Reputation: 91

fun setBackgroundAlpha(activity: Activity, bgAlpha: Float) {
    val lp: WindowManager.LayoutParams = activity.getWindow().getAttributes()
    lp.alpha = bgAlpha
    activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
    activity.getWindow().setAttributes(lp)
}

Upvotes: 0

user2257590
user2257590

Reputation: 359

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);

Upvotes: 5

Abhishek
Abhishek

Reputation: 104

try this code if your popup is an activity then it will help definetly. make a mystyle.xml file in values folder of your project and make these changes.

<resources> 
   <style name="customStyle" parent="@android:style/Theme.Dialog">
     <item name="android:windowBackground">@android:color/transparent</item>
   </style>
</resources>

do this change in menifest.xml

<activity android:name="yourActivity" android:theme="@style/customStyle"></activity>

Upvotes: 0

5hssba
5hssba

Reputation: 8079

If i am not wrong... you can create an activity with listview.... and put the theme as dialog in its manifest like this..

 <activity android:theme="@android:style/Theme.Dialog" />

this will make the background darker..

Upvotes: 0

Related Questions