Dark theme android app
SIMPLEST SOLUTION
You can enable/disable dark theme just by:
enable dark theme:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
forcefully disable dark theme:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
set app theme based on mobile settings of dark mode, i.e. if dark mode is enabled then the theme will be set to a dark theme, if not then the default theme, but this will only work in version >= Android version Q
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
Notes:
- Your base theme for app/activity should be
"Theme.AppCompat.DayNight"
like
<style name="DarkTheme" parent="Theme.AppCompat.DayNight">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
- Your res folder's names would end with -night so that different colors and images you can set for day and night themes like
drawable & drawable-night,
values & values-night
Replace these values in your colors.xml
<color name="colorPrimary">#101010</color>
<color name="colorPrimaryDark">#000000</color>
This would be enough to change the Toolbar's color.
If you don't want to change the whole app primary color (which seems it is what you were trying to do in the first place), try creating a new Toolbar by:
Add this to your app's build.gradle
compile 'com.android.support:design:23.1.1'
Add this to your main layout (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="mx.evin.apps.startingtemplate.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/a_main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/black"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Set this in your styles (styles.xml):
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
And set the new toolbar (MainActivity.java).
Toolbar toolbar = (Toolbar) findViewById(R.id.a_main_toolbar);
setSupportActionBar(toolbar);