I want to create a transparent Activity on top of another activity.
How can I achieve this?
I just did two things, and it made my activity transparent. They are below.
In the manifest file I just added the below code in the activity tag.
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
And I just set the background of the main layout for that activity as "#80000000". Like
android:background="#80000000"
It perfectly works for me.
It goes like this:
<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
Along with the gnobal's above solution, I had to set alpha to 0 in the layout file of that particular activity, because on certain phone (Redmi Narzo 20 pro running on Android 10) a dialog portion of the screen was showing with the screen that was supposed to be transparent. For some reason the windowIsFloating was causing this issue, but on removing it I wasn't getting the desired output.
Steps:
Add the following in the style.xml located under res > values > styles.xml
<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>
<item name="android:colorBackgroundCacheHint">@null</item>
</style>
Set the theme of the activity with the above style in AndroidManifest.xml
<activity
android:name=".activityName"
android:theme="@style/Theme.Transparent"/>
Open your layout file of the activity on which you applied the above style and set it's alpha value to 0 (android:alpha="0"
) for the parent layout element.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0"/>
</androidx.constraintlayout.widget.ConstraintLayout>
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>