From 68b690aa31cc85e255a80dfc1fa52ed3831b7f4c Mon Sep 17 00:00:00 2001 From: Finn M Glas Date: Thu, 21 May 2020 12:55:26 +0200 Subject: [PATCH] Update to better FontAwesome functions https://github.com/finnmglas/fontawesome-android --- .../com/finnmglas/launcher/FontAwesome.kt | 114 ++++++------------ app/src/main/res/values/attrs.xml | 8 ++ app/src/main/res/values/icons.xml | 18 ++- 3 files changed, 58 insertions(+), 82 deletions(-) create mode 100644 app/src/main/res/values/attrs.xml diff --git a/app/src/main/java/com/finnmglas/launcher/FontAwesome.kt b/app/src/main/java/com/finnmglas/launcher/FontAwesome.kt index 762d29a..7a4627a 100644 --- a/app/src/main/java/com/finnmglas/launcher/FontAwesome.kt +++ b/app/src/main/java/com/finnmglas/launcher/FontAwesome.kt @@ -7,87 +7,45 @@ import android.graphics.Typeface import android.util.AttributeSet import androidx.appcompat.widget.AppCompatTextView +/** [FontAwesome] is just a type of TextView with special functions: + * + * `setText(str)` can be used to change the icon + * `setIconType(Int)` changes the FontAwesome style ("solid", "regular" or "brand") + * `setTextColor(Int)` changes the color + * `setTextSize(Int, Float)` changes the icon size + */ -class FontAwesomeSolid : AppCompatTextView { - constructor( - context: Context?, - attrs: AttributeSet?, - defStyle: Int - ) : super(context, attrs, defStyle) { - init() +class FontAwesome : AppCompatTextView { + + var type = "" // "solid", "regular" or "brand" + + constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) + : super(context, attrs, defStyle) { init(attrs) } + constructor(context: Context?, attrs: AttributeSet?) + : super(context, attrs) { init(attrs) } + constructor(context: Context?) + : super(context) { init(null) } + + private fun init(attrs: AttributeSet?) { + if (attrs != null) { + val a = context!!.obtainStyledAttributes(attrs, R.styleable.FontAwesome) + if (a.hasValue(R.styleable.FontAwesome_type)) + type = a.getString(R.styleable.FontAwesome_type)!! + a.recycle() + if (type == "") type = "solid" + } + setIconType(type) } - constructor(context: Context?, attrs: AttributeSet?) : super( - context, - attrs - ) { - init() - } + // Useful if you want to change between a regular and solid icon (example: star) + fun setIconType(iconType : String){ + type = iconType - constructor(context: Context?) : super(context) { - init() - } - - private fun init() { - typeface = Typeface.createFromAsset( - context.assets, - "fontawesome/fa-solid-900.ttf" - ) - } -} - -class FontAwesomeRegular : AppCompatTextView { - constructor( - context: Context?, - attrs: AttributeSet?, - defStyle: Int - ) : super(context, attrs, defStyle) { - init() - } - - constructor(context: Context?, attrs: AttributeSet?) : super( - context, - attrs - ) { - init() - } - - constructor(context: Context?) : super(context) { - init() - } - - private fun init() { - typeface = Typeface.createFromAsset( - context.assets, - "fontawesome/fa-regular-400.ttf" - ) - } -} - -class FontAwesomeBrand : AppCompatTextView { - constructor( - context: Context?, - attrs: AttributeSet?, - defStyle: Int - ) : super(context, attrs, defStyle) { - init() - } - - constructor(context: Context?, attrs: AttributeSet?) : super( - context, - attrs - ) { - init() - } - - constructor(context: Context?) : super(context) { - init() - } - - private fun init() { - typeface = Typeface.createFromAsset( - context.assets, - "fontawesome/fa-brands-400.ttf" - ) + typeface = when (type) { + "regular" -> Typeface.createFromAsset(context!!.assets,"fontawesome/fa-regular-400.ttf") + "solid" -> Typeface.createFromAsset(context!!.assets,"fontawesome/fa-solid-900.ttf") + "brands" -> Typeface.createFromAsset(context!!.assets,"fontawesome/fa-brands-400.ttf") + else -> Typeface.createFromAsset(context!!.assets,"fontawesome/fa-solid-900.ttf") + } } } diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml new file mode 100644 index 0000000..f921e27 --- /dev/null +++ b/app/src/main/res/values/attrs.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/icons.xml b/app/src/main/res/values/icons.xml index 3243f56..44125b8 100644 --- a/app/src/main/res/values/icons.xml +++ b/app/src/main/res/values/icons.xml @@ -1,11 +1,21 @@ + - - - + + + + - + + + + + + + + +