Create Android Transparent Activity in 3 Possible Ways
Sometimes we need to have android transparent activity and transparent screen in our Application, which is totally possible by playing around with the android activity and resource file a little bit. You can create transparent activity on android by applying the possible three ways to accomplish your goal.
Possible Solution 1
Look for the style.xml file in the resource folder, or simply go to res/values/styles.xml, open it and add the following code to it.
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="Theme.Transparent" parent="android:Theme"> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:backgroundDimEnabled">false</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowNoTitle">true</item> <item name="android:windowContentOverlay">@null</item> </style> </resources> |
The value of @color/transparent is #000000, which you can add to your project. (if you’re not familiar to it then follow the below)
Now create another file in the resource folder and name it as “colors.xml” and paste the following code in it.
1 |
<color name="transparent"> #00000000 </color> |
You can add more colors to it according to your needs.
In your activity file, you can now add the following transparent background to your activity tag.
1 2 3 4 5 |
<activity android:theme="@style/Theme.Transparent" android:name=".SampleActivity"> ... // Your Code Goes Here #### // To do </activity> |
If Your App is Crashing then it is derived from AppCompatActivity, so remove parent=”android:Theme” from the parent tag and it will start working.
[Android Error] Facing error in the intent filter? get your solution now.
Possible Solution 2
Adding this code to your activity tag in manifest file will make android transparent activity. Assign Translucent Theme to the activity and it will work fine.
1 2 3 |
<activity android:theme="@android:style/Theme.Translucent.NoTitleBar" android:name=".usual.activity.Declaration" /> |
Possible Solution 3
1 |
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); |
This is also a possible way to create android transparent activity, just add that one line to your onCreate function, just below the setContentView which will make the background completely transparent.
For the dialogs, you can use this code to make an Android transparent activity
1 |
getWindow().getDecorView().setBackgroundResource(android.R.color.transparent); |
Final Words on Android Transparent Activity
These three possible solutions or ways can help you to create transparent activities on android. If none of them works for you just let us know in the comment section, we will try our best to get back to you as soon as possible. Please provide us with all the details, what you’ve already applied and little snap your code would be highly appreciated.