Skip to content

Commit 23aa691

Browse files
committed
Prepare xml files
1 parent 5147769 commit 23aa691

15 files changed

+117
-15
lines changed

app/src/main/java/fr/free/nrw/commons/SettingsActivity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.free.nrw.commons;
22

3+
import android.content.Context;
34
import android.content.SharedPreferences;
45
import android.content.res.Configuration;
56
import android.os.Bundle;
@@ -21,6 +22,13 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
2122
protected void onCreate(Bundle savedInstanceState) {
2223
getDelegate().installViewFactory();
2324
getDelegate().onCreate(savedInstanceState);
25+
//Check prefs on every activity starts
26+
if (getSharedPreferences("prefs", Context.MODE_PRIVATE).getBoolean("theme", false)) {
27+
setTheme(R.style.LightAppTheme);
28+
}else {
29+
setTheme(R.style.DarkAppTheme); //default
30+
}
31+
super.onCreate(savedInstanceState);
2432
super.onCreate(savedInstanceState);
2533
addPreferencesFromResource(R.xml.preferences);
2634
ListPreference licensePreference = (ListPreference) findPreference(Prefs.DEFAULT_LICENSE);

app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListFragment.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.os.Bundle;
1111
import android.support.v4.app.Fragment;
1212
import android.support.v4.content.ContextCompat;
13+
import android.support.v4.content.IntentCompat;
1314
import android.util.Log;
1415
import android.view.LayoutInflater;
1516
import android.view.Menu;
@@ -23,6 +24,8 @@
2324
import android.widget.TextView;
2425
import android.widget.Toast;
2526

27+
import java.lang.reflect.Method;
28+
2629
import fr.free.nrw.commons.AboutActivity;
2730
import fr.free.nrw.commons.CommonsApplication;
2831
import fr.free.nrw.commons.R;
@@ -163,6 +166,27 @@ public boolean onOptionsItemSelected(MenuItem item) {
163166
startActivity(nearbyIntent);
164167
return true;
165168
}
169+
case R.id.menu_theme_toggle:
170+
final SharedPreferences prefs = getActivity().getSharedPreferences("prefs", Context.MODE_PRIVATE);
171+
prefs.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
172+
@Override
173+
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
174+
if(s.equals("theme")){
175+
//http://stackoverflow.com/questions/5659742/onsharedpreferencechanged-called-multiple-times-why
176+
prefs.unregisterOnSharedPreferenceChangeListener(this);
177+
//Finish current activity and tart new one with selected theme
178+
Intent intent = getActivity().getIntent();
179+
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
180+
getActivity().finish();
181+
startActivity(intent);
182+
}
183+
}
184+
});
185+
SharedPreferences.Editor editor = prefs.edit();
186+
//put inverse of selected boolean
187+
editor.putBoolean("theme", !prefs.getBoolean("theme", false));
188+
editor.commit();
189+
return true;
166190
case R.id.menu_refresh:
167191
((SourceRefresher)getActivity()).refreshSource();
168192
return true;
@@ -207,6 +231,31 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
207231
menu.findItem(R.id.menu_refresh).setVisible(false);
208232
}
209233

234+
/*http://stackoverflow.com/questions/30076392/how-does-this-strange-condition-happens-when-show-menu-item-icon-in-toolbar-over/30337653#30337653
235+
Overriden to show toggle_layout button on overlay menu
236+
*/
237+
@Override
238+
public void onPrepareOptionsMenu(Menu menu) {
239+
if(menu != null){
240+
if(menu.getClass().getSimpleName().equals("MenuBuilder")){
241+
try{
242+
Method m = menu.getClass().getDeclaredMethod(
243+
"setOptionalIconsVisible", Boolean.TYPE);
244+
m.setAccessible(true);
245+
m.invoke(menu, true);
246+
}
247+
catch(NoSuchMethodException e){
248+
Log.e(TAG, "onMenuOpened", e);
249+
}
250+
catch(Exception e){
251+
throw new RuntimeException(e);
252+
}
253+
}
254+
}
255+
super.onPrepareOptionsMenu(menu);
256+
}
257+
258+
210259
@Override
211260
public void onCreate(Bundle savedInstanceState) {
212261
super.onCreate(savedInstanceState);

app/src/main/res/drawable/toggle.png

3.5 KB
Loading
3.68 KB
Loading
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
2+
<item
3+
android:drawable="@drawable/toggle"
4+
android:state_checked="true"/>
5+
6+
<item
7+
android:drawable="@drawable/toggle_inverse"
8+
android:state_checked="false"/>
9+
</selector>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
22
android:layout_width="match_parent"
33
android:layout_height="match_parent"
4-
android:background="#000"
4+
android:background="?attr/mainBackground"
55
>
66

77
<ImageView

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5-
android:background="#0c609c"
5+
android:background="?attr/commonsAppBlue"
66
>
77

88
<android.support.v4.view.ViewPager

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
android:id="@+id/mediaDetailCategoryItemText"
1515
android:textSize="14sp"
1616
android:textColor="@android:color/white"
17-
android:background="#20ffffff"
17+
android:background="?attr/subBackground"
1818
/>
1919

2020
<fr.free.nrw.commons.media.MediaDetailSpacer

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
android:orientation="vertical"
44
android:layout_width="match_parent"
55
android:layout_height="match_parent"
6-
android:background="#AA000000"
6+
android:background="?attr/fragmentCategorisationBackground"
77
android:paddingBottom="8dip"
88
android:paddingLeft="16dip"
99
android:paddingStart="16dip"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
android:orientation="vertical"
44
android:layout_width="match_parent"
55
android:layout_height="match_parent"
6-
android:background="#000000"
6+
android:background="?attr/mainBackground"
77
>
88

99
<TextView

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@
5858
android:orientation="vertical"
5959
android:layout_width="match_parent"
6060
android:layout_height="wrap_content"
61-
android:background="#AA000000"
61+
android:background="?attr/fragmentCategorisationBackground"
6262
android:padding="16dp"
6363
>
6464

6565
<LinearLayout
6666
android:orientation="vertical"
6767
android:layout_width="match_parent"
6868
android:layout_height="wrap_content"
69-
android:background="#20ffffff"
69+
android:background="?attr/subBackground"
7070
android:padding="16dp"
7171
>
7272
<TextView
@@ -85,7 +85,7 @@
8585
android:id="@+id/mediaDetailTitle"
8686
android:layout_gravity="left|start"
8787
android:textColor="@android:color/white"
88-
android:background="#20ffffff"
88+
android:background="?attr/subBackground"
8989
android:textSize="14sp"
9090
android:padding="12dp"
9191
/>
@@ -100,7 +100,7 @@
100100
android:orientation="vertical"
101101
android:layout_width="match_parent"
102102
android:layout_height="wrap_content"
103-
android:background="#20ffffff"
103+
android:background="?attr/subBackground"
104104
android:padding="16dp"
105105
>
106106
<TextView
@@ -116,7 +116,7 @@
116116
android:layout_width="match_parent"
117117
android:layout_height="wrap_content"
118118
android:text="@string/media_detail_description_explanation"
119-
android:background="#20ffffff"
119+
android:background="?attr/subBackground"
120120
android:id="@+id/mediaDetailDesc"
121121
android:textColor="@android:color/white"
122122
android:layout_gravity="left|start"
@@ -134,7 +134,7 @@
134134
android:orientation="vertical"
135135
android:layout_width="match_parent"
136136
android:layout_height="wrap_content"
137-
android:background="#20ffffff"
137+
android:background="?attr/subBackground"
138138
android:padding="16dp"
139139
>
140140
<TextView
@@ -152,7 +152,7 @@
152152
android:text="License link"
153153
android:id="@+id/mediaDetailLicense"
154154
android:layout_gravity="left|start"
155-
android:background="#20ffffff"
155+
android:background="?attr/subBackground"
156156
android:textColor="@android:color/white"
157157
android:textSize="14sp"
158158
android:padding="12dp"
@@ -168,7 +168,7 @@
168168
android:orientation="vertical"
169169
android:layout_width="match_parent"
170170
android:layout_height="wrap_content"
171-
android:background="#20ffffff"
171+
android:background="?attr/subBackground"
172172
android:padding="16dp"
173173
android:textStyle="bold"
174174
>

app/src/main/res/menu/fragment_contributions_list.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
android:title="@string/menu_nearby"
2929
app:showAsAction="never"
3030
/>
31+
<item android:id="@+id/menu_theme_toggle"
32+
android:title="Night mode"
33+
android:icon="?attr/toggleButtonIcon"
34+
app:showAsAction="never"
35+
/>
3136
<item android:id="@+id/menu_refresh"
3237
android:title="@string/menu_refresh"
3338
app:showAsAction="never"

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<attr name="mainBackground" format="reference"/>
4+
<attr name="semitransparentText" format="reference"/>
5+
<attr name="commonsAppBlue" format="reference"/>
6+
<attr name="subBackground" format="reference"/>
7+
<attr name="fragmentCategorisationBackground" format="reference"/>
8+
<attr name="buttonBackground" format="reference"/>
9+
<attr name="uploadOverlayBackground" format="reference"/>
10+
<attr name="toggleButtonIcon" format="reference"/>
11+
</resources>

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3-
3+
<!-- Some colours are same for dark/light themes. They are written two times in case
4+
we want to change light ones later.
5+
-->
46
<color name="text_background">#90000000</color>
7+
<color name="item_white_background">#ffffffff</color>
8+
<color name="main_background_dark">#000000</color>
9+
<color name="main_background_light">#bdbdbd</color>
10+
<color name="commons_app_blue_dark">#33FFFFFF</color>
11+
<color name="commons_app_blue_light">#33FFFFFF</color>
12+
<color name="activity_welcome_background_dark">#0c609c</color>
13+
<color name="activity_welcome_background_light">#0c609c</color>
14+
<color name="sub_background_dark">#20ffffff</color>
15+
<color name="sub_background_light">#20ffffff</color>
16+
<color name="fragment_categorisation_background_dark">#AA000000</color>
17+
<color name="fragment_categorisation_background_light">#44ffffff</color>
18+
<color name="button_background_dark">#90000000</color>
19+
<color name="button_background_light">#60000000</color>
20+
<color name="upload_overlay_background_dark">#77000000</color>
21+
<color name="upload_overlay_background_light">#44000000</color>
522

6-
</resources>
23+
</resources>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<string name="menu_from_camera">Take photo</string>
3434
<string name="menu_nearby">Nearby</string>
3535
<string name="provider_contributions">My uploads</string>
36+
<string name="menu_theme_toggle">Night mode</string>
3637
<string name="menu_share">Share</string>
3738
<string name="menu_open_in_browser">View in Browser</string>
3839
<string name="share_title_hint">Title</string>
@@ -90,6 +91,8 @@ Tap this message (or hit back) to skip this step.</string>
9091
<string name="use_previous">Use previous title/description</string>
9192
<string name="allow_gps">Automatically get current location</string>
9293
<string name="allow_gps_summary">Retrieve current location to offer category suggestions if image is not geotagged</string>
94+
<string name="preference_theme">Select theme</string>
95+
<string name="preference_theme_summary">Select application theme as light/dark</string>
9396
<string name="license_name_cc_by_sa_four"> Attribution-ShareAlike 4.0</string>
9497
<string name="license_name_cc_by_four"> Attribution 4.0</string>
9598
<string name="license_name_cc_by_sa"> Attribution-ShareAlike 3.0</string>

0 commit comments

Comments
 (0)