How to prevent white background when app starts up in Jetpack compose - android

I have created an app with Jetpack compose and expected the start up background is black or some other colors, not white. This is my themes.xml
<style name="Theme.AlluringScreenshot" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="android:statusBarColor">#030318</item>
<item name="android:windowBackground">#030318</item>
</style>
The above style works well till on Android 11 but Android 12. My app still has white background at starting up. Any suggestions for this matter?

Use the splash screen API as a placeholder while jetpack compose is still processing

This is the author of this question.
After spending more time diving into some documents, I have found the spot for this matter. It looks very simple than any the solutions I found.
The android:windowBackground attribute only work till Android 11. This should be replaced by android:windowSplashScreenBackground to work well on Android 12.
That's all.

There must be some conflicting declaration.
Import material 3
implementation 'androidx.compose.material3:material3:1.1.0-alpha02'
implementation 'com.google.android.material:material:1.8.0-alpha02'
use in parent for the activity theme:
<style name="AppTheme" parent="Theme.Material3.DayNight.NoActionBar">
...
</style>
create a composable wrapper for the theme in Theme.kt for compose:
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Typography
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
#Composable
fun AppTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: #Composable () -> Unit
) {
MaterialTheme(
colorScheme = when (darkTheme) {
true -> darkColorScheme()
else -> lightColorScheme()
},
typography = Typography(),
content = content
)
}
and then wrap your composable screens with :
AppTheme{
MyComposableScreen()
}
if you have to change colors do it in darkColorScheme() nad lightColorScheme(), if it is not enough then change the xml attributes

Related

How to change status Bar Color in Jetpack Compose + Material 3

I'm following the theming tutorial from developer.google. I'm trying to migrate the app to Mat. 3 and modify the status bar's color to match with the background color.
After adding android:statusBarColor and android:windowLightStatusBar, nothing changes.
I noticed that at the very first moments the app loading, status bar is really as my expected, but a moment later it becomes wrong.
Before: https://i.stack.imgur.com/i43cL.png
After: https://i.stack.imgur.com/kMwaP.png
What I've tried:
// res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Superheroes" parent="android:Theme.Material.Light.NoActionBar">
<item name="android:statusBarColor">#color/background_light</item>
<item name="android:windowLightStatusBar">true</item>
<item name="android:windowBackground">#color/background_light</item>
</style>
</resources>
// res/values-night/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Superheroes" parent="android:Theme.Material.NoActionBar">
<item name="android:statusBarColor">#color/background_dark</item>
<item name="android:windowLightStatusBar">false</item>
<item name="android:windowBackground">#color/background_dark</item>
</style>
</resources>
In Jetpack Compose the easiest is to use the Accompanist System UI Controller.
Add this to your dependencies : implementation "com.google.accompanist:accompanist-systemuicontroller:0.27.0"
Then, in your MainActivity:
setContent {
MyTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
val systemUiController = rememberSystemUiController()
SideEffect {
systemUiController.setStatusBarColor(
color = Color(0xff655D8A),
)
}
}
}
}
Source and details here.
The compose samples project Jetchat does this through xml themes.
Be sure to cover all android versions as they do in the various themes.xml files in the same way they do or you'll end up with some weird behaviour on specific versions.
Another way to change it without adding a dependency is at ui.theme/Theme.kt as #2801938 has mentioned:
#Composable
fun YourAppTheme(darkTheme...) {
...
val view = LocalView.current
if (!view.isInEditMode) {
SideEffect {
val window = (view.context as Activity).window
window.statusBarColor = colorScheme.primary.toArgb()
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
}
}
...
}

Set background color for the whole app. Android, Jetpack Compose

I need to set background color for the whole app. In xml, we use android:background tag in fragment or activity.
What analog Compose has? Surface argument for theme colorPalette doesn't help. Look for your solutions.
You can place your app inside a Box with needed background:
setContent {
YourTheme {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Yellow)
) {
YourApp()
}
}
}
Put a window background color from your theme, which will set the background of your whole app, and also remove the white flicker while the app is loading (which looks quite odd if you have a color background in your app):
res/values/themes/themes.xml
<style name="Theme.YourThemeName"
parent="Theme.MaterialComponents.DayNight.DarkActionBar">
...
<item name="android:windowBackground">#color/purple_700</item>
</style>

How to create splash screen theme on compose theme.kt file

I want to moving all theme related xml code to compose file. There is a splash screen xml code, is it possible to let it in compose style?
<!-- Splash screen theme. -->
<style name="splashScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/splash_screen</item>
</style>
#Composable
fun splashScreenTheme() {
}
As of Compose 1.0.1 / August 2021, there is no way to do this completely in Compose. I am currently using the XML/android:windowBackground based solution in the OP for my app which is pretty much entirely Compose otherwise.
However, note that Android 12 is introducing a SplashScreen API which lets you define a splash screen consisting of an adaptive icon and solid background colour.
Also, remember that things like date/time pickers are still not yet available natively in Compose, so if you use the Material Components library for that, you will still need the XML style files.

Android - Stripe - Applying style to PaymentMethodsActivity

I'm currently implementing Stripe into my application.
And using the example code from their documentation, I'm starting their PaymentMethodsActivity like this.
private fun startPaymentSelectActivity() {
val intent = PaymentMethodsActivity.newIntent(this#PaymentActivity)
startActivityForResult(intent, REQUEST_CODE_SELECT_SOURCE)
}
However, the created PaymentMethodsActivity's theme does not follow my app's theme, it's using their blue Toolbar. Like this
How do I apply a Theme to this Activity?
ScreenShot attached here
1.Download Stripe from here
"https://github.com/stripe/stripe-android"
2.import stripe module in your project and Update UI as you want from it's res folder
You can add this into your styles.xml and customize the colors
<style name="StripeDefaultTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#0091ea</item>
<item name="colorAccent">#color/accent_color_default</item>
<item name="colorControlNormal">#color/control_normal_color_default</item>
<item name="titleTextColor">#android:color/white</item>
<item name="android:textColorSecondary">#android:color/secondary_text_light</item>
</style>
Be careful to keep the style name ( name="StripeDefaultTheme" )
I'm using 'com.stripe:stripe-android:6.1.2' and that's work fine.
Source: https://github.com/stripe/stripe-android/issues/414

Is there a way to change the Android status bar color with React Native?

I just got started with React Native for Android, and I'm trying to figure out if there's a way to change the status bar color for Android...
Like this?
You can use React Native Status Bar(detailed description here). All you need to do is wrapping navigator with a view and adding a StatusBar component above it. Don't forget to import StatusBar from 'react-native' package.
<View>
<StatusBar
backgroundColor="blue"
barStyle="light-content"
/>
<Navigator
initialRoute={{statusBarHidden: true}}
renderScene={(route, navigator) =>
<View>
<StatusBar hidden={route.statusBarHidden} />
...
</View>
}
/>
</View>
One thing I've noticed is that you should style the parent View with flex:1, without it you'll just see a white blank screen. It's not mentioned in RN Documents though.
Yes you can:
import {StatusBar} from 'react-native';
componentDidMount() {
StatusBar.setBarStyle( 'light-content',true)
StatusBar.setBackgroundColor("#0996AE")
}
You can use react-native in-build StatusBar function
import {StatusBar} from 'react-native';
render() {
return <View>
<StatusBar
backgroundColor="#264d9b"
barStyle="light-content"
/>
... //Your code
</View>
}
reference:
https://facebook.github.io/react-native/docs/statusbar
I've made an npm package to control the StatusBar in android
https://www.npmjs.com/package/react-native-android-statusbar
The color changes do not reflect for versions before 21
There is no way currently to do that from JS. You can customize it by using a custom theme. Check out android/src/main/res/values/styles.xml file from your project (template is here: https://github.com/facebook/react-native/blob/master/local-cli/generator-android/templates/src/app/src/main/res/values/styles.xml) and read more here: https://developer.android.com/training/material/theme.html
Add this code on your header component
androidStatusBarColor="#34495e"
add color.xml in ..android/app/src/main/res/values and pate following code
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- color for the app bar and other primary UI elements -->
<color name="colorPrimary">#3F51B5</color>
<!-- a darker variant of the primary color, used for
the status bar (on Android 5.0+) and contextual app bars -->
<color name="colorPrimaryDark">#A52D53</color>
<!-- a secondary color for controls like checkboxes and text fields -->
<color name="colorAccent">#FF4081</color>
</resources>
copy and pate following code in ..android/app/src/main/res/values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
If you are using Expo for React Native then here is the solution for setting Android Status Bar Color.
First of all, In your app.json file add the code:
{
"expo": {
"sdkVersion": "Your given Version",
"androidStatusBar": {
"backgroundColor": "#4e2ba1" (Your desirable android Status Bar Color before the app loads)
}
}
}
And then Go to Your Main Component or App.js, import 'StatusBar' from 'react-native'. Then add Following Code in return:
return(
<View style={{flex: 1}}> (Do not forget to style flex as 1)
<StatusBar translucent backgroundColor="rgba(0,0,0,0.2)"/>
<Your Code>
</View>
);
Here, we are setting the status bar color as Black but with 0.2 opacity. Your statusBar Color will be the same as your headerStyle background Color for Stack Navigator but a bit darker. For standard Android App, this is how the StatusBar color is set. Also, Make it translucent so that your app draws under the status Bar and looks nice.
It hope this works perfectly for you.
There is no exposed API for now. This will work only from Android 5.0.
Working on a bridge module to achieve the same. Will keep you posted
Just add the following code to your App.js file inside your class component.
componentDidMount(){
StatusBar.setBarStyle( 'light-content',true)
StatusBar.setBackgroundColor("Your color Hex-code here")
}
And add this to your import statements.
import {StatusBar} from 'react-native';
If you guys are using expo then just add this in the app.json
"androidStatusBar": {
"backgroundColor": "#ffffff",
"barStyle":"dark-content"
}
Refer: https://docs.expo.io/versions/latest/guides/configuring-statusbar/
Use backgroundColor Prop in the StatusBar Component
<StatusBar backgroundColor="#yourColor" />
Check docs more information : https://reactnative.dev/docs/statusbar
You can use react-native-navigation-bar-color this module to change NavigationBar, install it using npm i react-native-navigation-bar-color

Categories

Resources