Implemented functionality to pick a font, currently limited to 3 default fonts (sans-serif, serif, monospace)

This commit is contained in:
denizk0461
2023-05-16 10:25:29 +02:00
parent a1cab07ea9
commit eca9594a7e
5 changed files with 86 additions and 0 deletions
@@ -103,6 +103,9 @@ class QuoteStorage private constructor(context: Context) {
prefs.edit().putInt(KEY_BACKGROUND_TYPE, newValue).apply() prefs.edit().putInt(KEY_BACKGROUND_TYPE, newValue).apply()
} }
fun getTypefaceIndex(): Int = prefs.getInt(KEY_TYPEFACE, 0) fun getTypefaceIndex(): Int = prefs.getInt(KEY_TYPEFACE, 0)
fun setTypefaceIndex(newValue: Int) {
prefs.edit().putInt(KEY_TYPEFACE, newValue).apply()
}
fun getTypeface(): Typeface = Typeface.create( fun getTypeface(): Typeface = Typeface.create(
when (getTypefaceIndex()) { when (getTypefaceIndex()) {
1 -> Typeface.SERIF 1 -> Typeface.SERIF
@@ -5,6 +5,7 @@ import android.os.Bundle
import android.util.TypedValue import android.util.TypedValue
import android.view.View import android.view.View
import android.widget.ImageButton import android.widget.ImageButton
import android.widget.PopupMenu
import android.widget.TextView import android.widget.TextView
import androidx.annotation.ColorInt import androidx.annotation.ColorInt
import com.denizd.textbasic.BuildConfig import com.denizd.textbasic.BuildConfig
@@ -28,7 +29,59 @@ class SettingsNewFragment : BaseFragment(R.layout.fragment_settings_new) {
storage = QuoteStorage.getInstance(context) storage = QuoteStorage.getInstance(context)
// --- text font --- // // --- text font --- //
// binding.autoCompleteFont.setOnClickListener {
// // Create menu for selecting a new canteen
// PopupMenu(binding.root.context, binding.textLayoutFont).apply {
// setOnMenuItemClickListener { item ->
// item?.itemId?.let { id ->
// storage.setTypefaceStyle(id)
// }
//
// // Update the preview
// updatePreview()
// true
// }
// menu.add(0, 0, 0, getString(R.string.sansserif))
// menu.add(1, 1, 1, getString(R.string.serif))
// menu.add(2, 2, 2, getString(R.string.monospace))
// show()
// }
// }
// val fontAdapter = ArrayAdapter(
// context,
// R.layout.item_dropdown_menu,
// resources.getStringArray(R.array.text_fonts),
// )
//
// binding.autoCompleteFont.setAdapter(fontAdapter)
binding.autoCompleteFont
.setText(context.resources.getStringArray(R.array.text_fonts)[storage.getTypefaceIndex()])
binding.autoCompleteFont.setOnClickListener {
PopupMenu(binding.root.context, binding.autoCompleteFont).apply {
setOnMenuItemClickListener { item ->
item?.itemId?.let { id ->
val index = when (id) {
R.id.serif -> 1
R.id.monospace -> 2
else -> 0 // sans-serif
}
storage.setTypefaceIndex(index)
binding.autoCompleteFont
.setText(context.resources.getStringArray(R.array.text_fonts)[index])
}
// Refresh the canteen menu for the newly selected canteen
updatePreview()
true
}
inflate(R.menu.list_fonts)
show()
}
}
// --- font style --- // // --- font style --- //
binding.toggleGroupFont.apply { binding.toggleGroupFont.apply {
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:paddingHorizontal="16dp"
android:paddingVertical="16dp"/>
+15
View File
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/sansserif"
android:title="@string/sansserif"
android:enabled="true"/>
<item
android:id="@+id/serif"
android:title="@string/serif"
android:enabled="true"/>
<item
android:id="@+id/monospace"
android:title="@string/monospace"
android:enabled="true"/>
</menu>
+6
View File
@@ -79,6 +79,12 @@
<string name="serif">Serif</string> <string name="serif">Serif</string>
<string name="monospace">Monospace</string> <string name="monospace">Monospace</string>
<string-array name="text_fonts">
<item>Sans-Serif</item>
<item>Serif</item>
<item>Monospace</item>
</string-array>
<string name="background">Background</string> <string name="background">Background</string>
<string name="outline">Outline</string> <string name="outline">Outline</string>
<string name="shadow">Shadow</string> <string name="shadow">Shadow</string>