Cordova Android set windowBackground programmatically at runtime - android

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.

Related

Have trouble setting up Toolbar in android studio

I am new to android development, (Rather of a PHP background) and tried adding a toolbar to an app I am building. The toolbar works but requires me to initialize it in every activity! Surely there must be a better way than that? I tried creating another class SetupActivity extending AppCompactActivity and moved all the repeated code in there. But no activity calls the onCreate in SetupActivity.Then I tried using a fragment but it is not a subclass of Context.Please help me find a way to fix it. Thanks!
Edit: I have to use the setSupportActionBar in every activity before I can get it to display.
Also, The app bar has a button that sends the user to another activity. I have to create the button using Java and then add a click listener to it in every activity.
One way of doing this is through xml:-
What you can do is first create a style in your app/src/main/res/values/themes.xml file.
In the following example, I'm removing the action bar from my app
Eg:
<style name="NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
Then set this theme as the "android:theme" in the "application" tag of your app/src/main/AndroidManifest.xml file. Eg:
<application
android:allowBackup="true"
android:icon="#mipmap/app_icon"
android:label="#string/app_name"
android:roundIcon="#mipmap/app_icon_round"
android:supportsRtl="true"
android:theme="#style/NoActionBar"/>
This way the style which you have created (containing the new toolbar) will be applied across your entire application.

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.

Android how to set ActionBar in just one Activity

I am new to Android and I'm facing this problem.
I got MainActivity with items list, when one of them is clicked then the DetailActivity starts.
I disabled ActionBar with
<style name="AppTheme" parent="android:Theme.Material.Light.NoActionBar">
so the MainActivity no longer have actionBar. But I want this ActionBar in the DetailActivity ( i need the basic one, with the left arrow to get back to MainActivity ), so I created another style
<style name="DetailTheme" parent="android:Theme.Material.Light.DarkActionBar">
and used it in activity_detail.xml like this
android:theme="#style/DetailTheme"
But it seems this is not the right way, because there is no ActionBar in that Activity .
I'm currently not sure if DetailActivity will be the only one with ActionBar, so I would like to know how to activate it on just this one Activity.
What is the right solution for this ?
Not sure but maybe try assigning it to the activity in the manifest
<activity android:name=".DetailActivity"
android:theme="#style/DetailTheme"/>
used it in activity_detail.xml like this android:theme="#style/DetailTheme"
My best guess is that you mean you added the android:theme attribute to the root view tag in your layout file. Maybe something like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:theme="#style/DetailTheme"
...
/>
This doesn't actually apply the theme to your Activity, it only applies the theme to your Activity's "content view". Content views don't have action bars, so those attributes of the theme will be ignored.
To apply a theme to an Activity, you have to specify it in AndroidManifest.xml
<activity
android:name=".DetailActivity"
android:theme="#style/DetailTheme"/>

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"
...>

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