From f2360677061f7ffaa3701355bef63f57184dee2f Mon Sep 17 00:00:00 2001 From: Bart Date: Tue, 5 Jan 2021 10:45:32 +0100 Subject: [PATCH 1/5] Added English string.xml --- app/src/main/res/layout/fragment_settings.xml | 8 +++---- .../main/res/layout/fragment_statistic.xml | 10 ++++----- app/src/main/res/values-en/strings.xml | 17 +++++++++++++++ app/src/main/res/values/strings.xml | 21 ++++++++----------- 4 files changed, 35 insertions(+), 21 deletions(-) create mode 100644 app/src/main/res/values-en/strings.xml diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml index 6d629eb..3ceecd5 100644 --- a/app/src/main/res/layout/fragment_settings.xml +++ b/app/src/main/res/layout/fragment_settings.xml @@ -45,7 +45,7 @@ + + Location + Total time: + Total distance: + Language + Statistics + Start Route + Routes + Colorblind + Settings + Imperial system + Coupons collected: + Visited locations: + 65+ mode + Coupons + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c322028..9b25e32 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,20 +1,17 @@ - Next Location - - Hello blank fragment + Next Location Locaties Routes Statistieken Instellingen - Taal - Imperiaal systeem - 65+ stand - Kleurenblind - Statistieken - Totale afstand: - Bezochte locaties: - Totale tijd: - Coupons gespaard: + Taal + Imperiaal systeem + 65+ stand + Kleurenblind + Totale afstand: + Bezochte locaties: + Totale tijd: + Coupons gespaard: Coupons Start Route \ No newline at end of file From fe5a5b6dfd63aecd27825319abf1749074e8cf3f Mon Sep 17 00:00:00 2001 From: Bart Date: Tue, 5 Jan 2021 13:38:21 +0100 Subject: [PATCH 2/5] Added functionality to the dropdown in settings. Language preference is stored in the sharedPreferences --- .../com/a1/nextlocation/MainActivity.java | 28 ++++++ .../fragments/SettingsFragment.java | 89 ++++++++++++++++--- 2 files changed, 107 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/a1/nextlocation/MainActivity.java b/app/src/main/java/com/a1/nextlocation/MainActivity.java index 9f7821e..dc3b7cf 100644 --- a/app/src/main/java/com/a1/nextlocation/MainActivity.java +++ b/app/src/main/java/com/a1/nextlocation/MainActivity.java @@ -5,6 +5,9 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; +import android.app.Activity; +import android.content.SharedPreferences; +import android.content.res.Configuration; import android.os.Bundle; import android.util.Log; import android.view.MenuItem; @@ -25,6 +28,7 @@ import com.google.android.material.bottomnavigation.BottomNavigationView; import java.io.File; import java.util.Arrays; +import java.util.Locale; public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getName(); @@ -54,9 +58,33 @@ public class MainActivity extends AppCompatActivity { RouteListManager.INSTANCE.setContext(this); RouteListManager.INSTANCE.load(); + // initialize saved language from sharedPreferences + setLocale(loadLocale()); + getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, new HomeFragment()).commit(); } + /** + * loads the saved language from SharedPreferences + * @return the language as string + */ + private String loadLocale(){ + SharedPreferences sharedPreferences = getSharedPreferences("Settings", Activity.MODE_PRIVATE); + return sharedPreferences.getString("Language", ""); + } + + /** + * sets the language of the application to the desired one + * @param language the desired language + */ + private void setLocale(String language){ + Locale locale = new Locale(language); + Locale.setDefault(locale); + Configuration configuration = new Configuration(); + configuration.setLocale(locale); + getBaseContext().getResources().getConfiguration().updateFrom(configuration); + } + private BottomNavigationView.OnNavigationItemSelectedListener navListener = item -> { Fragment selectedFragment = null; diff --git a/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java index 721ba3e..236ea8c 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java @@ -1,5 +1,8 @@ package com.a1.nextlocation.fragments; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.Configuration; import android.os.Bundle; import androidx.annotation.NonNull; @@ -9,36 +12,102 @@ import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import com.a1.nextlocation.MainActivity; import com.a1.nextlocation.R; +import java.util.Locale; + public class SettingsFragment extends Fragment { + private SharedPreferences.Editor editor; + private String language; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + editor = getContext().getSharedPreferences("Settings", Context.MODE_PRIVATE).edit(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - - return inflater.inflate(R.layout.fragment_settings, container, false); + + View view = inflater.inflate(R.layout.fragment_settings, container, false); + + initializeLanguageDropdown(view); + + return view; } - @Override - public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - - // Inflate the layout for this fragment - Spinner dropdown = view.findViewById(R.id.dropdown_menu_Settings); - + private void initializeLanguageDropdown(View view) { + Spinner languageDropdown = view.findViewById(R.id.dropdown_menu_Settings); String[] items = new String[]{"Nederlands", "Engels", "Chinees"}; ArrayAdapter arrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_dropdown_item, items); + languageDropdown.setAdapter(arrayAdapter); - dropdown.setAdapter(arrayAdapter); + // set the language dropdown on the currently selected language stored in the sharedPreferences + languageDropdown.setSelection(languageToDropdownPosition(getContext().getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("Language", ""))); + + languageDropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + setLocale(dropdownPositionToLanguage(position)); + } + + @Override + public void onNothingSelected(AdapterView parent) { + + } + }); + } + + /** + * converts the languageDropdown position to the belonging language + * @param position desired position to convert + * @return the language belonging to the position of the languageDropdown + */ + private String dropdownPositionToLanguage(int position) { + switch (position){ + case 0: + return "nl"; + case 1: + return "en"; + default: + return ""; + } + } + + /** + * converts language to the languageDropdown position + * @param language desired language to convert + * @return the position of the language in the languageDropdown + */ + private int languageToDropdownPosition(String language) { + switch (language) { + case "nl": + return 0; + case "en": + return 1; + default: + return 1; + } + } + + /** + * changes the current language to the desired language and saves this setting in SharedPreferences + * @param language the desired language to translate to + */ + private void setLocale(String language){ + Locale locale = new Locale(language); + Locale.setDefault(locale); + Configuration config = new Configuration(); + config.setLocale(locale); + getContext().getResources().getConfiguration().updateFrom(config); + editor.putString("Language", language); + editor.apply(); } } \ No newline at end of file From f7a2a79d0011af7f391ce8285ac72005e6afbac6 Mon Sep 17 00:00:00 2001 From: Bart Date: Tue, 5 Jan 2021 16:20:43 +0100 Subject: [PATCH 3/5] Language can be changed, doesn't refresh automatically yet --- .../com/a1/nextlocation/MainActivity.java | 4 ++-- .../fragments/SettingsFragment.java | 11 +++++----- app/src/main/res/values-nl/strings.xml | 20 +++++++++++++++++++ 3 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 app/src/main/res/values-nl/strings.xml diff --git a/app/src/main/java/com/a1/nextlocation/MainActivity.java b/app/src/main/java/com/a1/nextlocation/MainActivity.java index dc3b7cf..91268f0 100644 --- a/app/src/main/java/com/a1/nextlocation/MainActivity.java +++ b/app/src/main/java/com/a1/nextlocation/MainActivity.java @@ -70,7 +70,7 @@ public class MainActivity extends AppCompatActivity { */ private String loadLocale(){ SharedPreferences sharedPreferences = getSharedPreferences("Settings", Activity.MODE_PRIVATE); - return sharedPreferences.getString("Language", ""); + return sharedPreferences.getString("Language", "nl"); } /** @@ -82,7 +82,7 @@ public class MainActivity extends AppCompatActivity { Locale.setDefault(locale); Configuration configuration = new Configuration(); configuration.setLocale(locale); - getBaseContext().getResources().getConfiguration().updateFrom(configuration); + getBaseContext().getResources().updateConfiguration(configuration, getBaseContext().getResources().getDisplayMetrics()); } diff --git a/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java index 236ea8c..eaffcb0 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java @@ -55,7 +55,8 @@ public class SettingsFragment extends Fragment { languageDropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { - setLocale(dropdownPositionToLanguage(position)); + setLocale(dropdownPositionToLanguage(id)); + } @Override @@ -67,11 +68,11 @@ public class SettingsFragment extends Fragment { /** * converts the languageDropdown position to the belonging language - * @param position desired position to convert + * @param id desired position to convert * @return the language belonging to the position of the languageDropdown */ - private String dropdownPositionToLanguage(int position) { - switch (position){ + private String dropdownPositionToLanguage(long id) { + switch ((int) id){ case 0: return "nl"; case 1: @@ -106,7 +107,7 @@ public class SettingsFragment extends Fragment { Locale.setDefault(locale); Configuration config = new Configuration(); config.setLocale(locale); - getContext().getResources().getConfiguration().updateFrom(config); + getContext().getResources().updateConfiguration(config, getContext().getResources().getDisplayMetrics()); editor.putString("Language", language); editor.apply(); } diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml new file mode 100644 index 0000000..289db4c --- /dev/null +++ b/app/src/main/res/values-nl/strings.xml @@ -0,0 +1,20 @@ + + + + Next Location + Locaties + Routes + Statistieken + Instellingen + Taal + Imperiaal systeem + 65+ stand + Kleurenblind + Totale afstand: + Bezochte locaties: + Totale tijd: + Coupons gespaard: + Coupons + Start Route + + \ No newline at end of file From 00d306631b873e74338cd6594ed77acb2e35f522 Mon Sep 17 00:00:00 2001 From: Bart Date: Tue, 5 Jan 2021 17:19:09 +0100 Subject: [PATCH 4/5] Finished language functionality --- .../fragments/SettingsFragment.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java index eaffcb0..9b5d16a 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java @@ -8,6 +8,7 @@ import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; @@ -52,16 +53,22 @@ public class SettingsFragment extends Fragment { // set the language dropdown on the currently selected language stored in the sharedPreferences languageDropdown.setSelection(languageToDropdownPosition(getContext().getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("Language", ""))); + long previousID = languageDropdown.getSelectedItemId(); languageDropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { setLocale(dropdownPositionToLanguage(id)); - + if (id != previousID){ + Fragment currentFragment = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_layout); + FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); + fragmentTransaction.detach(currentFragment); + fragmentTransaction.attach(currentFragment); + fragmentTransaction.commit(); + } } @Override public void onNothingSelected(AdapterView parent) { - } }); } @@ -98,6 +105,17 @@ public class SettingsFragment extends Fragment { } } + /** + * reloads the fragment + */ + private void refresh(){ + Fragment currentFragment = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_layout); + FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); + fragmentTransaction.detach(currentFragment); + fragmentTransaction.attach(currentFragment); + fragmentTransaction.commit(); + } + /** * changes the current language to the desired language and saves this setting in SharedPreferences * @param language the desired language to translate to From 6dd2f5de40e246ccb160e15b9caf32626d8bc955 Mon Sep 17 00:00:00 2001 From: Bart Date: Wed, 6 Jan 2021 10:48:23 +0100 Subject: [PATCH 5/5] default strings.xml is now English, added more string resources --- .../fragments/CouponFragment.java | 12 +++------ .../fragments/SettingsFragment.java | 14 +++++----- app/src/main/res/values-en/strings.xml | 17 ------------ app/src/main/res/values-nl/strings.xml | 5 +++- app/src/main/res/values/strings.xml | 26 +++++++++++-------- 5 files changed, 31 insertions(+), 43 deletions(-) delete mode 100644 app/src/main/res/values-en/strings.xml diff --git a/app/src/main/java/com/a1/nextlocation/fragments/CouponFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/CouponFragment.java index 1faea7f..62c9432 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/CouponFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/CouponFragment.java @@ -62,22 +62,18 @@ public class CouponFragment extends Fragment { private void showPopup(Coupon coupon) { AlertDialog.Builder activateBuilder = new AlertDialog.Builder(getContext()); AlertDialog.Builder couponCodeBuilder = new AlertDialog.Builder(getContext()); - // TODO: use string resources instead of hardcoded strings - activateBuilder.setMessage("Weet je zeker dat je deze coupon wilt activeren?"); + activateBuilder.setMessage(getResources().getString(R.string.activate_question)); activateBuilder.setCancelable(true); - // TODO: use string resources instead of hardcoded strings - activateBuilder.setPositiveButton("activeren", (dialog, which) -> { - // TODO: use string resources instead of hardcoded strings + activateBuilder.setPositiveButton(R.string.activate, (dialog, which) -> { dialog.cancel(); couponCodeBuilder.setMessage("Code: " + coupon.getCode()); - couponCodeBuilder.setPositiveButton("Klaar", (dialog1, which1) -> { + couponCodeBuilder.setPositiveButton(R.string.done, (dialog1, which1) -> { dialog.cancel(); }); AlertDialog couponCodePopup = couponCodeBuilder.create(); couponCodePopup.show(); }); - // TODO: use string resources instead of hardcoded strings - activateBuilder.setNegativeButton("annuleren", (dialog, which) -> dialog.cancel()); + activateBuilder.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.cancel()); AlertDialog couponPopup = activateBuilder.create(); couponPopup.show(); diff --git a/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java index 9b5d16a..9c0f42b 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java @@ -25,8 +25,7 @@ import java.util.Locale; public class SettingsFragment extends Fragment { private SharedPreferences.Editor editor; - private String language; - + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -58,7 +57,7 @@ public class SettingsFragment extends Fragment { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { setLocale(dropdownPositionToLanguage(id)); - if (id != previousID){ + if (id != previousID) { Fragment currentFragment = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_layout); FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); fragmentTransaction.detach(currentFragment); @@ -75,11 +74,12 @@ public class SettingsFragment extends Fragment { /** * converts the languageDropdown position to the belonging language + * * @param id desired position to convert * @return the language belonging to the position of the languageDropdown */ private String dropdownPositionToLanguage(long id) { - switch ((int) id){ + switch ((int) id) { case 0: return "nl"; case 1: @@ -91,6 +91,7 @@ public class SettingsFragment extends Fragment { /** * converts language to the languageDropdown position + * * @param language desired language to convert * @return the position of the language in the languageDropdown */ @@ -108,7 +109,7 @@ public class SettingsFragment extends Fragment { /** * reloads the fragment */ - private void refresh(){ + private void refresh() { Fragment currentFragment = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_layout); FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); fragmentTransaction.detach(currentFragment); @@ -118,9 +119,10 @@ public class SettingsFragment extends Fragment { /** * changes the current language to the desired language and saves this setting in SharedPreferences + * * @param language the desired language to translate to */ - private void setLocale(String language){ + private void setLocale(String language) { Locale locale = new Locale(language); Locale.setDefault(locale); Configuration config = new Configuration(); diff --git a/app/src/main/res/values-en/strings.xml b/app/src/main/res/values-en/strings.xml deleted file mode 100644 index 94b8326..0000000 --- a/app/src/main/res/values-en/strings.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - Location - Total time: - Total distance: - Language - Statistics - Start Route - Routes - Colorblind - Settings - Imperial system - Coupons collected: - Visited locations: - 65+ mode - Coupons - \ No newline at end of file diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index 289db4c..893ffe7 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -16,5 +16,8 @@ Coupons gespaard: Coupons Start Route - + Weet je zeker dat je deze coupon wilt activeren? + activeren + Klaar + annuleren \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9b25e32..1cfcd8d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,17 +1,21 @@ Next Location - Locaties + Location Routes - Statistieken - Instellingen - Taal - Imperiaal systeem - 65+ stand - Kleurenblind - Totale afstand: - Bezochte locaties: - Totale tijd: - Coupons gespaard: + Statistics + Settings + Language + Imperial system + 65+ mode + Colorblind + Total distance: + Visited locations: + Total time: + Coupons collected: Coupons Start Route + Are you sure you want to activate this coupon? + activate + Done + cancel \ No newline at end of file