Skip to content
This repository was archived by the owner on Jul 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #33 from shirosweets/m3_switch_language
Browse files Browse the repository at this point in the history
M3 switch language

#16
#17
#26
  • Loading branch information
shirosweets authored Oct 22, 2021
2 parents 10d4eea + 058a8fc commit a989797
Show file tree
Hide file tree
Showing 24 changed files with 452 additions and 86 deletions.
4 changes: 2 additions & 2 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
package="com.example.myapplication" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:name=".MainApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher_v2"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_v2_round"
android:supportsRtl="true"
android:theme="@style/Theme.Dark">
android:theme="@style/Theme.Dark" >
<activity
android:name=".MenuActivity"
android:exported="true">
android:exported="true" >
</activity>

<activity
android:name=".MainActivity"
android:exported="true">
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/com/example/myapplication/ConfigManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,36 @@ import android.app.Activity
import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
import android.content.res.Configuration
import android.content.res.Resources
import java.util.*


class ConfigManager {
companion object {
private const val IS_DARK_THEME_DEFAULT: Boolean = true
private const val DEFAULT_LANGUAGE: String = "es"

fun setLocale(activity: Activity, languageCode: String?) {
val locale = Locale(languageCode)
Locale.setDefault(locale)
val resources: Resources = activity.resources
val config: Configuration = resources.configuration
config.setLocale(locale)
resources.updateConfiguration(config, resources.displayMetrics)
}

fun setLanguage(context: Context, iso_lan: String){
val userPrefs = PreferenceManager.getDefaultSharedPreferences(context)
userPrefs.edit().putString(
context.getString(R.string.pref_key_ISO_Language), iso_lan).apply()
}

fun getLanguage(context: Context): String{
val userPrefs = PreferenceManager.getDefaultSharedPreferences(context)
val isoLanguage = context.getString(R.string.pref_key_ISO_Language)
return userPrefs.getString(isoLanguage, DEFAULT_LANGUAGE) ?: DEFAULT_LANGUAGE
}

private fun setTheme(context: Context, isDark: Boolean) {
val userPreferences = PreferenceManager.getDefaultSharedPreferences(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ConfigManager.setLocale(this, ConfigManager.getLanguage(this))
this.setTheme(ConfigManager.getThemeResourceId(this))
if (LoginManager.isLoggedIn(this)) {
val intent = Intent(this, MenuActivity::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class MenuActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ConfigManager.setLocale(this, ConfigManager.getLanguage(this))
this.setTheme(ConfigManager.getThemeResourceId(this))
setContentView(R.layout.activity_menu)
menuNavigationBottom = findViewById(R.id.bottomNavigationView)
Expand Down
17 changes: 10 additions & 7 deletions app/src/main/java/com/example/myapplication/ProfileFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
Expand All @@ -16,13 +17,13 @@ import com.google.android.material.switchmaterial.SwitchMaterial
import com.squareup.picasso.Picasso

class ProfileFragment : Fragment() {
private lateinit var themeSwitch: SwitchMaterial
private lateinit var recycler: RecyclerView
private lateinit var userFirstName: TextView
private lateinit var userImage: ShapeableImageView
private lateinit var userEmail: TextView
private lateinit var sharedPreferences: SharedPreferences
private lateinit var closeSession: Button
private lateinit var profSettings: ImageView

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
Expand All @@ -37,13 +38,8 @@ class ProfileFragment : Fragment() {
userFirstName = view.findViewById(R.id.user_first_name)
userImage = view.findViewById(R.id.user_shapeable_image)
userEmail = view.findViewById(R.id.user_email)
themeSwitch = view.findViewById(R.id.theme_switch)
themeSwitch.isChecked = ConfigManager.isDarkTheme(requireContext())
themeSwitch.setOnCheckedChangeListener { _, _ ->
activity?.applicationContext?.let { ConfigManager.switchTheme(it) }
activity?.recreate()
}

profSettings = view.findViewById(R.id.prof_settings)
closeSession = view.findViewById(R.id.buttonCloseSession)
closeSession.setOnClickListener {
LoginManager.logOut(requireActivity())
Expand All @@ -57,6 +53,13 @@ class ProfileFragment : Fragment() {
recycler.layoutManager = LinearLayoutManager(activity)

sharedPreferences = ConfigManager.prefs(requireActivity())

profSettings.setOnClickListener {
findNavController().navigate(
R.id.action_profileFragment_to_settingsFragment,
null
)
}
setUserData()
}

Expand Down
89 changes: 89 additions & 0 deletions app/src/main/java/com/example/myapplication/SettingsFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.example.myapplication

import android.os.Bundle
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.Button
import android.widget.Spinner
import androidx.navigation.fragment.findNavController
import com.google.android.material.switchmaterial.SwitchMaterial

class SettingsFragment : Fragment() {
private lateinit var themeSwitch: SwitchMaterial
private lateinit var returnToProfile: Button
private lateinit var spinnerChangeLanguage: Spinner

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_settings, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
themeSwitch = view.findViewById(R.id.theme_switch)
themeSwitch.isChecked = ConfigManager.isDarkTheme(requireContext())
spinnerChangeLanguage = view.findViewById(R.id.change_language)

var languageList = ArrayList<String>()
languageList.add("Español")
languageList.add("English")


// var adapter = ArrayAdapter(
// requireContext(),
// R.layout.spinner_tet_view,
// languageList
// )
val adapter = ArrayAdapter.createFromResource(
requireContext(),
R.array.availableLanguagesArray,
R.layout.spinner_text_view
)
adapter.setDropDownViewResource(R.layout.spinner_checked_text_view)
spinnerChangeLanguage.adapter = adapter

returnToProfile = view.findViewById(R.id.return_to_profile_bt)

val current_lan = ConfigManager.getLanguage(requireContext())
when(current_lan){
"en" -> spinnerChangeLanguage.setSelection(1)
else -> spinnerChangeLanguage.setSelection(0)
}

spinnerChangeLanguage.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {
}

override fun onItemSelected(
parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
var cur_select_lan = ConfigManager.getLanguage(requireContext())
val iso_lan = when(languageList[position]){
"English" -> "en"
else -> "es"
}
if(iso_lan != cur_select_lan){
ConfigManager.setLanguage(requireContext(), iso_lan)
activity?.recreate()
}
}
}

themeSwitch.setOnCheckedChangeListener { _, _ ->
activity?.applicationContext?.let { ConfigManager.switchTheme(it) }
activity?.recreate()
}

returnToProfile.setOnClickListener {
findNavController().navigate(
R.id.action_settingsFragment_to_profileFragment,
null
)
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_prof_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FF2A05"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M19.14,12.94c0.04,-0.3 0.06,-0.61 0.06,-0.94c0,-0.32 -0.02,-0.64 -0.07,-0.94l2.03,-1.58c0.18,-0.14 0.23,-0.41 0.12,-0.61l-1.92,-3.32c-0.12,-0.22 -0.37,-0.29 -0.59,-0.22l-2.39,0.96c-0.5,-0.38 -1.03,-0.7 -1.62,-0.94L14.4,2.81c-0.04,-0.24 -0.24,-0.41 -0.48,-0.41h-3.84c-0.24,0 -0.43,0.17 -0.47,0.41L9.25,5.35C8.66,5.59 8.12,5.92 7.63,6.29L5.24,5.33c-0.22,-0.08 -0.47,0 -0.59,0.22L2.74,8.87C2.62,9.08 2.66,9.34 2.86,9.48l2.03,1.58C4.84,11.36 4.8,11.69 4.8,12s0.02,0.64 0.07,0.94l-2.03,1.58c-0.18,0.14 -0.23,0.41 -0.12,0.61l1.92,3.32c0.12,0.22 0.37,0.29 0.59,0.22l2.39,-0.96c0.5,0.38 1.03,0.7 1.62,0.94l0.36,2.54c0.05,0.24 0.24,0.41 0.48,0.41h3.84c0.24,0 0.44,-0.17 0.47,-0.41l0.36,-2.54c0.59,-0.24 1.13,-0.56 1.62,-0.94l2.39,0.96c0.22,0.08 0.47,0 0.59,-0.22l1.92,-3.32c0.12,-0.22 0.07,-0.47 -0.12,-0.61L19.14,12.94zM12,15.6c-1.98,0 -3.6,-1.62 -3.6,-3.6s1.62,-3.6 3.6,-3.6s3.6,1.62 3.6,3.6S13.98,15.6 12,15.6z"/>
</vector>
42 changes: 23 additions & 19 deletions app/src/main/res/layout/fragment_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -26,6 +27,16 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/prof_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:src="@drawable/ic_prof_settings"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
</ImageView>

<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/user_shapeable_image"
Expand All @@ -42,6 +53,7 @@

<TextView
android:id="@+id/user_first_name"
style="@style/AdjustTitleBedu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
Expand All @@ -50,11 +62,11 @@
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/user_shapeable_image"
style="@style/AdjustTitleBedu" />
app:layout_constraintTop_toBottomOf="@+id/user_shapeable_image" />

<TextView
android:id="@+id/user_email"
style="@style/AdjustTitleBedu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
Expand All @@ -64,14 +76,13 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/user_first_name"
style="@style/AdjustTitleBedu" />
app:layout_constraintTop_toBottomOf="@+id/user_first_name" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/optionRecyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="40dp"
android:layout_marginTop="28dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -80,12 +91,16 @@

<Button
android:id="@+id/buttonCloseSession"
style="@style/MaterialButtonUniformStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginTop="35dp"
android:layout_marginBottom="40dp"
android:drawableLeft="@drawable/ic_power"
android:drawablePadding="10dp"
android:drawableTint="?attr/colorOnSecondary"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:singleLine="true"
android:text="@string/log_out"
android:textAlignment="center"
Expand All @@ -94,21 +109,10 @@
app:iconTint="?attr/colorOnSecondary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/optionRecyclerView"
style="@style/MaterialButtonUniformStyle" />

<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/theme_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dark_theme_switch_text"

app:layout_constraintBottom_toTopOf="@+id/user_shapeable_image"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
style="@style/NormalTextStyle" />
app:layout_constraintVertical_bias="1.0" />

</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Expand Down
Loading

0 comments on commit a989797

Please sign in to comment.