A Java library for implementing dark theme easily in android apps.
- Easy Implentation;
- Saves the theme even when app restarts.
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.erliteHq:themelite:1.1.0'
}
Step 4. In your colors-night xml file, set the corresponding dark theme colors for the colors in the colors xml file.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_###);
//initialize ThemeLite
themeLite = new ThemeLite(this);
//declare
darkSwitch = findViewById(R.id.darkSwitch);
//set theme in splash or main activity
themeLite.setTheme();
//set switch state dependent on theme
if (themeLite.getTheme == themeLite.isDark){
darkSwitch.setChecked(true);
}
//switch listener
darkSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
themeLite.setDark();
} else {
themeLite.setLight();
}
}
});
}
//save theme for app restart in closing activity
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
themeLite.saveTheme(this);
}
For smooth transitions between light mode and dark mode add this to your styles xml file
//Base Application theme
<style name="AppTheme" parent="Theme.AppCompat.Light.xxx">
...
...
<item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
</style>
<style name="WindowAnimationTransition">
<item name="android:windowEnterAnimation">@android:anim/fade_in</item>
<item name="android:windowExitAnimation">@android:anim/fade_out</item>
</style>
Don't forget to add it to your styles-night file too!
Feel free to fork and clone the repo!
You can reach me via