How do I create a transparent Activity on Android?
Add the following style in your res/values/styles.xml
file (if you don’t have one, create it.) Here’s a complete file:
<?xml version="1.0" encoding="utf-8"?><resources> <style name="Theme.Transparent" parent="android:Theme"> <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> </style></resources>
(The value @color/transparent
is the color value #00000000
which I put in the res/values/color.xml
file. You can also use @android:color/transparent
in later Android versions.)
Then apply the style to your activity, for example:
<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">...</activity>
It goes like this:
<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
In the styles.xml:
<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar"> <item name="android:background">#33000000</item> <!-- Or any transparency or color you need --> <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></style>
In the AndroidManifest.xml:
<activity android:name=".WhateverNameOfTheActivityIs" android:theme="@style/Theme.AppCompat.Translucent"> ...</activity>