add a Dark Theme toggle

This commit is contained in:
tibbi 2016-07-18 20:11:58 +02:00
parent a15911738c
commit 45527473e3
8 changed files with 109 additions and 7 deletions

View File

@ -45,6 +45,10 @@
android:name=".activities.LicenseActivity"
android:label="@string/third_party_licences"/>
<activity
android:name=".activities.SettingsActivity"
android:label="@string/settings"/>
<receiver
android:name=".MyWidgetProvider"
android:icon="@mipmap/widget_preview">

View File

@ -21,4 +21,12 @@ public class Config {
public void setIsFirstRun(boolean firstRun) {
mPrefs.edit().putBoolean(Constants.IS_FIRST_RUN, firstRun).apply();
}
public boolean getIsDarkTheme() {
return mPrefs.getBoolean(Constants.IS_DARK_THEME, false);
}
public void setIsDarkTheme(boolean isDarkTheme) {
mPrefs.edit().putBoolean(Constants.IS_DARK_THEME, isDarkTheme).apply();
}
}

View File

@ -4,11 +4,13 @@ public class Constants {
public static final float LOW_ALPHA = .2f;
public static final float HIGH_ALPHA = .8f;
public static final String PREFS_KEY = "Calendar";
public static final String IS_FIRST_RUN = "is_first_run";
public static final String WIDGET_BG_COLOR = "widget_bg_color";
public static final String WIDGET_TEXT_COLOR = "widget_text_color";
public static final String DAY_CODE = "day_code";
public static final String EVENT = "event";
// Shared Preferences
public static final String PREFS_KEY = "Calendar";
public static final String IS_FIRST_RUN = "is_first_run";
public static final String IS_DARK_THEME = "is_dark_theme";
public static final String WIDGET_BG_COLOR = "widget_bg_color";
public static final String WIDGET_TEXT_COLOR = "widget_text_color";
}

View File

@ -103,9 +103,11 @@ public class MainActivity extends AppCompatActivity implements Calendar {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
startActivity(new Intent(getApplicationContext(), SettingsActivity.class));
return true;
case R.id.about:
final Intent intent = new Intent(getApplicationContext(), AboutActivity.class);
startActivity(intent);
startActivity(new Intent(getApplicationContext(), AboutActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);

View File

@ -0,0 +1,38 @@
package com.simplemobiletools.calendar.activities;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SwitchCompat;
import com.simplemobiletools.calendar.Config;
import com.simplemobiletools.calendar.R;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class SettingsActivity extends AppCompatActivity {
@BindView(R.id.settings_dark_theme) SwitchCompat mDarkThemeSwitch;
private static Config mConfig;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
mConfig = Config.newInstance(getApplicationContext());
ButterKnife.bind(this);
setupDarkTheme();
}
private void setupDarkTheme() {
mDarkThemeSwitch.setChecked(mConfig.getIsDarkTheme());
}
@OnClick(R.id.settings_dark_theme_holder)
public void handleDarkTheme() {
mDarkThemeSwitch.setChecked(!mDarkThemeSwitch.isChecked());
mConfig.setIsDarkTheme(mDarkThemeSwitch.isChecked());
}
}

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="@+id/settings_scrollview"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/settings_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/settings_dark_theme_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/settings_padding"
android:background="?android:attr/selectableItemBackground"
android:padding="@dimen/activity_margin">
<TextView
android:id="@+id/settings_dark_theme_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="@dimen/settings_padding"
android:text="@string/dark_theme"/>
<android.support.v7.widget.SwitchCompat
android:id="@+id/settings_dark_theme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@null"
android:clickable="false"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>

View File

@ -1,6 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/settings"
android:title="@string/settings"
app:showAsAction="never"/>
<item
android:id="@+id/about"
android:title="@string/about"

View File

@ -52,4 +52,8 @@
<item quantity="one">1 event deleted</item>
<item quantity="other">%1$d events deleted</item>
</plurals>
<!-- Settings -->
<string name="settings">Settings</string>
<string name="dark_theme">Dark theme</string>
</resources>