I have a single activity with a simple layout (a bunch of coloured panels) and I’m trying to overlay a small moving graphic on a transparent view over the top. Like @umar here I’m following this tutorial but my overlay view is refusing to be transparent. I can make the view appear, it contains just what it should, but the background of the view is black and however I try to make it transparent fails. I’ve tried a few of the solutions mentioned on SO and it always comes out the same.
My layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/everything"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#00ffffff"
>
<LinearLayout
android:id="@+id/row_1_bg"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal"
>
<FrameLayout
android:layout_height="fill_parent"
android:layout_width="100dp"
>
<LinearLayout
android:id="@+id/row_1_table"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#808080"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal"
>
<TextView
android:id="@+id/row_1_pantone"
android:paddingLeft="6dip"
android:textColor="#f0f0f0"
android:textStyle="bold"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal"
>
<TextView
android:id="@+id/row_1_rgb"
android:paddingLeft="6dip"
android:textColor="#f0f0f0"
android:textStyle="bold"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal"
>
<TextView
android:id="@+id/row_1_hex"
android:paddingLeft="6dip"
android:textColor="#f0f0f0"
android:textStyle="bold"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</TextView>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingRight="5px"
android:paddingTop="5px"
android:paddingLeft="5px"
android:gravity="bottom"
android:orientation="vertical"
android:background="#00808080">
<barry.pantone.TransparentPanel
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/transparent_panel"
android:paddingRight="5px"
android:paddingTop="5px"
android:paddingLeft="5px"
android:paddingBottom="5px"
android:background="#00808080">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/button_click_me"
android:text="Click Me!"
/>
</barry.pantone.TransparentPanel>
</LinearLayout>
</LinearLayout>
My TransparentPanel.java is straight from the tutorial, using:
innerPaint.setARGB(225, 75, 75, 75);
to make a transparent panel, and this is my onCreate from the main activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
sPantone = res.getStringArray(R.array.pantone); // gets colour definitions
iHeight = getWindowManager().getDefaultDisplay().getHeight();
PopulateTable (); // draws colours in the various layout elements
}
The problem is that whatever I do inside the view is fine – I can draw a semi transparent panel over other elements in that view, but the overlay view itself has a black background which obscures the layout beneath. If anyone can help me understand why transparency doesn’t work, I will be eternally grateful.
Many thanks
Baz
EDIT
Following another idea I tried moving the definition of the overlay out into its own XML file and using a LayoutInflater to inflate and add the new view. Nope. Exactly the same – the additional view has a solid background. Here’s the tranny.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:background="#80000040"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingRight="5dip"
android:paddingTop="5dip"
android:paddingLeft="5dip"
android:gravity="bottom"
android:orientation="vertical"
>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/button_click_me"
android:text="Click Me!"
/>
</LinearLayout>
and here’s the code from onCreate to inflate the new view and add it to the main layout:
private View view;
ViewGroup parent = (ViewGroup) findViewById(R.id.everything);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.tranny, parent, false);
parent.addView(view);
Basically I’ve gone about creating exactly the same result using a different technique. Handy to learn about LayoutInflater for the first time, but no help re transparency 🙁
OK, no answers so I’m assuming there’s no way to use
parent.addView(view)to add an overlaid view which has a transparent background. I had hoped to add a narrow transparent view over the right hand edge of my layout (a scrollbar) into which I could draw a bitmap indicating the position in the big list of displayed items.Instead I have now made my whole layout slightly narrower and left room for a narrow
LinearLayoutdown the right edge, which has a black background, and into that I draw my bitmap, so I get this:I would have preferred to put the pointer over the top of the blocks of colour, but unless some guru in the future finds this question and shows us how to do it, I’m close enough.
Baz.