-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract night mode code so it works on CheeseDetailActivity too
- Loading branch information
1 parent
081893e
commit 22269b3
Showing
3 changed files
with
98 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
app/src/main/java/com/support/android/designlibdemo/NightModeUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.support.android.designlibdemo; | ||
|
||
import android.app.Activity; | ||
import android.os.Build; | ||
import android.support.annotation.NonNull; | ||
import android.support.v7.app.AppCompatDelegate; | ||
import android.view.Menu; | ||
import android.view.MenuItem; | ||
|
||
class NightModeUtils { | ||
private Activity activity; | ||
|
||
private NightModeUtils(@NonNull Activity activity) { | ||
this.activity = activity; | ||
} | ||
|
||
static NightModeUtils from(@NonNull Activity activity) { | ||
return new NightModeUtils(activity); | ||
} | ||
|
||
boolean checkNightMode(@NonNull Menu menu) { | ||
switch (AppCompatDelegate.getDefaultNightMode()) { | ||
case AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM: | ||
menu.findItem(R.id.menu_night_mode_system).setChecked(true); | ||
break; | ||
case AppCompatDelegate.MODE_NIGHT_AUTO: | ||
menu.findItem(R.id.menu_night_mode_auto).setChecked(true); | ||
break; | ||
case AppCompatDelegate.MODE_NIGHT_YES: | ||
menu.findItem(R.id.menu_night_mode_night).setChecked(true); | ||
break; | ||
case AppCompatDelegate.MODE_NIGHT_NO: | ||
menu.findItem(R.id.menu_night_mode_day).setChecked(true); | ||
break; | ||
default: | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public boolean onNightModeItemSelected(@NonNull MenuItem item) { | ||
switch (item.getItemId()) { | ||
case R.id.menu_night_mode_system: | ||
setNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); | ||
break; | ||
case R.id.menu_night_mode_day: | ||
setNightMode(AppCompatDelegate.MODE_NIGHT_NO); | ||
break; | ||
case R.id.menu_night_mode_night: | ||
setNightMode(AppCompatDelegate.MODE_NIGHT_YES); | ||
break; | ||
case R.id.menu_night_mode_auto: | ||
setNightMode(AppCompatDelegate.MODE_NIGHT_AUTO); | ||
break; | ||
default: | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private void setNightMode(@AppCompatDelegate.NightMode int nightMode) { | ||
AppCompatDelegate.setDefaultNightMode(nightMode); | ||
|
||
if (Build.VERSION.SDK_INT >= 11) { | ||
activity.recreate(); | ||
} | ||
} | ||
|
||
} |