From a5af4f888035a269158a7cd30eefa7f311068f46 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Sat, 25 Mar 2023 15:28:51 +0100 Subject: [PATCH] Add a new layout to handle text on top of a round background --- .../android/utils/CircleImageView.kt | 61 +++++++++++++++++++ .../src/main/res/layout/circle_image_view.xml | 26 ++++++++ androidApp/src/main/res/values/styles.xml | 6 ++ 3 files changed, 93 insertions(+) create mode 100644 androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/CircleImageView.kt create mode 100644 androidApp/src/main/res/layout/circle_image_view.xml diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/CircleImageView.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/CircleImageView.kt new file mode 100644 index 0000000..2ebd1d4 --- /dev/null +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/CircleImageView.kt @@ -0,0 +1,61 @@ +package bou.amine.apps.readerforselfossv2.android.utils + +import android.content.Context +import android.graphics.drawable.GradientDrawable +import android.util.AttributeSet +import android.view.LayoutInflater +import android.view.View +import android.widget.RelativeLayout +import android.widget.TextView +import bou.amine.apps.readerforselfossv2.android.R +import com.google.android.material.imageview.ShapeableImageView +import kotlin.math.abs + +class CircleImageView @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = 0 +) : RelativeLayout(context, attrs, defStyleAttr) { + val view: View + val imageView: ShapeableImageView + val textView: TextView + + private val colorScheme = listOf( + -0x1a8c8d, + -0xf9d6e, + -0x459738, + -0x6a8a33, + -0x867935, + -0x9b4a0a, + -0xb03c09, + -0xb22f1f, + -0xb24954, + -0x7e387c, + -0x512a7f, + -0x759b, + -0x2b1ea9, + -0x2ab1, + -0x48b3, + -0x5e7781, + -0x6f5b52 + ) + + init { + view = LayoutInflater.from(context).inflate(R.layout.circle_image_view, this, true) + imageView = view.findViewById(R.id.circleImage) + textView = view.findViewById(R.id.circleText) + } + + fun setBackgroundAndText(colorIdentifier: String, text: String) { + val circleDrawable = GradientDrawable() + val color = colorFromIdentifier(colorIdentifier) + circleDrawable.setColor(color) + imageView.setImageDrawable(circleDrawable) + + textView.text = text + } + + private fun colorFromIdentifier(key: String): Int { + return colorScheme[abs(key.hashCode()) % colorScheme.size] + } +} \ No newline at end of file diff --git a/androidApp/src/main/res/layout/circle_image_view.xml b/androidApp/src/main/res/layout/circle_image_view.xml new file mode 100644 index 0000000..040361b --- /dev/null +++ b/androidApp/src/main/res/layout/circle_image_view.xml @@ -0,0 +1,26 @@ + + + + + + + diff --git a/androidApp/src/main/res/values/styles.xml b/androidApp/src/main/res/values/styles.xml index 9ba9049..166644e 100644 --- a/androidApp/src/main/res/values/styles.xml +++ b/androidApp/src/main/res/values/styles.xml @@ -32,4 +32,10 @@ @null true + + +