I'm developing an Android application with a transparent background. It works fine, but in the recents view, the activity turns grey instead of retaining its transparency. I've tried several approaches but haven't been able to resolve the issue.
Here is my MainActivity.kt
package b.os.calculator
import android.os.Bundle
import android.view.View
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import b.os.calculator.ui.theme.CalculatorTheme
import android.view.Window;
import android.view.WindowManager;
import androidx.compose.material3.MaterialTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.Theme_Calculator)
super.onCreate(savedInstanceState)
enableEdgeToEdge()
window.setBackgroundDrawableResource(android.R.color.transparent)
setBlurEffect()
enableEdgeToEdge()
setContent {
CalculatorTheme {
TransparentBackgroundContent()
}
}
}
private fun setBlurEffect() {
window.setBackgroundBlurRadius(169) // Adjust the radius as needed
}
}
@Composable
public fun TransparentBackgroundContent() {
val transparency = 0.9f
// Define your primary Material color (e.g., Primary color)
val primaryColor = MaterialTheme.colorScheme.background // Replace with your actual primary color
// Create a Color with the desired transparency and tint
val transparentPrimaryColor = primaryColor.copy(alpha = transparency)
Box(
modifier = Modifier
.fillMaxSize()
.background(color = transparentPrimaryColor)
) {
// Add other UI elements but now removed for simplicity
}
}
@Preview(showBackground = false)
@Composable
fun TransparentBackgroundContentPreview() {
CalculatorTheme {
TransparentBackgroundContent()
}
}
I've added a blur effect and set the window background to transparent. The activity displays correctly while in use, but the transparency effect doesn't carry over to the recents view.I also tried to integrate a function to safely set the task description, but the app crashes when I do this:
private fun setTaskDescriptionSafely() {
try {
val taskDescription = ActivityManager.TaskDescription.Builder()
.setLabel(null) // Use default app label
.setIcon(null) // Use default app icon
.setPrimaryColor(ContextCompat.getColor(this, android.R.color.transparent)) // Set background color to transparent
.build()
setTaskDescription(taskDescription)
} catch (e: Exception) {
e.printStackTrace() // Handle any potential exceptions
}
}
Screenshots:
Normal view: When my app runs normally
Recents view: When the app goes in the recents
I've been searching for a solution but haven't had any luck. How can I ensure that the activity maintains its transparency in the recents view?
BTW I AM USING API 34 (Android 14) And using a Pixel 7a vitual device running Android 15 Api 35
Thanks in advance for your help!