Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement 'follow system' theme option #122

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions res/layout/settings_ui_theme_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
android:layout_marginLeft="2dip"
android:layout_marginRight="10dip"
>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/setting_ui_theme_system"
android:text="@string/setting_ui_theme_system"
android:layout_marginBottom="5dip"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<string name="action_add_dictionaries">Добавить</string>
<string name="setting_fav_random_search">Использовать только избранные словари для случайного поиска</string>
<string name="setting_ui_theme">Интерфейс</string>
<string name="setting_ui_theme_system">Следовать системе</string>
<string name="setting_ui_theme_dark">Тёмный</string>
<string name="setting_ui_theme_light">Светлый</string>
<string name="article_collection_invalid_link">Некорректная ссылка</string>
Expand Down
1 change: 1 addition & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<string name="setting_fav_random_search">Use only favorite dictionaries for random lookup</string>
<string name="article_collection_invalid_link">Invalid link</string>
<string name="setting_ui_theme">User Interface Style</string>
<string name="setting_ui_theme_system">Follow system</string>
<string name="setting_ui_theme_light">Light</string>
<string name="setting_ui_theme_dark">Dark</string>
<string name="action_load_remote_content">Load Remote Content</string>
Expand Down
14 changes: 12 additions & 2 deletions src/itkach/aard2/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.database.DataSetObserver;
import android.net.Uri;
import android.os.AsyncTask;
Expand Down Expand Up @@ -71,6 +72,7 @@ public class Application extends android.app.Application {
private static final String PREF = "app";
static final String PREF_RANDOM_FAV_LOOKUP = "onlyFavDictsForRandomLookup";
static final String PREF_UI_THEME = "UITheme";
static final String PREF_UI_THEME_SYSTEM = "system";
static final String PREF_UI_THEME_LIGHT = "light";
static final String PREF_UI_THEME_DARK = "dark";
static final String PREF_USE_VOLUME_FOR_NAV = "useVolumeForNav";
Expand Down Expand Up @@ -227,8 +229,16 @@ SharedPreferences prefs() {
}

String getPreferredTheme() {
return prefs().getString(Application.PREF_UI_THEME,
Application.PREF_UI_THEME_LIGHT);
String theme = prefs().getString(Application.PREF_UI_THEME,
Application.PREF_UI_THEME_SYSTEM);

if (theme.equals(PREF_UI_THEME_SYSTEM)) {
int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;

return (mode == Configuration.UI_MODE_NIGHT_YES) ? "dark" : "light";
}

return theme;
}

void installTheme(Activity activity) {
Expand Down
9 changes: 8 additions & 1 deletion src/itkach/aard2/SettingsListAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private View getUIThemeSettingsView(View convertView, ViewGroup parent) {
final SharedPreferences prefs = app.prefs();

String currentValue = prefs.getString(Application.PREF_UI_THEME,
Application.PREF_UI_THEME_LIGHT);
Application.PREF_UI_THEME_SYSTEM);
Log.d("Settings", Application.PREF_UI_THEME + " current value: " + currentValue);

View.OnClickListener clickListener = new View.OnClickListener() {
Expand All @@ -130,6 +130,9 @@ public void onClick(View view) {
SharedPreferences.Editor editor = prefs.edit();
String value = null;
switch(view.getId()) {
case R.id.setting_ui_theme_system:
value = Application.PREF_UI_THEME_SYSTEM;
break;
case R.id.setting_ui_theme_light:
value = Application.PREF_UI_THEME_LIGHT;
break;
Expand All @@ -145,12 +148,16 @@ public void onClick(View view) {
context.recreate();
}
};
RadioButton btnSystem = (RadioButton) view
.findViewById(R.id.setting_ui_theme_system);
RadioButton btnLight = (RadioButton) view
.findViewById(R.id.setting_ui_theme_light);
RadioButton btnDark = (RadioButton) view
.findViewById(R.id.setting_ui_theme_dark);
btnSystem.setOnClickListener(clickListener);
btnLight.setOnClickListener(clickListener);
btnDark.setOnClickListener(clickListener);
btnSystem.setChecked(currentValue.equals(Application.PREF_UI_THEME_SYSTEM));
btnLight.setChecked(currentValue.equals(Application.PREF_UI_THEME_LIGHT));
btnDark.setChecked(currentValue.equals(Application.PREF_UI_THEME_DARK));
};
Expand Down