14

what I want to achieve is activity with dialog-like transparency with 100% visibility of RelativeLayout content. This is activity's xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="50dip"
        android:layout_marginLeft="8dip"
        android:layout_marginRight="8dip"
        android:layout_marginTop="50dip">
        (...)
    </RelativeLayout>
</LinearLayout>

and this is manifest:

<activity
        android:name="com.acentic.rcontrol.activities.MyActivity"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" >
</activity>

Right now background is still visible, what am I doing wrong?

--- EDIT: I added

android:background="#c0000000"

to LinearLayout. Now background is transparent as I wanted to, but also TextViews inside RelativeLayout are also transparent.. how to change it?

Mariusz
  • 1,825
  • 5
  • 22
  • 36
  • possible duplicate of [How to create Transparent Activity in Android?](http://stackoverflow.com/questions/2176922/how-to-create-transparent-activity-in-android) –  Jul 09 '13 at 07:50
  • set android:background="#000000" to the RelativeLayout – sweggersen Jul 09 '13 at 07:54
  • Check code in link which works for me. http://stackoverflow.com/questions/2176922/how-to-create-transparent-activity-in-android/38589105#38589105 – Naeem Ibrahim Jul 26 '16 at 11:50

5 Answers5

12

Try to set

    getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

in your activity.

You can also try to do this in a style:

 <style name="AppTheme" parent="@android:style/Theme.Translucent.NoTitleBar">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
</style>
sweggersen
  • 2,342
  • 1
  • 22
  • 27
  • 1st approach doesn't work by my side, 2nd gives full background transparency + TextViews inside RelativeLayout are semitransparent, which is not eligible – Mariusz Jul 09 '13 at 07:52
7

Create Style

<style name="MyTransparentTheme" parent="android:Theme.Dialog">
       <item name="android:windowIsTranslucent">true</item>
       <item name="android:windowBackground">@android:color/transparent</item>
       <item name="android:windowContentOverlay">@null</item>
       <item name="android:windowNoTitle">true</item>
       <item name="android:windowIsFloating">true</item>
       <item name="android:backgroundDimEnabled">false</item>

this is manifest:

<activity
            android:name="your package.activity"
            android:theme="@style/MyTransparentTheme">
        </activity>
Harshid
  • 5,701
  • 4
  • 37
  • 50
3

I added android:background="#c0000000" to LinearLayout. Now background is transparent as I wanted to, but also TextViews inside RelativeLayout are also transparent.. how to change it?

Add a solid background to your RelativeLayout element. This way the RelativeLayout will have a solid background, and only the margins will be transparent.

Andy Res
  • 15,963
  • 5
  • 60
  • 96
1

If your activity needs to extends from AppCompatActivity, you can create a custom style for your activity

<style name="transparentActivity" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

And then change the default style of your activity in the Manifest

<activity android:name=".MyActivity"
        android:theme="@style/transparentActivity"/>

Otherwise, if you do not need your activity to extend from AppCompatActivity, just change the theme of your activity in the manifest in this way.

<activity android:name=".MyActivity"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
Alejandro Fort
  • 545
  • 4
  • 9
0

After setting LinearLayout transparency try to set it's child transparency to the value that you want. This should make them have different transparency than LinearLayout. Try it.

Marek
  • 3,935
  • 10
  • 46
  • 70