I created a new empty activity in Android Studio
I wanted to build a custom home screen launcher for my private usage. I already build the concept art and so on, then it was time to build it. But the background color never changed to anything but white.
The code for the MainActivity.kt:
package com.private.homescreen
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
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 com.private.homescreen.ui.theme.HomescreenTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
Scaffold(modifier = Modifier.fillMaxSize().background(color = Color(0xFF00FF00))) { innerPadding ->
Greeting(
name = "Android",
modifier = Modifier.padding(innerPadding)
)
}
}
}
}
The themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Homescreen" parent="android:Theme.Material.Light.NoActionBar">
<item name="android:windowShowWallpaper">true</item>
<item name="android:windowBackground">#FF0000FF</item>
</style>
</resources>
And the AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Homescreen"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.Homescreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Most of this code is just the default code from Android Studio
I tried all of the color changes (as you can see with the Color(0xFF00FF00))
and the <item name="android:windowBackground">#FF0000FF</item>
nothing seamed to work, the background always remained white, no matter the dark-mode setting on the device.
The only thing that kind of worked was to start the app wrapped in the MaterialTheme and then remove it white it was running and then the live preview changed green.
Thats it, nothing else effected the background in the slightest.
The end goal is to have the application see-through, but first I’ll have to figure out why everything is white.
I tried clearing and rebuilding, using multiple different virtual devices, nothing seamed to work.
If you go into the layout inspector and view the scaffold, you can see that the Scaffold has the containerColor set to #FFFBFE
(that’s where the white background comes from).
If you set the containerColor attribute to #00FFFFFF
you will notice that the white background is gone.
Scaffold(
modifier = Modifier.fillMaxSize(),
containerColor = Color.Transparent
){...}
If you continue and remove all other backgrounds the app will become transparent.
I have this helper Modifier
:
@Composable
fun Modifier.customBackground(
radius: Float = 0f,
color: Color = colorResource(id = R.color.bg_primary)
): Modifier {
return this
.background(color = color, shape = RoundedCornerShape(radius.dp))
}
Then in your composable you can use it like this:
Column(
modifier = Modifier
.customBackground()
) {
// Content
}