Android Question How to implement night mode with B4A

wes58

Active Member
Licensed User
How to implement night mode with B4A?(https://developer.android.com/preview/features/darktheme)
Also, how to detect if night mode is on or off?

Can someone please do a tutorial on this?
Did you have a look at this thread https://www.b4x.com/android/forum/threads/changing-the-theme-at-runtime.57277/ ? It is about how to change a theme at runtime. If that's what you are looking for. The link that you posted is talking about new feature introduced in Andoroid Q.
 
Upvote 0

DonManfred

Expert
Licensed User
Supporting Dark theme in your app
In order to support Dark theme, you must set your app's theme (usually found in res/values/styles.xml) to inherit from a DayNight theme:

<style name="AppTheme" parent="Theme.AppCompat.DayNight">

You can also use MaterialComponents' dark theming:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">

Make sure you app is inherited from one of the above Themes

Post your manifest code which is related to the theme
 
Upvote 0

Multiverse app

Active Member
Licensed User
Post your manifest code which is related to the theme
I tried to add:
B4X:
    CreateResource(values-v21, theme.xml,
    <resources>
        <style name="AppTheme" parent="Theme.AppCompat.DayNight">
            <item name="android:colorPrimary">#000000</item>
            <item name="android:colorPrimaryDark">#000000</item>
            <item name="android:colorAccent">#424242</item>
            <item name="android:statusBarColor">#492B82</item>
            <item name="windowNoTitle">true</item>
            <item name="windowActionBar">false</item>
            <item name="android:windowDrawsSystemBarBackgrounds">true</item>
            <item name="android:statusBarColor">@android:color/transparent</item>
        </style>
    </resources>
    )
get the following error:
Logger connected to: ce0317137d70324005
--------- beginning of crash
--------- beginning of main
--------- beginning of system
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/com.app.main}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3108)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3251)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7045)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:555)
at android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:518)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:457)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:145)
at com.app.onCreate(main.java:76)
at android.app.Activity.performCreate(Activity.java:7327)
at android.app.Activity.performCreate(Activity.java:7318)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3088)
... 11 more



Make sure you app is inherited from one of the above Themes
How to do this?
 
Upvote 0

Multiverse app

Active Member
Licensed User
Can this be converted in B4A?

To check what the current theme is, apps can run code like this:
B4X:
int currentNightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK
switch (currentNightMode) {
    case Configuration.UI_MODE_NIGHT_NO:
        // Night mode is not active, we're using the light theme
        break;
    case Configuration.UI_MODE_NIGHT_YES:
        // Night mode is active, we're using dark theme
        break;
}
 
Last edited:
Upvote 0

wes58

Active Member
Licensed User
Can this be converted in B4A?

To check what the current theme is, apps can run code like this:
B4X:
int currentNightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK
switch (currentNightMode) {
    case Configuration.UI_MODE_NIGHT_NO:
        // Night mode is not active, we're using the light theme
        break;
    case Configuration.UI_MODE_NIGHT_YES:
        // Night mode is active, we're using dark theme
        break;

}
There are some java code that you can find. One example is there http://blog.nkdroidsolutions.com/android-daynight-theme-example-using-appcompat-v23-2/
He is using AppCompatDelegate to check what modetype is. You could probably implement it using inline Java
B4X:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.widget.TextView;
 
public class ModeActivity extends AppCompatActivity {
 
    private TextView txtModeType;
    int modeType;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_mode);
        txtModeType = (TextView) findViewById(R.id.txtModeType);
        modeType = AppCompatDelegate.getDefaultNightMode();
 
        if (modeType == AppCompatDelegate.MODE_NIGHT_AUTO) {
            txtModeType.setText("Default Mode: Auto");
        } else if (modeType == AppCompatDelegate.MODE_NIGHT_YES) {
            txtModeType.setText("Default Mode: Night");
        } else if (modeType == AppCompatDelegate.MODE_NIGHT_NO) {
            txtModeType.setText("Default Mode: Day");
        }
    }
And another example how you can set the mode using AppCompatDelegate https://www.journaldev.com/19352/android-daynight-theme-night-mode
B4X:
if (InitApplication.getInstance().isNightModeEnabled()) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
 
Last edited:
Upvote 0
Top