Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Issue with transparent activity

Recently, on one of our production apps, transparent activity has stopped working. By this I mean that it became a black background instead of transparent. When I set activity's background color to solid one (i.e. red, green etc.), it's applied w/o issues. Probably the issue was caused by migration to AndroidX, but I don't have evidences for this.

After many hours of debugging, testing and reading through related SO topics, I was finally able to identify circumstances, under which the issue is happening.

My test environment is a very simple clean project with two activities (you can check full code under the link).

Conditions for working state

I'm able to make second activity transparent only if my 'themes.xml' file is very simple. You can see first activity in the background:

enter image description here

Conditions for non-working state

It's enough to add a simple style, even without items inside and with no parents, to cause background to be black instead of transparent:

enter image description here

Here is my 'themes.xml':

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyApplication" parent="Theme.AppCompat.Light">
        <item name="android:windowBackground">@android:color/white</item>
    </style>

    <style name="Transparent" parent="Theme.AppCompat.Dialog">
        <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>

    <!--    By removing this style I can make transparent activity to work -->
    <style name="ScrollViewStyle" />

</resources>

And 'AndroidManifest.xml':

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">
        <activity
            android:name=".SecondActivity"
            android:label="@string/title_activity_second"
            android:theme="@style/Transparent" />
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_first">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I hope someone will be able to help me getting technical explanation of this strange behavior.

like image 804
Myroslav Avatar asked Jan 19 '21 16:01

Myroslav


1 Answers

As a workaround, I was able to make activity transparent by setting its theme to android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" directly in Manifest. Note 'directly', as it will not work, if I even create custom empty theme with above mentioned theme as a parent. But this solution has limitations:

  • not possible to customize behavior, like using Dialog enter/exit transition animations etc.
  • activity should extend Android Activity, and not AppCompatActivity.
  • I also had to set transparent background for a root view of this activity to achieve transparency.
like image 92
Myroslav Avatar answered Sep 28 '22 04:09

Myroslav