My use case is writing an overlay controller activity for a landscape camera preview. I followed the instructions from a couple of tutorials for writing a transparent theme.
So my res/values/style.xml looks like this:
<resources>
<style name="Theme" parent="android:Theme" />
<style name="Theme.Transparent">
<item name="android:windowBackground">@drawable/transparent_background</item>
</style>
<drawable name="transparent_background">#00000000</drawable>
</resources>
The activity snippet:
<activity android:name=".CameraPreview"
android:label="Camera"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Controlls"
android:label="Controlls"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent">
</activity>
When I start this activity from my root activity, the layout gets drawn correctly, but the background stays black. I tried to use @android:style/Theme.Translucent
instead, but this Theme inherits the orientation from the calling activity (landscape) and thats not what I want.
Edit:
The application holding the camera preview is set to landscape view as it does not display the preview correctly in portrait orientation. (see old google bug report)
What I wanted to do was to put an independent activity for user interaction interface in front of the camera surface holder (this activity should be set to 'portrait', or even better to 'sensor')
In Android, we can create a transparent activity that will not be visible but your application will be running. The best part of this transparent activity is that you can create a transparent activity by just changing the resource file and we need not write the java or kotlin code for the same.
Here's a somewhat related answer for anyone else that has a similar problem.
tl;dr
Adding a new style where the name is a suffix of an existing style can cause problems (like making transparent activities have a black screen).
Problem: Our transparent activity background was black after doing a large refactor. (The activity was already using a transparent theme.)
After several hours of going through commits I found what seemed to be the cause of the problem. In our app we use styles like CSS. There was an existing style like this that we applied to a TextView.
<style name="HeadLine.SM.White.MyFontBold">
<item name="android:fontFamily">@font/some_bold_font</item>
</style>
The non-bold variant of the style was added as a style before the bold variant as below.
//This style was added
<style name="HeadLine.SM.White.MyFont">
<item name="android:fontFamily">@font/some_font</item>
</style>
<style name="HeadLine.SM.White.MyFontBold">
<item name="android:fontFamily">@font/some_bold_font</item>
</style>
For some reason, after the non-bold style variant was added all transparent activities had a black screen. When we changed the non-bold variant style name to something else it fixed the problem for us. Now our styles look like this (I know there are better ways to handle font styles - these styles are a few years old).
<style name="HeadLine.SM.White.MyFontRegular">
<item name="android:fontFamily">@font/some_font</item>
</style>
<style name="HeadLine.SM.White.MyFontBold">
<item name="android:fontFamily">@font/some_bold_font</item>
</style>
It seemed that adding a new style where the name is a suffix of an existing style caused problems. If you're adding a new style make sure the name is not a suffix of an existing style.
We did try cleaning the build, rebuilding, and invalidating Android Studio caches. None of these things solved our problem.
Try the built-in @android:style/Theme.Translucent
instead of your custom one, according to this blog post. I haven't tried to do this myself, so I don't know if the technique written about there works or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With