Setting dark or light theme of application by user choice (Android) - 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.

Related

Apply DayNight theme to selected fragment

I have an app that consists of two fragments connected with ViewPager. The one on the left has custom background, the one on the right has basic light theme (white background, black text color, etc.). Now I want to implement DayNight theme to the right fragment (not to the one that has custom background). I tried to set DayNight to the main activity in AndroidManifest, but it changed the whole app - the left fragment and the right fragment.
Manifest:
android:theme="#style/MyTheme"
Styles.xml:
<style name="MyTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
...
</style>
Is it possible to set DayNight theme to only one of them? Thank you very much for your advice.

Set theme programatically to Android system preview window

I have an app in which the user can choose between the default theme and a dark theme. The manifest applies AppTheme to the <application>, while each activity checks the relevant flag and uses setTheme(AppTheme.Dark) if necessary.
This implementation works well for all my activities, but the preview window shown by the OS before the application launches uses the Manifest theme, i.e. the light one. So users get a sudden white screen before reaching the dark app.
Based on other SO answers, I gathered that the only customization we can do is to set a windowBackground drawable for the window to use (and a statusBarColor if needed).
Even though I have two different windowBackground set for each theme, I can't change the theme for the preview window since the Application class seems to load after it, and setting theme there makes no difference.
I'd like to avoid disabling the preview since it adds a noticeable delay without much indication that the app is launching. Is there any solution to this? I'm considering having a dark color for both themes in the worst case, like the Reddit app does.
Styles:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- primary and accent color attributes -->
<item name="android:windowBackground">#color/activityBackgroundLight</item>
</style>
<style name="AppTheme.Dark" parent="AppTheme">
<!-- primary and accent color attributes -->
<item name="android:windowBackground">#color/activityBackgroundDark</item>
</style>
Note: While I'm using colors here, I've already tried with a gradient drawable and it doesn't change anything.
UPDATE: I wasn't able to get any other method, so I went ahead and applied the dark theme by default on the SplashActivity. This means the preview window is always dark, while the check in the SplashActivity's onCreate is inverted (compared to other activities) and applies the light theme if needed.
Still looking for a solution that actually lets me switch at runtime.

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.

Android preference title/summary text style

I have a couple custom preference items -- one that displays a swatch of the currently selected color, and another one that displays a thumbnail.
I have a custom layout for these that matches up very well, and have found that I can make the text appearance match by using android:textAppearance="?android:attr/textAppearanceLarge" as part of the TextView's xml. The problem is, while these generally look fine, they must not be the appearance the 'official' preferences use, since the colors end up wrong on some devices. In particular I'm porting my application to the Nook Color, and it uses a light grey background and black text color on the preference screen instead of black background/light grey text. My text color in this situation stays the same, but the rest of my layout is themed appropriately.
I'm really unsure what I'm supposed to do here to make my text match up with the 'official' theme. Should I be using obtainStyledAttributes and running though my layout to set things? The tutorials I've seen on using that so far have been really baffling, and it seems like there must be a textAppearance or style I can set in the XML to fix this.
You've to define your own application theme which inherits from the official theme you want. Actually, you can just define a theme MyTheme extending the Theme.Light for example.
Create an res/values/styles.xml file like that:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="#android:style/Theme.Light">
</style>
</resources>
Then, you just have to apply your theme using the android:theme attribute of the application entity of your AndroidManifest.xml:
<application android:theme="#style/MyTheme">
[...]
</application>

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