Java Code Examples for android.support.v7.app.AppCompatDelegate#setDefaultNightMode()

The following examples show how to use android.support.v7.app.AppCompatDelegate#setDefaultNightMode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Canvas.java    From Google-AAD-CheatSheet with MIT License 6 votes vote down vote up
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.night_mode) {
            // Get the night mode state of the app.
            int nightMode = AppCompatDelegate.getDefaultNightMode();
            //Set the theme mode for the restarted activity
            if (nightMode == AppCompatDelegate.MODE_NIGHT_YES) {
                AppCompatDelegate.setDefaultNightMode
                        (AppCompatDelegate.MODE_NIGHT_NO);
            } else {
                AppCompatDelegate.setDefaultNightMode
                        (AppCompatDelegate.MODE_NIGHT_YES);
            }
// Recreate the activity for the theme change to take effect.
            recreate();

        }
        return super.onOptionsItemSelected(item);
    }
 
Example 2
Source File: BaseActivity.java    From KernelAdiutor with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    // Don't initialize analytics with debug build
    if (!BuildConfig.DEBUG) {
        Fabric.with(this, new Crashlytics());
    }

    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    Utils.DARK_THEME = Themes.isDarkTheme(this);
    Themes.Theme theme = Themes.getTheme(this, Utils.DARK_THEME);
    if (Utils.DARK_THEME) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    } else {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }
    setTheme(theme.getStyle());
    super.onCreate(savedInstanceState);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && setStatusBarColor()) {
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(statusBarColor());
    }
}
 
Example 3
Source File: MainActivity.java    From Android-Developer-Fundamentals-Version-2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.night_mode){
        int nightMode = AppCompatDelegate.getDefaultNightMode();
        if (nightMode == AppCompatDelegate.MODE_NIGHT_YES){
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
        else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        }
        recreate();
    }
    return true;
}
 
Example 4
Source File: MyApplication.java    From AndroidDemo with MIT License 5 votes vote down vote up
protected void initNightMode() {
    boolean isNight = MyMusicUtil.getNightMode(context);
    if (isNight) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    } else {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }
}
 
Example 5
Source File: BaseActivity.java    From SeeWeather with Apache License 2.0 5 votes vote down vote up
public static void setNightTheme(AppCompatActivity activity) {
    AppCompatDelegate.setDefaultNightMode(
        AppCompatDelegate.MODE_NIGHT_YES);
    activity.getDelegate().setLocalNightMode(
        AppCompatDelegate.MODE_NIGHT_YES);
    // 调用 recreate() 使设置生效
    activity.recreate();
}
 
Example 6
Source File: BaseActivity.java    From Awesome-WanAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public void useNightMode(boolean isNight) {
    if (isNight) {
        AppCompatDelegate.setDefaultNightMode(
                AppCompatDelegate.MODE_NIGHT_YES);
    } else {
        AppCompatDelegate.setDefaultNightMode(
                AppCompatDelegate.MODE_NIGHT_NO);
    }
    recreate();
}
 
Example 7
Source File: VMTheme.java    From VMLibrary with Apache License 2.0 5 votes vote down vote up
/**
 * 设置是否使用夜间模式,这里只是切换主题设置,具体的主题样式资源等还需要自己配置,
 * 参考项目的 values-night 配置
 *
 * @param night 是否设置为夜间模式
 */
public static void setNightTheme(boolean night) {
    if (night) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    } else {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }
}
 
Example 8
Source File: ThemeSwitcher.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
/**
 * Returns true if the current UI_MODE_NIGHT is enabled, false otherwise.
 *
 * @param context to retrieve the current configuration
 */
private static boolean isNightModeEnabled(Context context) {
  if (isNightModeFollowSystem()) {
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
  }
  int uiMode = retrieveCurrentUiMode(context);
  return uiMode == Configuration.UI_MODE_NIGHT_YES;
}
 
Example 9
Source File: NightModelManager.java    From NightModel with Apache License 2.0 5 votes vote down vote up
public void applyNightModel(AppCompatActivity activity){
    invokeResources(activity);
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    activity.getDelegate().applyDayNight();
    applyNewModel();
    PersistenceUtils.setNightModel(activity.getApplicationContext(), true);
    ModelChangeManager.getInstance().notifyChange(true);
}
 
Example 10
Source File: App.java    From WanAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    context = getApplicationContext();
    Realm.init(this);
    if (PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(SettingsUtil.KEY_NIGHT_MODE, false)) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }else {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }
}
 
Example 11
Source File: SettingsFragment.java    From QuickNote with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
    String key = preference.getKey();
    if (key.equals(KEY_THEME)) {
        if (value.equals(QuickNote.getString(R.string.theme_value_default))) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        } else if (value.equals(QuickNote.getString(R.string.theme_value_night))) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        }
        getActivity().recreate();
    } else if (key.equals(KEY_LANG)) {
        if (value.equals(QuickNote.getString(R.string.lang_value_chinese))) {
            CommonUtil.changeLocalLanguage(QuickNote.getAppContext(), Locale.SIMPLIFIED_CHINESE);
        } else if (value.equals(QuickNote.getString(R.string.lang_value_english))) {
            CommonUtil.changeLocalLanguage(QuickNote.getAppContext(), Locale.US);
        }
        getActivity().recreate();
    } else if (key.equals(KEY_AUTO_UPDATE)) {
        if (value.equals(false)) { // 提示用户如需更新可到关于界面进行手动更新
            CommonUtil.showSnackBarOnUiThread(getView(), R.string.prompt_close_auto_update);
        }
    } else if (key.equals(KEY_GESTURE_PWD)) {
        if (value.equals(true)) {
            if (TextUtils.isEmpty(QuickNote.getSavedPattern())) { // 如果是初次启用
                Intent intent = new Intent(getActivity(), LockActivity.class);
                startActivity(intent);
            }
        }
    }
    return true;
}
 
Example 12
Source File: MainActivity.java    From Android-Developer-Fundamentals-Version-2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.night_mode){
        int nightMode = AppCompatDelegate.getDefaultNightMode();
        if (nightMode == AppCompatDelegate.MODE_NIGHT_YES){
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
        else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        }
        recreate();
    }
    return true;
}
 
Example 13
Source File: BaseActivity.java    From FriendBook with GNU General Public License v3.0 5 votes vote down vote up
private void initTheme() {
    //设置该app的主题根据时间不同显示
    if (AppConfig.isNightMode()) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    } else {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }

}
 
Example 14
Source File: GankApp.java    From GankGirl with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    _context = getApplicationContext();
    instance = this;
    PreferencesUtils preferencesUtils = new PreferencesUtils(this);
    if(preferencesUtils.getBoolean(R.string.action_day_night, false)){
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }else {
        //设置该app的主题根据时间不同显示
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
    }
}
 
Example 15
Source File: MyApplication.java    From ZZShow with Apache License 2.0 5 votes vote down vote up
private void initDayNightMode() {
    if (SharedPreferencesUtil.isNightMode()) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    } else {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }
}
 
Example 16
Source File: SettingActivity.java    From apkextractor with GNU General Public License v3.0 4 votes vote down vote up
private void refreshNightMode(int value){
    result_code=RESULT_OK;
    AppCompatDelegate.setDefaultNightMode(value);
    recreate();
}
 
Example 17
Source File: Utils.java    From FireFiles with Apache License 2.0 4 votes vote down vote up
public static void changeThemeStyle(AppCompatDelegate delegate) {
    int nightMode = Integer.valueOf(SettingsActivity.getThemeStyle());
    AppCompatDelegate.setDefaultNightMode(nightMode);
    delegate.setLocalNightMode(nightMode);
}
 
Example 18
Source File: MyApplication.java    From v9porn with MIT License 4 votes vote down vote up
private void initNightMode() {
    boolean isNightMode = dataManager.isOpenNightMode();
    AppCompatDelegate.setDefaultNightMode(isNightMode ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
}
 
Example 19
Source File: NightModeHelper.java    From chaoli-forum-for-android-2 with GNU General Public License v3.0 4 votes vote down vote up
private static void setNight(){
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    SharedPreferences.Editor editor = getSp().edit();
    editor.putString(MODE,NIGHT);
    editor.apply();
}
 
Example 20
Source File: StartActivity.java    From meshenger-android with GNU General Public License v3.0 4 votes vote down vote up
private void continueInit() {
    if (this.binder == null) {
        return;
    }

    this.startState += 1;

    switch (this.startState) {
        case 1:
            log("init 1: load database");
            // open without password
            this.binder.loadDatabase();
            continueInit();
            break;
        case 2:
            log("init 2: check database");
            if (this.binder.getDatabase() == null) {
                // database is probably encrypted
                showDatabasePasswordDialog();
            } else {
                continueInit();
            }
            break;
        case 3:
            log("init 3: check username");
            if (this.binder.getSettings().getUsername().isEmpty()) {
                // set username
                showMissingUsernameDialog();
            } else {
                continueInit();
            }
            break;
        case 4:
            log("init 4: check key pair");
            if (this.binder.getSettings().getPublicKey() == null) {
                // generate key pair
                initKeyPair();
            }
            continueInit();
            break;
        case 5:
            log("init 5: check addresses");
            if (this.binder.isFirstStart()) {
                showMissingAddressDialog();
            } else {
                continueInit();
            }
            break;
        case 6:
            log("init 6: battery optimizations");
            if (this.binder.isFirstStart() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                try {
                    PowerManager pMgr = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
                    if (!pMgr.isIgnoringBatteryOptimizations(this.getPackageName())) {
                        Intent intent = new Intent(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                        intent.setData(Uri.parse("package:" + this.getPackageName()));
                        startActivityForResult(intent, IGNORE_BATTERY_OPTIMIZATION_REQUEST);
                        break;
                    }
                } catch(Exception e) {
                    // ignore
                }
            }
            continueInit();
            break;
        case 7:
           log("init 7: start contact list");
            // set night mode
            boolean nightMode = this.binder.getSettings().getNightMode();
            AppCompatDelegate.setDefaultNightMode(
                    nightMode ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO
            );

            // all done - show contact list
            startActivity(new Intent(this, MainActivity.class));
            finish();
            break;
    }
}