Cannot set PopupWindow background to transparent
Solution 1
What you do is create a transparent png (eg. clear.png) file and place it in your drawables folder, then call
yourPopUp.setBackgroundDrawable(getResources().getDrawable(R.drawable.clear));
This way the popup is still dismissible, which is not the case if you try to use null.
Solution 2
You can also set background transparent like below. This Worked for Me.
popup.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Radu
Embedded software developer, Android application developer. Developer of AndroidRC Python developer for testing scripts. I also like football :D
Updated on June 04, 2022Comments
-
Radu almost 2 years
I have the following code:
View child = getLayoutInflater().inflate(R.layout.contextual_menu_lp_activity, null) child.setBackgroundColor(getResources().getColor(R.color.transparent)); child.setBackgroundDrawable(new BitmapDrawable()); popup = new PopupWindow(MapViewActivity.this); popup.setContentView(child); popup.showAtLocation(MapViewActivity.mapView, Gravity.CENTER, 10,10); popup.setBackgroundDrawable(new BitmapDrawable()); child.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); popup.update(50, 50,child.getMeasuredWidth(), child.getMeasuredHeight());
As you may see, I set the view, and desperately try to make its background transparent.
On the xml layout, all the relativelayouts are given a transparent color background.
Here is the xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/transparent" > <RelativeLayout android:id="@+id/ivDialogPopup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:paddingBottom="40dp" android:background="@drawable/new_pop_up_bk" > <Button android:id="@+id/btLivePolice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/btSaveParkingLocation" android:layout_marginRight="4dp" android:layout_marginTop="8dp" android:background="@drawable/live_police_button_popup" /> <Button android:id="@+id/btSaveParkingLocation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="8dp" android:layout_centerHorizontal="true" android:background="@drawable/parking_location_button" /> <Button android:id="@+id/btGetDirections" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="4dp" android:layout_marginTop="8dp" android:layout_toRightOf="@+id/btSaveParkingLocation" android:background="@drawable/directions_button" />
Still, the view's parent layout(a relative layout) still has a gray background. (Previously, I was using the normal activity class and in the manifest i had a transparent theme -the most important part of it was setting this android:windowBackground to transparent- I have to change it for a different reason).
Any help is appreciated.
-
Radu over 11 yearsStill not working unfortunately. I have tried the suggested fix. It does better than the original: the parent layout is transparent. However, the image background is now no longer showing, and something gray is put in its place - but only over the image background, not over the entire layout!
-
prolink007 over 11 yearsCould you possibly draw the desired layout. Expressing which parts you are wanting transparent and which parts you are not wanting transparent? I am having difficulty understanding your desired layout.
-
Radu over 11 yearsI managed to fix it. I will post the results asap on SO, with explanations as well.
-
prolink007 over 11 yearsCool, looking forward to seeing what you were trying to achieve.
-
LKallipo about 11 yearsSo, what was the solution?
-
Radu about 11 years@LKallipo Never got around to it, but I will today or tommorow.
-
nikhil over 8 yearssetBackgroundDrawable takes a drawable as param
-
pt123 over 8 yearsI accidentally forgot the getResources.getDrawable()
-
Mark Delphi over 3 yearsso simple... Thanks!
-
hims_3009 about 3 years@MarkDelphi You are welcome. Glad it works for you.