Skip to content

Instantly share code, notes, and snippets.

@albeee
Last active July 28, 2016 01:23
Show Gist options
  • Save albeee/f13e5bf8c565f1e0a49249c51500c551 to your computer and use it in GitHub Desktop.
Save albeee/f13e5bf8c565f1e0a49249c51500c551 to your computer and use it in GitHub Desktop.
Android Night Mode - themes.xml
<!-- ========================== -->
<!-- Base App Theme -->
<!-- ========================== -->
<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
...
</style>
<!-- ========================== -->
<!-- Regular or Light App Theme -->
<!-- ========================== -->
<style name="AppTheme.Light" parent="AppTheme.Base">
<item name="colorPrimary">@color/actionbar_background_color</item>
<item name="colorPrimaryDark">@color/statusbar_bg_color_filter_none</item>
...
</style>
<!-- ========================== -->
<!-- Night or Dark App Theme -->
<!-- ========================== -->
<style name="AppTheme.Dark" parent="AppTheme.Base">
<item name="colorPrimary">@color/dark_action_bar_color</item>
<item name="colorPrimaryDark">@color/dark_status_bar_color</item>
...
</style>
public final class ThemeUtils {
private static int mAppTheme;
public static int getAppTheme(){
mAppTheme = isNightMode() ? R.style.Apptheme_Dark : R.style.Apptheme_Light;
return mAppTheme;
}
public static boolean isNightMode(){
// TODO - Create a time difference logic to detect the daylight and nightmode
return false;
}
}
public class MainActivity extends Activity {
public void onCreate(Bundle saveInstance) {
// set the daylight or night theme here
setTheme(Utils.getAppTheme());
// super activity constructor
super.onCreate(saveInstance);
// set the content display view or layout
setContentView(R.layout.activity_main);
}
}
<style name="MyTheme" parent="Theme.AppCompat.DayNight">
<!-- do something -->
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment