Added the ability to change fonts and font styles

This commit is contained in:
ottoptj 2024-09-05 04:29:49 +03:00
commit 84381d75eb
7 changed files with 158 additions and 31 deletions

View file

@ -63,7 +63,7 @@ class AppMenuAdapter(
}
inner class AppViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val listItem: FrameLayout = itemView.findViewById(R.id.listItem)
val listItem: FrameLayout = itemView.findViewById(R.id.listItem)
val textView: TextView = listItem.findViewById(R.id.appName)
val actionMenuLayout: LinearLayout = listItem.findViewById(R.id.actionMenu)
val editView: LinearLayout = listItem.findViewById(R.id.renameView)
@ -136,14 +136,15 @@ class AppMenuAdapter(
uiUtils.setItemSpacing(holder.textView)
uiUtils.setTextFont(holder.textView)
holder.textView.setTextColor(sharedPreferenceManager.getTextColor())
// Update the application information (allows updating apps to work)
val appInfo = appUtils.getAppInfo(
app.first.applicationInfo.packageName,
app.third
)
holder.textView.setTextColor(sharedPreferenceManager.getTextColor())
// Set app name on the menu. If the app has been uninstalled, replace it with "Removing" until the app menu updates.
val appLabel: CharSequence = appInfo?.let { activity.packageManager.getApplicationLabel(it) } ?: "Removing..."

View file

@ -296,6 +296,10 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
uiUtils.setMenuItemColors(searchView)
uiUtils.setMenuItemColors(binding.menuTitle, "A9")
uiUtils.setTextFont(binding.homeView)
uiUtils.setFont(searchView)
uiUtils.setFont(binding.menuTitle)
uiUtils.setClockVisibility(clock)
uiUtils.setDateVisibility(dateText)
uiUtils.setSearchVisibility(searchView, binding.searchReplacement)
@ -432,6 +436,18 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
uiUtils.setMenuItemColors(binding.menuTitle, "A9")
}
"textFont" -> {
uiUtils.setTextFont(binding.homeView)
uiUtils.setFont(searchView)
uiUtils.setFont(binding.menuTitle)
}
"textStyle" -> {
uiUtils.setTextFont(binding.homeView)
uiUtils.setFont(searchView)
uiUtils.setFont(binding.menuTitle)
}
"clockEnabled" -> {
uiUtils.setClockVisibility(clock)
}

View file

@ -3,6 +3,8 @@ package eu.ottop.yamlauncher.settings
import android.app.AlertDialog
import android.content.Context
import android.graphics.Color
import android.os.Handler
import android.os.Looper
import android.util.TypedValue
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
@ -30,7 +32,6 @@ class SharedPreferenceManager (private val context: Context) {
return Color.parseColor(textColor)
}
private fun getThemeColor(attr: Int): Int {
val typedValue = TypedValue()
val theme = context.theme
@ -38,6 +39,14 @@ class SharedPreferenceManager (private val context: Context) {
return typedValue.data
}
fun getTextFont(): String? {
return preferences.getString("textFont", "system")
}
fun getTextStyle(): String? {
return preferences.getString("textStyle", "normal")
}
fun isBarVisible(): Boolean {
return preferences.getBoolean("barVisibility", false)
}
@ -247,21 +256,26 @@ class SharedPreferenceManager (private val context: Context) {
editor.clear()
editor.apply()
// We need to navigate through the fragments to apply all settings properly
activity.supportFragmentManager
.beginTransaction()
.replace(R.id.settingsLayout, UISettingsFragment())
.commit()
activity.supportFragmentManager
.beginTransaction()
.replace(R.id.settingsLayout, HomeSettingsFragment())
.commit()
activity.supportFragmentManager
.beginTransaction()
.replace(R.id.settingsLayout, AppMenuSettingsFragment())
.commit()
activity.supportFragmentManager
.beginTransaction()
.replace(R.id.settingsLayout, SettingsFragment())
.commit()
// The swapping after ui settings needs to be delayed or font changes don't work in app menu
val handler = Handler(Looper.getMainLooper())
handler.postDelayed({
activity.supportFragmentManager
.beginTransaction()
.replace(R.id.settingsLayout, HomeSettingsFragment())
.commit()
activity.supportFragmentManager
.beginTransaction()
.replace(R.id.settingsLayout, AppMenuSettingsFragment())
.commit()
activity.supportFragmentManager
.beginTransaction()
.replace(R.id.settingsLayout, SettingsFragment())
.commit()
}, 50)
}
}

View file

@ -4,6 +4,7 @@ import android.content.Context
import android.graphics.BlendMode
import android.graphics.BlendModeColorFilter
import android.graphics.Color
import android.graphics.Typeface
import android.util.TypedValue
import android.view.Gravity
import android.view.View
@ -50,7 +51,6 @@ class UIUtils(private val context: Context) {
BlendModeColorFilter(sharedPreferenceManager.getTextColor(), BlendMode.SRC_ATOP)
view.compoundDrawables[2]?.colorFilter =
BlendModeColorFilter(sharedPreferenceManager.getTextColor(), BlendMode.SRC_ATOP)
}
else -> {
view.setBackgroundColor(color)
@ -62,28 +62,56 @@ class UIUtils(private val context: Context) {
return try {
view.javaClass.getMethod(methodName, Int::class.java)
true
} catch (e: NoSuchMethodException) {
} catch (_: NoSuchMethodException) {
false
}
}
fun setMenuItemColors(view: TextView, alphaHex: String = "FF") {
val viewTreeObserver = view.viewTreeObserver
val color = sharedPreferenceManager.getTextColor()
view.setTextColor(setAlpha(color, alphaHex))
view.setHintTextColor(setAlpha(color, "A9"))
val globalLayoutListener = object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
val color = sharedPreferenceManager.getTextColor()
view.setTextColor(setAlpha(color, alphaHex))
view.setHintTextColor(setAlpha(color, "A9"))
view.compoundDrawables[0]?.mutate()?.colorFilter = BlendModeColorFilter(color, BlendMode.SRC_ATOP)
}
view.compoundDrawables[0]?.mutate()?.colorFilter = BlendModeColorFilter(color, BlendMode.SRC_ATOP)
fun setTextFont(view: View) {
when {
view is ViewGroup -> {
view.children.forEach { child ->
setTextFont(child)
}
}
hasMethod(view, "setTextAppearance") -> {
setFont(view as TextView)
}
}
}
if (viewTreeObserver.isAlive) {
viewTreeObserver.addOnGlobalLayoutListener(globalLayoutListener)
fun setFont(view: TextView) {
var font = sharedPreferenceManager.getTextFont()
val style = sharedPreferenceManager.getTextStyle()
if (font == "system") {
val typedArray = context.obtainStyledAttributes(android.R.style.TextAppearance_DeviceDefault, intArrayOf(android.R.attr.fontFamily))
font = typedArray.getString(0)
typedArray.recycle()
}
when (style) {
"normal" -> {
view.setTypeface(Typeface.create(font, Typeface.NORMAL))
}
"bold" -> {
view.setTypeface(Typeface.create(font, Typeface.BOLD))
}
"italic" -> {
view.setTypeface(Typeface.create(font, Typeface.ITALIC))
}
"bold-italic" -> {
view.setTypeface(Typeface.create(font, Typeface.BOLD_ITALIC))
}
}
}

View file

@ -99,7 +99,7 @@
android:layout_marginHorizontal="32dp"
android:layout_marginTop="45dp"
android:layout_marginBottom="27dp"
android:fontFamily="@null"
android:fontFamily="More Fonts..."
android:format12Hour="hh:mm"
android:format24Hour="HH:mm"
android:textAlignment="textStart"
@ -115,7 +115,6 @@
android:id="@+id/textDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:fontFamily="@null"
android:format12Hour="dd MMM yyyy"
android:format24Hour="dd MMM yyyy"
android:lineSpacingExtra="8sp"

View file

@ -31,6 +31,56 @@
<item>material</item>
</string-array>
<string-array name="font_options">
<item>System</item>
<item>Casual</item>
<item>Cursive</item>
<item>Monospace</item>
<item>Sans Serif</item>
<item>Sans Serif Black</item>
<item>Sans Serif Condensed</item>
<item>Sans Serif Condensed Light</item>
<item>Sans Serif Condensed Medium</item>
<item>Sans Serif Light</item>
<item>Sans Serif Medium</item>
<item>Sans Serif Smallcaps</item>
<item>Sans Serif Thin</item>
<item>Serif</item>
<item>Serif Monospace</item>
</string-array>
<string-array name="font_values">
<item>system</item>
<item>casual</item>
<item>cursive</item>
<item>monospace</item>
<item>sans-serif</item>
<item>sans-serif-black</item>
<item>sans-serif-condensed</item>
<item>sans-serif-condensed-light</item>
<item>sans-serif-condensed-medium</item>
<item>sans-serif-light</item>
<item>sans-serif-medium</item>
<item>sans-serif-smallcaps</item>
<item>sans-serif-thin</item>
<item>serif</item>
<item>serif-monospace</item>
</string-array>
<string-array name="style_options">
<item>Normal</item>
<item>Bold</item>
<item>Italic</item>
<item>Bold-Italic</item>
</string-array>
<string-array name="style_values">
<item>normal</item>
<item>bold</item>
<item>italic</item>
<item>bold-italic</item>
</string-array>
<string-array name="animation_options">
<item>0.25x</item>
<item>0.5x</item>

View file

@ -4,7 +4,7 @@
<PreferenceCategory
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:title="Colors"
android:title="Appearance"
app:allowDividerAbove="false">
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
@ -24,6 +24,25 @@
app:key="textColor"
app:title="Text Color"
app:useSimpleSummaryProvider="true" />
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:defaultValue="system"
android:entries="@array/font_options"
android:entryValues="@array/font_values"
app:key="textFont"
app:title="Text Font"
app:useSimpleSummaryProvider="true" />
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:defaultValue="system"
android:entries="@array/style_options"
android:entryValues="@array/style_values"
app:key="textStyle"
app:title="Text Style"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory
android:title="Operation"