Java Code Examples for androidx.appcompat.app.AppCompatDelegate#setDefaultNightMode()

The following examples show how to use androidx.appcompat.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: MainActivity.java    From AsteroidOSSync with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    getDelegate().onConfigurationChanged(newConfig);
    int currentNightMode = newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK;
    switch (currentNightMode) {
        case Configuration.UI_MODE_NIGHT_NO:
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            break;
        case Configuration.UI_MODE_NIGHT_YES:
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            break;
    }

    finish();
    overridePendingTransition(0, 0);
    startActivity(getIntent());
}
 
Example 2
Source File: LaunchActivity.java    From WanAndroid with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    AppCompatDelegate.setDefaultNightMode(
            App.getContext().getAppComponent()
                    .getDataModel()
                    .getNightModeState()
                    ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_launch);
    SVGBgView svgBgView = findViewById(R.id.iv_launch);
    ObjectAnimator animator = ObjectAnimator
            .ofFloat(svgBgView, "alpha", 0, 1f)
            .setDuration(2800);
    animator.setInterpolator(new AccelerateInterpolator());
    animator.start();
    new Handler().postDelayed(() -> {
        startActivity(new Intent(LaunchActivity.this, MainActivity.class));
        overridePendingTransition(R.anim.anim_launch_enter, 0);
        finish();
    }, 3000);
}
 
Example 3
Source File: BaseActivity.java    From MTweaks-KernelAdiutorMOD with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {

    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 4
Source File: ThemeHelper.java    From android-DarkTheme with Apache License 2.0 6 votes vote down vote up
public static void applyTheme(@NonNull String themePref) {
    switch (themePref) {
        case LIGHT_MODE: {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            break;
        }
        case DARK_MODE: {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            break;
        }
        default: {
            if (BuildCompat.isAtLeastQ()) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
            } else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
            }
            break;
        }
    }
}
 
Example 5
Source File: ThemeHelper.java    From user-interface-samples with Apache License 2.0 6 votes vote down vote up
public static void applyTheme(@NonNull String themePref) {
    switch (themePref) {
        case LIGHT_MODE: {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            break;
        }
        case DARK_MODE: {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            break;
        }
        default: {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
            } else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
            }
            break;
        }
    }
}
 
Example 6
Source File: Application.java    From materialistic with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    AppCompatDelegate.setDefaultNightMode(Preferences.Theme.getAutoDayNightMode(this));
    AlgoliaClient.sSortByTime = Preferences.isSortByRecent(this);
    mRefWatcher = LeakCanary.install(this);
    if (BuildConfig.DEBUG) {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectAll()
                .penaltyFlashScreen()
                .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectAll()
                .penaltyLog()
                .build());
    }
    Preferences.migrate(this);
    TYPE_FACE = FontCache.getInstance().get(this, Preferences.Theme.getTypeface(this));
    AppUtils.registerAccountsUpdatedListener(this);
    AdBlocker.init(this, Schedulers.io());
}
 
Example 7
Source File: MyApplication.java    From weather with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate() {
  AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
  super.onCreate();
  ViewPump.init(ViewPump.builder()
      .addInterceptor(new CalligraphyInterceptor(
          new CalligraphyConfig.Builder()
              .setDefaultFontPath("fonts/Vazir.ttf")
              .setFontAttrId(R.attr.fontPath)
              .build()))
      .build());
  createBoxStore();
  if (SharedPreferencesUtil.getInstance(this).isDarkThemeEnabled())
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
  else
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
 
Example 8
Source File: Utility.java    From indigenous-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Set the night theme.
 *
 * @param context
 *   The current context.
 */
public static void setNightTheme(Context context) {
    if (Preferences.getPreference(context, "night_mode", false)) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }
    else {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    }
}
 
Example 9
Source File: History.java    From EBookDownloader with MIT License 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    setContentView(R.layout.activity_history);

    try {
        historyDB = DBFactory.open(getApplicationContext(), "history");
    } catch (SnappydbException e) {
        e.printStackTrace();
    }

    ListView listHistory = findViewById(R.id.list_history);
    history = new ArrayList<>();

    fillHistoryList();

    arrayAdapter = new ArrayAdapter<>(this, R.layout.listview_list_item,
            history);
    listHistory.setAdapter(arrayAdapter);
    listHistory.setOnItemLongClickListener(this);
    listHistory.setOnItemClickListener(this);

    if (history.isEmpty()) {
        showNoHistory();
    }
}
 
Example 10
Source File: ThemeManager.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
public void applyThemeToActivity(Activity activity) {
    if (currentCustomThemePatcher == null && currentCustomTheme != null) {
        ThemeResourceFileBuilder.CustomTheme theme = ThemeResourceFileBuilder
                .createTheme(context, currentCustomTheme);
        currentTheme = theme;
        File themeFile = ThemeResourceFileBuilder.createThemeZipFile(context,
                theme.getResTable());
        currentCustomThemePatcher = new Theme(context, themeFile.getAbsolutePath());
    }
    ThemeResInfo currentBaseTheme = currentTheme;
    if (currentCustomTheme != null)
        currentBaseTheme = currentCustomTheme.baseThemeInfo;
    boolean isThemeDark = currentBaseTheme instanceof BaseTheme &&
            ((BaseTheme) currentBaseTheme).isDark;
    if (currentCustomThemePatcher == null && isThemeDark) {
        currentCustomThemePatcher = new Theme(activity.getAssets());
    }
    if (mNeedsApplyIrcColors) {
        Configuration c = new Configuration();
        c.setToDefaults();
        c.uiMode = Configuration.UI_MODE_TYPE_NORMAL;
        if (currentBaseTheme instanceof BaseTheme && ((BaseTheme) currentBaseTheme).isDark)
            c.uiMode |= Configuration.UI_MODE_NIGHT_YES;
        Resources r = new Resources(currentCustomThemePatcher != null ?
                currentCustomThemePatcher.getAssetManager() : context.getAssets(),
                new DisplayMetrics(), c);
        Resources.Theme t = r.newTheme();
        ThemeResInfo resInfo = currentTheme != null ? currentTheme : fallbackTheme;
        t.applyStyle(resInfo.getThemeResId(), true);
        IRCColorUtils.loadColors(t, resInfo.getIRCColorsResId());
        mNeedsApplyIrcColors = false;
    }
    if (currentCustomThemePatcher != null) {
        currentCustomThemePatcher.applyToActivity(activity);
    }
    if (isThemeDark)
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    else
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
 
Example 11
Source File: MApplication.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
public void initNightTheme() {
    if (isNightTheme()) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    } else {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }
}
 
Example 12
Source File: ThemedActivity.java    From materialistic with Apache License 2.0 5 votes vote down vote up
private void onThemeChanged(int key) {
    if (key == R.string.pref_daynight_auto) {
        AppCompatDelegate.setDefaultNightMode(Preferences.Theme.getAutoDayNightMode(this));
    }
    if (mResumed) {
        AppUtils.restart(this, true);
    } else {
        mPendingThemeChanged = true;
    }
}
 
Example 13
Source File: MainActivity.java    From InviZible with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private void setDayNightTheme() {
    SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    try {
        String theme = defaultSharedPreferences.getString("pref_fast_theme", "4");

        if (appVersion.startsWith("g") && !accelerated) {
            theme = defaultSharedPreferences.getString("pref_fast_theme", "1");
        }

        switch (Objects.requireNonNull(theme)) {
            case "1":
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                break;
            case "2":
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                break;
            case "3":
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_TIME);
                break;
            case "4":
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
                break;
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, "MainActivity setDayNightTheme exception " + e.getMessage() + " " + e.getCause());
    }
}
 
Example 14
Source File: MainActivity.java    From MaterialPreference with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_night_mode:
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES ?
                    AppCompatDelegate.MODE_NIGHT_NO : AppCompatDelegate.MODE_NIGHT_YES);

            recreate();

            return true;
        case R.id.rtl:
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                Resources resources = getBaseContext().getResources();

                Locale locale = resources.getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL
                        ? Locale.ENGLISH : new Locale("ar");
                Locale.setDefault(locale);

                resources.getConfiguration().setLocale(locale);
                resources.updateConfiguration(resources.getConfiguration(), resources.getDisplayMetrics());

                startActivity(new Intent(this, this.getClass()));
                finish();
            }

            return true;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 15
Source File: MainActivity.java    From Hauk with Apache License 2.0 5 votes vote down vote up
/**
 * Loads preferences from storage and applies them to the UI.
 */
private void loadPreferences() {
    Log.i("Loading preferences..."); //NON-NLS
    PreferenceManager prefs = new PreferenceManager(this);
    ((TextView) findViewById(R.id.txtDuration)).setText(String.valueOf(prefs.get(Constants.PREF_DURATION)));
    ((TextView) findViewById(R.id.txtNickname)).setText(prefs.get(Constants.PREF_NICKNAME));
    // Because I can choose between an unchecked cast warning and an overly strong cast warning,
    // I'm going to with the latter.
    //noinspection OverlyStrongTypeCast
    ((Spinner) findViewById(R.id.selUnit)).setSelection(prefs.get(Constants.PREF_DURATION_UNIT));
    ((Checkable) findViewById(R.id.chkAllowAdopt)).setChecked(prefs.get(Constants.PREF_ALLOW_ADOPTION));

    // Set night mode preference.
    AppCompatDelegate.setDefaultNightMode(prefs.get(Constants.PREF_NIGHT_MODE).resolve());
}
 
Example 16
Source File: ThemeHelper.java    From android with MIT License 4 votes vote down vote up
public static void setTheme(Context context, String newTheme) {
    AppCompatDelegate.setDefaultNightMode(ofKey(context, newTheme));
}
 
Example 17
Source File: GeneralPreferencesActivity.java    From mage-android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
	AppCompatDelegate.setDefaultNightMode(Integer.parseInt(newValue.toString()));
	getActivity().recreate();
	return true;
}
 
Example 18
Source File: ThemePreferencesManager.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
public void saveAndApplyTheme(@IdRes int id) {
  int nightMode = convertToNightMode(id);
  saveNightMode(nightMode);
  AppCompatDelegate.setDefaultNightMode(nightMode);
}
 
Example 19
Source File: NotesApplication.java    From nextcloud-notes with GNU General Public License v3.0 4 votes vote down vote up
public static void setAppTheme(DarkModeSetting setting) {
    AppCompatDelegate.setDefaultNightMode(setting.getModeId());
}
 
Example 20
Source File: SettingsActivity.java    From HgLauncher with GNU General Public License v3.0 4 votes vote down vote up
@Override public void onCreate(Bundle savedInstanceState) {
    if (!PreferenceHelper.hasEditor()) {
        PreferenceHelper.initPreference(this);
    }
    PreferenceHelper.fetchPreference();

    if (PreferenceHelper.getProviderList().isEmpty()) {
        Utils.setDefaultProviders(getResources());
    }

    // Check the caller of this activity.
    // If it's coming from the launcher itself, it will always have a calling activity.
    checkCaller();

    // Load the appropriate theme.
    switch (PreferenceHelper.appTheme()) {
        default:
        case "auto":
            if (Utils.atLeastQ()) {
                AppCompatDelegate.setDefaultNightMode(
                        AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
            } else {
                AppCompatDelegate.setDefaultNightMode(
                        AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
            }
            break;
        case "light":
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            break;
        case "dark":
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            setTheme(R.style.AppTheme_Dark);
            break;
        case "black":
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            break;
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    if (getRequestedOrientation() != PreferenceHelper.getOrientation()) {
        setRequestedOrientation(PreferenceHelper.getOrientation());
    }

    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    if (savedInstanceState == null) {
        ViewUtils.setFragment(getSupportFragmentManager(), new BasePreference(), "settings");
    } else {
        fragmentTitle = savedInstanceState.getCharSequence("title");
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(fragmentTitle);
        }
    }
}