Skip to content

Commit ef2a3b7

Browse files
Mahonstercomann
authored andcommitted
Create Basic Framework For Preset Themes (#213)
* Try again * Create Framework for Themes * Revert "Try again" This reverts commit d66c458. * update gradle on client end * merge upstream #2 * Slightly modify merge * Add Default Theme Selection and Fix default dark text
1 parent 0a106ad commit ef2a3b7

13 files changed

+329
-15
lines changed

app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,4 @@ dependencies {
6464
testCompile 'org.powermock:powermock-module-junit4:1.6.5'
6565
testCompile 'org.powermock:powermock-api-mockito:1.6.5'
6666

67-
}
67+
}

app/src/main/AndroidManifest.xml

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
<activity android:name=".views.PermissionActivity"></activity>
2121
<activity
2222
android:name=".views.settings.SettingsActivity"
23-
android:launchMode="singleTask"
24-
android:parentActivityName=".views.MainActivity"
25-
android:theme="@style/AppTheme.NoActionBar" />
23+
android:parentActivityName=".views.MainActivity" />
2624
<activity
2725
android:name=".views.GoogleAuthActivity"
2826
android:parentActivityName=".views.LoginActivity"
2927
android:theme="@style/AppTheme.NoActionBar"></activity>
3028

29+
<activity android:name=".views.settings.ThemeActivity"
30+
android:parentActivityName=".views.settings.SettingsActivity"/>
31+
3132
<!--
3233
use an alias in case we want to change the launch activity later without breaking
3334
homescreen shortcuts. Note must be defined after the targetActivity

app/src/main/java/com/omkarmoghe/pokemap/views/MainActivity.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.omkarmoghe.pokemap.views;
22

33
import android.content.DialogInterface;
4+
import android.content.Context;
45
import android.content.Intent;
6+
import android.content.SharedPreferences;
57
import android.content.pm.PackageManager;
68
import android.os.Bundle;
79
import android.support.annotation.NonNull;
@@ -39,11 +41,17 @@ public class MainActivity extends BaseActivity {
3941

4042
private boolean skipNotificationServer;
4143
private PokemapAppPreferences pref;
44+
private SharedPreferences sharedPref;
45+
private int themeId;
4246

4347
//region Lifecycle Methods
4448
@Override
4549
protected void onCreate(Bundle savedInstanceState) {
4650
super.onCreate(savedInstanceState);
51+
52+
sharedPref = this.getSharedPreferences(getString(R.string.pref_file_key), Context.MODE_PRIVATE);
53+
themeId = sharedPref.getInt(getString(R.string.pref_theme_no_action_bar), R.style.AppTheme_NoActionBar);
54+
setTheme(themeId);
4755
setContentView(R.layout.activity_main);
4856

4957
pref = new PokemapSharedPreferences(this);
@@ -69,9 +77,14 @@ public void onResume(){
6977
super.onResume();
7078
EventBus.getDefault().register(this);
7179

72-
if(pref.isServiceEnabled()){
80+
if(pref.isServiceEnabled()) {
7381
stopNotificationService();
7482
}
83+
84+
// If the theme has changed, recreate the activity.
85+
if(themeId != sharedPref.getInt(getString(R.string.pref_theme_no_action_bar), R.style.AppTheme_NoActionBar)) {
86+
recreate();
87+
}
7588
}
7689

7790
@Override

app/src/main/java/com/omkarmoghe/pokemap/views/settings/SettingsActivity.java

+25
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
package com.omkarmoghe.pokemap.views.settings;
22

3+
import android.content.Context;
4+
import android.content.SharedPreferences;
35
import android.os.Bundle;
6+
import android.support.v4.app.NavUtils;
47
import android.support.v7.app.AppCompatActivity;
58
import android.support.v7.widget.Toolbar;
69

710
import com.omkarmoghe.pokemap.R;
811

912
public class SettingsActivity extends AppCompatActivity {
1013

14+
private SharedPreferences sharedPref;
15+
private int themeId;
16+
1117
@Override
1218
protected void onCreate(Bundle savedInstanceState) {
1319
super.onCreate(savedInstanceState);
20+
21+
sharedPref = this.getSharedPreferences(getString(R.string.pref_file_key), Context.MODE_PRIVATE);
22+
themeId = sharedPref.getInt(getString(R.string.pref_theme_no_action_bar), R.style.AppTheme_NoActionBar);
23+
setTheme(themeId);
24+
setTitle(getString(R.string.action_settings));
1425
setContentView(R.layout.activity_settings);
1526

1627
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
@@ -23,4 +34,18 @@ protected void onCreate(Bundle savedInstanceState) {
2334
.replace(R.id.settings_content, new SettingsFragment())
2435
.commit();
2536
}
37+
38+
@Override
39+
protected void onResume() {
40+
super.onResume(); // Check if theme has changed, if so then restart the activity.
41+
if(themeId != sharedPref.getInt(getString(R.string.pref_theme_no_action_bar), R.style.AppTheme_NoActionBar)) {
42+
recreate();
43+
}
44+
}
45+
46+
@Override
47+
public void onBackPressed() {
48+
super.onBackPressed();
49+
NavUtils.navigateUpFromSameTask(this);
50+
}
2651
}

app/src/main/java/com/omkarmoghe/pokemap/views/settings/SettingsFragment.java

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.omkarmoghe.pokemap.views.settings;
22

3+
import android.content.Intent;
34
import android.content.SharedPreferences;
45
import android.os.Bundle;
56
import android.preference.Preference;
@@ -38,6 +39,19 @@ else if(s.equals(getString(R.string.pref_password_key)))
3839
passwordPref.setSummary(pref.getString(getString(R.string.pref_password_key).replaceAll(".", "*"), getString(R.string.pref_default_password).replaceAll(".", "*")));
3940
}
4041
};
42+
43+
// Create Theme button to link to Theme Fragment
44+
Preference button = (Preference) getPreferenceManager().findPreference(getString(R.string.pref_theme_button_key));
45+
if (button != null) {
46+
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
47+
@Override
48+
public boolean onPreferenceClick(Preference preference) {
49+
Intent intent = new Intent(getActivity(), ThemeActivity.class);
50+
startActivity(intent);
51+
return true;
52+
}
53+
});
54+
}
4155
}
4256

4357
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.omkarmoghe.pokemap.views.settings;
2+
3+
import android.content.Context;
4+
import android.content.SharedPreferences;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.os.Bundle;
7+
import android.view.View;
8+
import android.widget.RadioButton;
9+
10+
import com.omkarmoghe.pokemap.R;
11+
12+
public class ThemeActivity extends AppCompatActivity {
13+
14+
private SharedPreferences sharedPref;
15+
private int themeId;
16+
17+
private String PREF_ID;
18+
private String PREF_ID_NO_ACTION_BAR;
19+
20+
@Override
21+
protected void onCreate(Bundle savedInstanceState) {
22+
super.onCreate(savedInstanceState);
23+
24+
PREF_ID = getString(R.string.pref_theme);
25+
PREF_ID_NO_ACTION_BAR = getString(R.string.pref_theme_no_action_bar);
26+
27+
sharedPref = this.getSharedPreferences(getString(R.string.pref_file_key), Context.MODE_PRIVATE);
28+
themeId = sharedPref.getInt(getString(R.string.pref_theme), R.style.AppTheme);
29+
setTheme(themeId);
30+
31+
setTitle("Preset Themes");
32+
setContentView(R.layout.activity_theme);
33+
34+
RadioButton r1 = (RadioButton) findViewById(R.id.radioButton1);
35+
RadioButton r2 = (RadioButton) findViewById(R.id.radioButton2);
36+
RadioButton r3 = (RadioButton) findViewById(R.id.radioButton3);
37+
RadioButton r4 = (RadioButton) findViewById(R.id.radioButton4);
38+
RadioButton r5 = (RadioButton) findViewById(R.id.radioButton5);
39+
40+
switch (themeId) {
41+
case R.style.AppThemeSquirtle:
42+
r1.setChecked(true);
43+
break;
44+
case R.style.AppThemeCharmander:
45+
r2.setChecked(true);
46+
break;
47+
case R.style.AppThemeBulbasaur:
48+
r3.setChecked(true);
49+
break;
50+
case R.style.AppThemePikachu:
51+
r4.setChecked(true);
52+
break;
53+
case R.style.AppTheme:
54+
r5.setChecked(true);
55+
break;
56+
default:
57+
break;
58+
}
59+
}
60+
61+
public void onRadioButtonClicked(View v) {
62+
63+
SharedPreferences sharedPref = this.getSharedPreferences(getString(R.string.pref_file_key), Context.MODE_PRIVATE);
64+
SharedPreferences.Editor editor = sharedPref.edit();
65+
66+
boolean checked = ((RadioButton) v).isChecked();
67+
68+
69+
switch(v.getId()) {
70+
case R.id.radioButton1:
71+
if(checked) {
72+
editor.putInt(PREF_ID, R.style.AppThemeSquirtle);
73+
editor.putInt(PREF_ID_NO_ACTION_BAR, R.style.AppThemeSquirtle_NoActionBar);
74+
editor.apply();
75+
}
76+
break;
77+
case R.id.radioButton2:
78+
if(checked) {
79+
editor.putInt(PREF_ID, R.style.AppThemeCharmander);
80+
editor.putInt(PREF_ID_NO_ACTION_BAR, R.style.AppThemeCharmander_NoActionBar);
81+
editor.apply();
82+
}
83+
break;
84+
case R.id.radioButton3:
85+
if(checked) {
86+
editor.putInt(PREF_ID, R.style.AppThemeBulbasaur);
87+
editor.putInt(PREF_ID_NO_ACTION_BAR, R.style.AppThemeBulbasaur_NoActionBar);
88+
editor.apply();
89+
}
90+
break;
91+
case R.id.radioButton4:
92+
if(checked) {
93+
editor.putInt(PREF_ID, R.style.AppThemePikachu);
94+
editor.putInt(PREF_ID_NO_ACTION_BAR, R.style.AppThemePikachu_NoActionBar);
95+
editor.apply();
96+
}
97+
break;
98+
case R.id.radioButton5:
99+
if(checked) {
100+
editor.putInt(PREF_ID, R.style.AppTheme);
101+
editor.putInt(PREF_ID_NO_ACTION_BAR, R.style.AppTheme_NoActionBar);
102+
editor.apply();
103+
}
104+
break;
105+
}
106+
}
107+
}

app/src/main/res/layout/activity_settings.xml

+1-7
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@
1313
android:layout_height="match_parent"
1414
android:orientation="vertical">
1515

16-
<android.support.v7.widget.Toolbar
17-
android:id="@+id/toolbar"
18-
android:layout_width="match_parent"
19-
android:layout_height="?attr/actionBarSize"
20-
android:background="?attr/colorPrimary"
21-
android:theme="@style/AppTheme.AppBarOverlay"
22-
app:popupTheme="@style/AppTheme.PopupOverlay" />
16+
<include layout="@layout/toolbar"/>
2317

2418
<LinearLayout
2519
android:layout_width="match_parent"
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:id="@+id/activity_theme"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:layout_marginStart="@dimen/activity_horizontal_margin"
8+
android:layout_marginEnd="@dimen/activity_horizontal_margin"
9+
android:layout_marginBottom="@dimen/activity_vertical_margin"
10+
android:layout_marginTop="@dimen/activity_vertical_margin"
11+
android:orientation="vertical"
12+
tools:context="com.omkarmoghe.pokemap.views.settings.ThemeActivity">
13+
14+
15+
<TextView
16+
android:text="Will make this selection look pretty in the future. Select a theme:"
17+
android:layout_width="match_parent"
18+
android:layout_height="wrap_content"
19+
android:id="@+id/textView"
20+
android:textColor="#000000"
21+
android:layout_marginBottom="@dimen/activity_vertical_margin"/>
22+
23+
<RadioGroup
24+
android:layout_width="match_parent"
25+
android:layout_height="match_parent">
26+
27+
28+
<RadioButton
29+
android:text="Squirtle"
30+
android:layout_width="wrap_content"
31+
android:layout_height="wrap_content"
32+
android:id="@+id/radioButton1"
33+
android:onClick="onRadioButtonClicked"/>
34+
35+
<RadioButton
36+
android:text="Charmander"
37+
android:layout_width="wrap_content"
38+
android:layout_height="wrap_content"
39+
android:id="@+id/radioButton2"
40+
android:onClick="onRadioButtonClicked"/>
41+
42+
<RadioButton
43+
android:text="Bulbasaur"
44+
android:layout_width="wrap_content"
45+
android:layout_height="wrap_content"
46+
android:id="@+id/radioButton3"
47+
android:onClick="onRadioButtonClicked"/>
48+
49+
<RadioButton
50+
android:text="Pikachu"
51+
android:layout_width="wrap_content"
52+
android:layout_height="wrap_content"
53+
android:id="@+id/radioButton4"
54+
android:onClick="onRadioButtonClicked"/>
55+
56+
<RadioButton
57+
android:text="Default"
58+
android:layout_width="wrap_content"
59+
android:layout_height="wrap_content"
60+
android:id="@+id/radioButton5"
61+
android:onClick="onRadioButtonClicked"/>
62+
</RadioGroup>
63+
64+
65+
</LinearLayout>

app/src/main/res/layout/toolbar.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xmlns:app="http://schemas.android.com/apk/res-auto"
55
android:layout_width="match_parent"
66
android:layout_height="wrap_content"
7-
android:theme="@style/AppTheme.AppBarOverlay">
7+
android:theme="?attr/windowActionBarOverlay">
88

99
<android.support.v7.widget.Toolbar
1010
android:id="@+id/toolbar"

app/src/main/res/values/colors.xml

+20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3+
4+
<color name="material_black">#000000</color>
5+
<color name="material_white">#ffffff</color>
6+
37
<color name="colorPrimary">#3F51B5</color>
48
<color name="colorPrimaryDark">#303F9F</color>
59
<color name="colorAccent">#FF9800</color>
10+
11+
<color name="colorPrimarySquirtle">#03A9F4</color>
12+
<color name="colorPrimaryDarkSquirtle">#0288D1</color>
13+
<color name="colorAccentSquirtle">#795548</color>
14+
15+
<color name="colorPrimaryCharmander">#FF9800</color>
16+
<color name="colorPrimaryDarkCharmander">#F57C00</color>
17+
<color name="colorAccentCharmander">#2196F3</color>
18+
19+
<color name="colorPrimaryBulbasaur">#009688</color>
20+
<color name="colorPrimaryDarkBulbasaur">#00796B</color>
21+
<color name="colorAccentBulbasaur">#4CAF50</color>
22+
23+
<color name="colorPrimaryPikachu">#FFEB3B</color>
24+
<color name="colorPrimaryDarkPikachu">#FBC02D</color>
25+
<color name="colorAccentPikachu">#F44336</color>
626
</resources>

app/src/main/res/values/strings.xml

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<string name="action_clearmap">Clear Map</string>
66
<string name="action_logout">Logout</string>
77

8+
<string name="pref_file_key">com.omkarmoghe.pokemap.PREFERENCE_FILE_KEY</string>
9+
<string name="pref_theme">PresetTheme</string>
10+
<string name="pref_theme_no_action_bar">PresetThemeNoAb</string>
11+
812
<string name="logout_prompt_message">Are you sure you want to log out?</string>
913
<string name="yes">YES</string>
1014
<string name="no">NO</string>
@@ -38,6 +42,10 @@
3842
<string name="pref_service_refresh_summary">How often the service will update in seconds.</string>
3943
<string name="pref_service_refresh_key" translatable="false">service_refresh_rate</string>
4044

45+
<string name="pref_cat_other">Other</string>
46+
<string name="pref_theme_button">Theme</string>
47+
<string name="pref_theme_button_key">ThemeKey</string>
48+
4149
<string name="toast_credentials">Can\'t login. Please check your credentials</string>
4250
<string name="title_activity_poke_map">Pokémon Nearby</string>
4351

0 commit comments

Comments
 (0)