add a dark theme

This commit is contained in:
tibbi 2016-07-21 23:51:58 +02:00
parent c785b96ac8
commit 30e284415d
20 changed files with 174 additions and 34 deletions

View file

@ -29,11 +29,19 @@
<activity <activity
android:name=".activities.AboutActivity" android:name=".activities.AboutActivity"
android:label="@string/about"/> android:label="@string/about"
android:parentActivityName=".activities.MainActivity"/>
<activity <activity
android:name=".activities.LicenseActivity" android:name=".activities.LicenseActivity"
android:label="@string/third_party_licences"/> android:label="@string/third_party_licences"
android:parentActivityName=".activities.AboutActivity"/>
<activity
android:name=".activities.SettingsActivity"
android:allowTaskReparenting="true"
android:label="@string/settings"
android:parentActivityName=".activities.MainActivity"/>
<receiver <receiver
android:name=".MyWidgetProvider" android:name=".MyWidgetProvider"
@ -46,6 +54,5 @@
android:name="android.appwidget.provider" android:name="android.appwidget.provider"
android:resource="@xml/widget_info"/> android:resource="@xml/widget_info"/>
</receiver> </receiver>
</application> </application>
</manifest> </manifest>

View file

@ -21,4 +21,12 @@ public class Config {
public void setIsFirstRun(boolean firstRun) { public void setIsFirstRun(boolean firstRun) {
mPrefs.edit().putBoolean(Constants.IS_FIRST_RUN, firstRun).apply(); 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

@ -25,11 +25,10 @@ public class Constants {
public static final String EIGHT = "eight"; public static final String EIGHT = "eight";
public static final String NINE = "nine"; public static final String NINE = "nine";
public static final String PREFS = "prefs";
public static final String WIDGET_BG_COLOR = "widget_bg_color";
public static final String WIDGET_TEXT_COLOR = "widget_text_color";
// shared preferences // shared preferences
public static final String PREFS_KEY = "Calculator"; public static final String PREFS_KEY = "Calculator";
public static final String IS_FIRST_RUN = "is_first_run"; 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

@ -102,7 +102,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
} }
private SharedPreferences initPrefs(Context context) { private SharedPreferences initPrefs(Context context) {
return context.getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE); return context.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
} }
private void updateTextColors(int color) { private void updateTextColors(int color) {

View file

@ -5,7 +5,6 @@ import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html; import android.text.Html;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.view.View; import android.view.View;
@ -21,7 +20,7 @@ import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnClick; import butterknife.OnClick;
public class AboutActivity extends AppCompatActivity { public class AboutActivity extends SimpleActivity {
@BindView(R.id.about_copyright) TextView mCopyright; @BindView(R.id.about_copyright) TextView mCopyright;
@BindView(R.id.about_email) TextView mEmailTV; @BindView(R.id.about_email) TextView mEmailTV;
@BindView(R.id.about_rate_us) View mRateUs; @BindView(R.id.about_rate_us) View mRateUs;

View file

@ -3,14 +3,13 @@ package com.simplemobiletools.calculator.activities;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.simplemobiletools.calculator.R; import com.simplemobiletools.calculator.R;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnClick; import butterknife.OnClick;
public class LicenseActivity extends AppCompatActivity { public class LicenseActivity extends SimpleActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {

View file

@ -3,9 +3,7 @@ package com.simplemobiletools.calculator.activities;
import android.content.ClipData; import android.content.ClipData;
import android.content.ClipboardManager; import android.content.ClipboardManager;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -25,7 +23,7 @@ import butterknife.OnClick;
import butterknife.OnLongClick; import butterknife.OnLongClick;
import me.grantland.widget.AutofitHelper; import me.grantland.widget.AutofitHelper;
public class MainActivity extends AppCompatActivity implements Calculator { public class MainActivity extends SimpleActivity implements Calculator {
@BindView(R.id.result) TextView mResult; @BindView(R.id.result) TextView mResult;
@BindView(R.id.formula) TextView mFormula; @BindView(R.id.formula) TextView mFormula;
@ -38,7 +36,6 @@ public class MainActivity extends AppCompatActivity implements Calculator {
ButterKnife.bind(this); ButterKnife.bind(this);
mCalc = new CalculatorImpl(this); mCalc = new CalculatorImpl(this);
setupResultView();
AutofitHelper.create(mResult); AutofitHelper.create(mResult);
AutofitHelper.create(mFormula); AutofitHelper.create(mFormula);
} }
@ -58,24 +55,17 @@ public class MainActivity extends AppCompatActivity implements Calculator {
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.settings:
startActivity(new Intent(getApplicationContext(), SettingsActivity.class));
return true;
case R.id.about: case R.id.about:
final Intent intent = new Intent(getApplicationContext(), AboutActivity.class); startActivity(new Intent(getApplicationContext(), AboutActivity.class));
startActivity(intent);
return true; return true;
default: default:
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
} }
private void setupResultView() {
final Resources res = getResources();
mResult.setBackgroundColor(res.getColor(android.R.color.white));
mResult.setTextColor(res.getColor(R.color.text_grey));
mFormula.setBackgroundColor(res.getColor(android.R.color.white));
mFormula.setTextColor(res.getColor(R.color.text_grey));
}
@OnClick(R.id.btn_plus) @OnClick(R.id.btn_plus)
public void plusClicked() { public void plusClicked() {
mCalc.handleOperation(Constants.PLUS); mCalc.handleOperation(Constants.PLUS);

View file

@ -0,0 +1,43 @@
package com.simplemobiletools.calculator.activities;
import android.os.Bundle;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.widget.SwitchCompat;
import com.simplemobiletools.calculator.Config;
import com.simplemobiletools.calculator.R;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class SettingsActivity extends SimpleActivity {
@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());
restartActivity();
}
private void restartActivity() {
TaskStackBuilder.create(getApplicationContext()).addNextIntentWithParentStack(getIntent()).startActivities();
}
}

View file

@ -0,0 +1,31 @@
package com.simplemobiletools.calculator.activities;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import com.simplemobiletools.calculator.Config;
import com.simplemobiletools.calculator.R;
public class SimpleActivity extends AppCompatActivity {
protected Config mConfig;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
mConfig = Config.newInstance(getApplicationContext());
setTheme(mConfig.getIsDarkTheme() ? R.style.AppTheme_Dark : R.style.AppTheme);
super.onCreate(savedInstanceState);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

View file

@ -56,7 +56,7 @@ public class WidgetConfigureActivity extends AppCompatActivity {
} }
private void initVariables() { private void initVariables() {
final SharedPreferences prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE); final SharedPreferences prefs = getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
mBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, 1); mBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, 1);
if (mBgColor == 1) { if (mBgColor == 1) {
mBgColor = Color.BLACK; mBgColor = Color.BLACK;
@ -95,7 +95,7 @@ public class WidgetConfigureActivity extends AppCompatActivity {
} }
private void storeWidgetBackground() { private void storeWidgetBackground() {
final SharedPreferences prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE); final SharedPreferences prefs = getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
prefs.edit().putInt(Constants.WIDGET_BG_COLOR, mBgColor).apply(); prefs.edit().putInt(Constants.WIDGET_BG_COLOR, mBgColor).apply();
prefs.edit().putInt(Constants.WIDGET_TEXT_COLOR, mTextColor).apply(); prefs.edit().putInt(Constants.WIDGET_TEXT_COLOR, mTextColor).apply();
} }

View file

@ -5,9 +5,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/buttons_background"
android:orientation="vertical" android:orientation="vertical"
android:theme="@style/MainTheme"
tools:context=".activities.MainActivity"> tools:context=".activities.MainActivity">
<TextView <TextView
@ -20,7 +18,6 @@
android:maxLines="1" android:maxLines="1"
android:paddingLeft="@dimen/activity_margin" android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin" android:paddingRight="@dimen/activity_margin"
android:textColor="@android:color/white"
android:textSize="@dimen/formula_text_size"/> android:textSize="@dimen/formula_text_size"/>
<TextView <TextView
@ -34,7 +31,6 @@
android:paddingLeft="@dimen/activity_margin" android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin" android:paddingRight="@dimen/activity_margin"
android:text="0" android:text="0"
android:textColor="@android:color/white"
android:textSize="@dimen/display_text_size"/> android:textSize="@dimen/display_text_size"/>
<LinearLayout <LinearLayout

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"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/settings"
android:title="@string/settings"
app:showAsAction="never"/>
<item <item
android:id="@+id/about" android:id="@+id/about"
android:title="@string/about" android:title="@string/about"

View file

@ -11,6 +11,10 @@
<string name="follow_us">Seguici:</string> <string name="follow_us">Seguici:</string>
<string name="copyright">v %1$s\nCopyright © Simple Mobile Tools %2$d</string> <string name="copyright">v %1$s\nCopyright © Simple Mobile Tools %2$d</string>
<!-- Settings -->
<string name="settings">Impostazioni</string>
<string name="dark_theme">Tema scuro</string>
<!--License--> <!--License-->
<string name="notice">Questa app usa le seguenti librerie di terze parti per semplificarmi la vita. Grazie.</string> <string name="notice">Questa app usa le seguenti librerie di terze parti per semplificarmi la vita. Grazie.</string>
<string name="third_party_licences">Licenze di terze parti</string> <string name="third_party_licences">Licenze di terze parti</string>

View file

@ -11,6 +11,10 @@
<string name="follow_us">フォローしてください:</string> <string name="follow_us">フォローしてください:</string>
<string name="copyright">v %1$s\nCopyright © Simple Mobile Tools %2$d</string> <string name="copyright">v %1$s\nCopyright © Simple Mobile Tools %2$d</string>
<!-- Settings -->
<string name="settings">設定</string>
<string name="dark_theme">ダークテーマ</string>
<!--License--> <!--License-->
<string name="notice">このアプリは、私の人生を容易にするために、次のサードパーティのライブラリーを使用しています。 ありがとうございます。</string> <string name="notice">このアプリは、私の人生を容易にするために、次のサードパーティのライブラリーを使用しています。 ありがとうございます。</string>
<string name="third_party_licences">サードパーティー ライセンス</string> <string name="third_party_licences">サードパーティー ライセンス</string>

View file

@ -12,6 +12,10 @@
<string name="version">v %1$s</string> <string name="version">v %1$s</string>
<string name="copyright">v %1$s\nCopyright © Simple Mobile Tools %2$d</string> <string name="copyright">v %1$s\nCopyright © Simple Mobile Tools %2$d</string>
<!-- Settings -->
<string name="settings">Inställningar</string>
<string name="dark_theme">Mörkt tema</string>
<!--License--> <!--License-->
<string name="notice">Denna app använder följande tredjepartsbibliotek för att göra mitt liv enklare. Tack.</string> <string name="notice">Denna app använder följande tredjepartsbibliotek för att göra mitt liv enklare. Tack.</string>
<string name="third_party_licences">Tredjepartslicenser</string> <string name="third_party_licences">Tredjepartslicenser</string>

View file

@ -3,7 +3,6 @@
<color name="colorPrimary">#fff68630</color> <color name="colorPrimary">#fff68630</color>
<color name="colorPrimaryDark">#ffe27725</color> <color name="colorPrimaryDark">#ffe27725</color>
<color name="colorAccent">@color/colorPrimary</color> <color name="colorAccent">@color/colorPrimary</color>
<color name="buttons_background">#fff3f3f3</color>
<color name="text_grey">#77000000</color> <color name="text_grey">#77000000</color>
<color name="dark_grey_pressed_mask">#11000000</color> <color name="dark_grey_pressed_mask">#11000000</color>
</resources> </resources>

View file

@ -3,6 +3,7 @@
<dimen name="result_padding">64dp</dimen> <dimen name="result_padding">64dp</dimen>
<dimen name="social_padding">8dp</dimen> <dimen name="social_padding">8dp</dimen>
<dimen name="social_logo">40dp</dimen> <dimen name="social_logo">40dp</dimen>
<dimen name="settings_padding">8dp</dimen>
<dimen name="min_widget_width">250dp</dimen> <dimen name="min_widget_width">250dp</dimen>
<dimen name="min_widget_height">250dp</dimen> <dimen name="min_widget_height">250dp</dimen>

View file

@ -12,6 +12,10 @@
<string name="follow_us">Follow us:</string> <string name="follow_us">Follow us:</string>
<string name="copyright">v %1$s\nCopyright © Simple Mobile Tools %2$d</string> <string name="copyright">v %1$s\nCopyright © Simple Mobile Tools %2$d</string>
<!-- Settings -->
<string name="settings">Settings</string>
<string name="dark_theme">Dark theme</string>
<!--License--> <!--License-->
<string name="notice">This app uses the following third party libraries to make my life easier. Thank you.</string> <string name="notice">This app uses the following third party libraries to make my life easier. Thank you.</string>
<string name="third_party_licences">Third party licences</string> <string name="third_party_licences">Third party licences</string>

View file

@ -8,6 +8,14 @@
<item name="android:textSize">@dimen/normal_text_size</item> <item name="android:textSize">@dimen/normal_text_size</item>
</style> </style>
<style name="AppTheme.Dark" parent="Theme.AppCompat">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="actionBarStyle">@style/AppTheme.ActionBarStyle</item>
<item name="android:textSize">@dimen/normal_text_size</item>
</style>
<style name="MainTheme" parent="@style/AppTheme"> <style name="MainTheme" parent="@style/AppTheme">
<item name="android:textColor">@android:color/white</item> <item name="android:textColor">@android:color/white</item>
</style> </style>