Change android:windowBackground in Jetpack Compose - android

I want to implement Splash Screen using Jetpack Compose. In the old View system, we can just change the android:windowBackground via XML Theme.
How to do this in Compose?

As I looked into the AndroidManifest.xml, I saw that the app is still using old themes.xml for its activities.
<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.AppName">
From here I just edit the theme to apply a windowBackground
<style name="Theme.AppName" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="android:windowBackground">#color/black</item>
</style>
I did not create a new style and just use the main style. It's enough to just apply a white Surface on the root composable to hide the splash background when the activity showed up.

The Window Background is something specific to Android itself, it tells the system what background this activity draws "At All times".
For example, an activity with no view at all can still have a background, as it is defined in its theme. This is actually used for stuff like Splash Screens, as they have no UI, just a background. Since the background of an activity takes no time to draw, while the UI can get delayed.
Compose is a UI framework, and any UI will only be drawn when it's ready, meaning it will only show when the activity is ready. So a splash screen is impossible in Compose. More like, it's unrelated.
If you just want to set a background to your entire UI, maybe put the entire UI in a Box with a background. Or set the background colour in the MaterialTheme. And if you're not using a theme, just make a common Composable that's just a Box

Related

Setting dark or light theme of application by user choice (Android)

We are applying dark and light themes to an app. Our original theme is going to be light. In this situation I have a problem changing the real transition color of an activity. While switching between activities, I apply an animation using this code:
finish();
startActivity(new Intent(this, MainActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
This leaves a background that we set on the application theme on the manifest that goes like this:
<activity
android:name=".MainActivity"
android:theme="#style/MyStyle_Light" />
where the styles are something like this:
<style android:name="MyStyle_Light" parent="Theme.AppCompat.Light">
<!-- some style resources -->
</style>
<style android:name"MyStyle_Dark" parent="Theme.AppCompat">
<!-- some style resources -->
</style>
Now, the style Theme.AppCompat.Light has a light background on its window. It is defined within the activity from AndroidManifest.xml.
To change an activity's theme I use a code like this, and it works after seeing the original color that comes from the manifest at the very first opening.
#Override
public void onCreate()
{
setTheme(R.style.MyStyle_Dark); // can also be MyStyle_Light depending on user choice
super.onCreate();
setContentView(R.layout.MyLayout);
// rest goes on
}
The problem lies on the original color that appears after the fade-out and fade-in animation, because even if user chooses the dark theme, since MyStyle_Light is on manifest theme, they always see a light background on the fade out animation.
How can I change the background color that comes from the manifest?
Things I've tried or thought so far:
Using "android:windowBackground" which doesn't work,
Setting the theme on the application class without using a theme in manifest which also didn't work.
Using an attribute on the manifest to change styles at runtime from .xml which I don't think will work since there is nothing to refer to. (Just tried, it doesn't work.)
Using a duplicate activity with different theme resource attached to the manifest and starting it depending on user choice (seems unnecessary to me)
Any suggestion is appreciated, thanks.

Cordova Android set windowBackground programmatically at runtime

When the Cordova android app launches, a blank screen is briefly visible before cordova-plugin-splashscreen kicks in. I have learned that this is the windowBackground colour and can be changed by making a custom styles.xml and referencing it within AndroidManifest.xml though the activity's android:theme property. Example:
From AndroidManifest.xml:
<activity android:configChanges="orientation|keyboard|keyboardHidden|screenLayout|screenSize" android:label="#string/activity_name" android:launchMode="singleTask" android:name="MainActivity" android:screenOrientation="portrait" android:theme="#style/CustomStyle" android:windowSoftInputMode="adjustPan">
From styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomStyle" parent="#android:style/Theme.Material.Light.NoActionBar">
<item name="android:windowBackground">#drawable/init_splash</item>
</style>
</resources>
styles.xml references another file just containing a drawable colour.
This works. It allows me to change the colour that appears before the splash screen.
However, I am now looking to allow the user to optionally change to a dark theme. I have already figured out how to modify cordova-plugin-splashscreen to change the splashscreen based on the user preference, but I'm having trouble changing the windowBackground/theme programatically at runtime.
I have tried adding the following within MainActivity.java or CordovaActivity.java:
setTheme(R.style.CustomDarkStyle);
getWindow().setBackgroundDrawableResource(getResources().getIdentifier("init_splash_dark", "drawable", getPackageName()));
getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLACK));
I placed these in the onCreate before super.onCreate() or setContentView(). The window background colour does indeed change, but the initial blank screen before the splash stays at whatever colour was set in the manifest.
How can I change the activity/window background colour programmatically when the application is started?
Some have suggested changing the app theme to a transparent one to prevent the blank screen entirely, but that causes a delay in opening the app. I'm fine with the blank screen, I just want to change it's colour programmatically.
As of April 22nd, I am yet to find a solution to this problem.
Create a class with the same name as of your project and it will extend Application not Activity. Put this code inside this class as this will be automatically initialized as soon as your application starts. It will be acting as a constructor for your application.
Hope it helps!
I have a project named "slot" so i created a class named slot like this
package com.xyz.slot;
import android.app.Application;
public class slot extends Application {
#Override
public void onCreate() {
setTheme(R.style.CustomDarkStyle);
super.onCreate();
}
}
But make sure there is no theme set in manifest as it will not be overridden.

Default layout colour

I'm wondering why, every time I create a new Android project, Eclipse opens a main activity with a white background, when in previous versions, it was black.
Is there any way for me to change that back to black as a default color for future activities?.
They changed the default theme that eclipse uses in your resources to the light theme. Change your resource files (delete the stuff eclipse added that you don't use) and it will go back to black.
So if you want to reskin your whole application to black, you can use following line in your manifest file.
Write to Application tag attribute theme.
<Application
android:theme="#android:style/Theme.Black" //old targeted application
OR
android:theme="#android:style/Theme.Holo" //for new devices
... >
Now all activities in this application have black theme.
You can "override" this by using theme attribute in Activity tag.
<Activity
android:theme="some theme"
...>

Android activity Background affects alertDialog

I have a style I created like this:
<style name="myStyle" parent="#android:style/Theme.Dialog">
<item name="android:textColor">#FFFFFF</item>
<item name="android:background">#000099</item>
</style>
Then I created an xml and an activity, and in the manifest declared this
<activity android:name=".Try"
android:theme="#style/myStyle" >
</activity>
Then when I start this activity I have an xml with a background color (blue);
THe problem is , when I create an Alert Dialog
AlertDialog.Builder dialog = new AlertDialog.Builder....
It is also affected by this background (It looks more like a blue rectangle behind it that is bigger then the dialog and is coming out from all 4 sides).
I dont want it to be, I want to use another style for the alert dialog.
How can I disable this?
Are you saying that the background of your activity is too prominent when the dialog pops up? If so, you can blur what's in the background with the following code:
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Or are you saying that the background of your activity is being applied to the background of your dialog...?
I managed to fix that, well not entirley but to pass around it.
Instead of using:
#000099
I used
IMAGE_NAME
and I added a green rectangle image at the "drawable" folder.
This makes the background to become from an image instead of painting it, and the image does not affect the Dailog.
It will look a bit different (for example it will cover the white border as well) but it is better then before.

Application background image with transparent activities

hope you guys can give me a hand now! this is tricky to me
I have run into an requirement of my application that i cant find a way to do this.
I need my application to be fullscreen and no title bar (done), and this application will have a background image. However, all the activities/views of my application must be transparent/translucent so the application background will be visible all the time behind the information i am displaying.
Basically i would like to have this behavior
http://www.geeky-gadgets.com/wp-content/uploads/2010/05/amazon-kindle-android-app.jpg
supousing that the image you see is not the phone wallpaper, but the application background image. What would be the application configuration in the manifest file and what would be the configuration for eacy activity/view?
I also noticed that, i need to setup each activity to be fullscreen without title bar. Is there a way to do this globaly in the application so all activities will behave this way?
Many thanks for your time
To hide the title bar in all your activities you should set a custom theme in your AndroidManifest.xml file like so:
<application
android:icon="#drawable/application_icon"
android:label="#string/app_name"
stuff..
android:theme="#style/MyTheme">
Then define the theme in styles.xml:
<style name="MyTheme" parent="android:Theme.Black">
<item name="android:windowNoTitle">true</item>
</style>
For the background, I'm pretty sure you can just set a the background of a ListView to point to a .PNG that you have in your resource folder (it probably has to be the exact dimensions of the screen).

Categories

Resources