Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented configuration block to access other setting like font scale. #11

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .idea/modules.xml

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

10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class MyApp : Application() {
private val localeAppDelegate = LocaleHelperApplicationDelegate()

override fun attachBaseContext(base: Context) {
LocaleHelper.configurationBlock = {
//Access configuration object to set exta setting
//For eg: FontScale
it.fontScale = 1.0f
}
super.attachBaseContext(localeAppDelegate.attachBaseContext(base))
}

Expand All @@ -77,6 +82,11 @@ open class BaseActivity : AppCompatActivity() {
override fun getDelegate() = localeDelegate.getAppCompatDelegate(super.getDelegate())

override fun attachBaseContext(newBase: Context) {
LocaleHelper.configurationBlock = {
//Access configuration object to set exta setting
//For eg: FontScale
it.fontScale = 1.0f
}
super.attachBaseContext(localeDelegate.attachBaseContext(newBase))
}

Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/zeugmasolutions/localeexample/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
import androidx.multidex.MultiDex
import com.zeugmasolutions.localehelper.LocaleAwareApplication
import com.zeugmasolutions.localehelper.LocaleHelper

class Application : LocaleAwareApplication() {

companion object {
var FONT_SCALE = 1.0f
}

override fun onCreate() {
super.onCreate()
AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM)
}

override fun attachBaseContext(base: Context) {
LocaleHelper.configurationBlock = {
it.fontScale = FONT_SCALE
}

super.attachBaseContext(base)
MultiDex.install(this)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package com.zeugmasolutions.localeexample

import android.content.Context
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO
import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES
import com.zeugmasolutions.localehelper.LocaleAwareCompatActivity
import com.zeugmasolutions.localehelper.LocaleHelper

open class BaseActivity : LocaleAwareCompatActivity() {

override fun attachBaseContext(newBase: Context) {
LocaleHelper.configurationBlock = {
it.fontScale = Application.FONT_SCALE
}
super.attachBaseContext(newBase)
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.main_menu, menu)
return true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.zeugmasolutions.localeexample

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.SeekBar
import com.zeugmasolutions.localehelper.Locales
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.view_language_buttons.*
import java.util.*
import kotlin.math.roundToInt

class MainActivity : BaseActivity() {

Expand All @@ -21,8 +24,33 @@ class MainActivity : BaseActivity() {
toURButton.setOnClickListener { updateLocale(Locales.Urdu) }

secondButton.setOnClickListener { startActivity(Intent(this, SecondActivity::class.java)) }

fontSizeSeekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
fontSizeSeekbar?.setProgress(progress, true)
}
}

override fun onStartTrackingTouch(seekBar: SeekBar?) {}

override fun onStopTrackingTouch(seekBar: SeekBar?) {
seekBar?.run {
Application.FONT_SCALE = getFontScale(progress)
recreate()
}
}

})

fontSizeSeekbar.max = 3
fontSizeSeekbar.progress = getSeekBarProgress(resources.configuration.fontScale)
}

private fun getSeekBarProgress(fontScale: Float) = ((fontScale - 0.8F) / 1.5F).roundToInt()

private fun getFontScale(progressValue: Int) = 0.8F + (progressValue * 1.5F)

override fun updateLocale(locale: Locale) {
super.updateLocale(locale)
setTitle(R.string.main_activity_title)
Expand Down
102 changes: 62 additions & 40 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,44 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_activity_title"
android:textStyle="bold"/>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<TextView
android:id="@+id/descTextView"
android:layout_width="wrap_content"
<LinearLayout
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/main_activity_desc"/>
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<TextView
android:id="@+id/aboutTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/main_activity_about"/>

<include layout="@layout/view_language_buttons"/>

<com.google.android.material.button.MaterialButton
style="@style/AppTheme.Button.Text"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/main_second_activity"
android:id="@+id/secondButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_activity_title"
android:textStyle="bold" />

<TextView
android:id="@+id/descTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/main_activity_desc" />

<TextView
android:id="@+id/aboutTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/main_activity_about" />

<include layout="@layout/view_language_buttons" />

<com.google.android.material.button.MaterialButton
android:id="@+id/secondButton"
style="@style/AppTheme.Button.Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/main_second_activity" />

<TextView
android:id="@+id/fontSizeTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/accessibality_font_size"
android:textStyle="bold" />

<SeekBar
android:id="@+id/fontSizeSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</LinearLayout>
</ScrollView>
1 change: 1 addition & 0 deletions app/src/main/res/values-tr-rTR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
<string name="main_second_activity">Ikinci sayfa ></string>
<string name="back_button"><![CDATA[< Geri]]></string>
<string name="main_activity_to_ur_button">Urduca</string>
<string name="accessibality_font_size">Erişilebilirlik: Yazı Tipi Boyutu</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-ur/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
<string name="main_second_activity">دوسرا صفحہ></string>
<string name="back_button"><![CDATA[واپس جاؤ]]></string>
<string name="main_activity_to_ur_button">اردو</string>
<string name="accessibality_font_size">رسائی: فونٹ سائز</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
<string name="main_second_activity">第二页 ></string>
<string name="back_button"><![CDATA[< 回去]]></string>
<string name="main_activity_to_ur_button">乌尔都语</string>
<string name="accessibality_font_size">輔助功能:字體大小</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
<string name="main_activity_to_ur_button">Urdu</string>
<string name="dark" translatable="false">Dark</string>
<string name="light" translatable="false">Light</string>
<string name="accessibality_font_size">Accessibality: Font Size</string>
</resources>
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Sun Apr 19 15:29:34 BST 2020
#Fri Jun 12 03:43:17 GST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.zeugmasolutions.localehelper

import android.content.Context
import android.content.SharedPreferences
import android.content.res.Configuration
import android.os.Build
import java.util.*

Expand All @@ -10,6 +11,8 @@ object LocaleHelper {
private const val SELECTED_COUNTRY = "Locale.Helper.Selected.Country"
private var initialized = false

var configurationBlock: ((Configuration) -> Unit)? = null

/**
* Attach the selected or default [Locale] to the [context]
*/
Expand Down Expand Up @@ -68,6 +71,8 @@ object LocaleHelper {
configuration.setLayoutDirection(locale)
}

configurationBlock?.invoke(configuration)

return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
context.createConfigurationContext(configuration)
} else {
Expand Down