Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set PopupWindow background to transparent

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.

like image 996
Radu Avatar asked Dec 19 '12 16:12

Radu


2 Answers

You can also set background transparent like below. This Worked for Me.

popup.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
like image 149
hims_3009 Avatar answered Oct 12 '22 22:10

hims_3009


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.

like image 45
pt123 Avatar answered Oct 12 '22 23:10

pt123