Skip to content

Instantly share code, notes, and snippets.

@toe-lie
Last active August 4, 2020 05:34
Show Gist options
  • Save toe-lie/5c173d05d1ee4004a15300585a33e2e0 to your computer and use it in GitHub Desktop.
Save toe-lie/5c173d05d1ee4004a15300585a33e2e0 to your computer and use it in GitHub Desktop.
Noti Badge Count View
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="NotiBadgeCountView">
<attr name="count" format="integer" />
<attr name="backgroundColor" format="color" />
</declare-styleable>
</resources>
package com.example.androidplayground
import android.content.Context
import android.content.res.Resources
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.RoundRectShape
import android.util.AttributeSet
import android.view.Gravity
import androidx.appcompat.widget.AppCompatTextView
class NotiBadgeCountView @JvmOverloads constructor(
context: Context, attrs:
AttributeSet? = null,
defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr) {
private var _count: Int = 0
private var _backgroundColor: Int = DEFAULT_BACKGROUND_COLOR
init {
val a = context.obtainStyledAttributes(
attrs, R.styleable.NotiBadgeCountView, 0, 0
)
a.getInt(R.styleable.NotiBadgeCountView_count, 0).let { count ->
_count = count
setCount(count)
}
_backgroundColor =
a.getColor(R.styleable.NotiBadgeCountView_backgroundColor,
DEFAULT_BACKGROUND_COLOR
)
updateBackground()
gravity = Gravity.CENTER
a.recycle()
}
fun setCount(count: Int) {
_count = count
text = if (!isLengthTooLong()) count.toString() else "${LIMIT}+"
updateBackground()
}
private fun isLengthTooLong(): Boolean {
return _count > LIMIT
}
private fun updateBackground() {
background = if (isLengthTooLong()) {
createRectangleBackground()
} else {
createCircleBackground()
}
}
private fun createCircleBackground(): Drawable {
val drawable = GradientDrawable()
drawable.shape = GradientDrawable.OVAL
drawable.setSize(dpToPx(24), dpToPx(24))
drawable.setColor(_backgroundColor)
return drawable
}
private fun createRectangleBackground(): Drawable {
val cornerRadius = dpToPx(4).toFloat()
val rectShape = RoundRectShape(
floatArrayOf(
cornerRadius,
cornerRadius,
cornerRadius,
cornerRadius,
cornerRadius,
cornerRadius,
cornerRadius,
cornerRadius
), null, null
)
val drawable = ShapeDrawable(rectShape)
drawable.paint.color = _backgroundColor
drawable.intrinsicHeight = dpToPx(24)
drawable.intrinsicWidth = dpToPx(36)
drawable.setPadding(dpToPx(8), 0, dpToPx(8), 0)
return drawable
}
companion object {
private val DEFAULT_BACKGROUND_COLOR: Int = Color.parseColor("#ff0000")
private const val LIMIT = 99
}
}
fun dpToPx(dp: Int): Int {
return (dp * Resources.getSystem().displayMetrics.density).toInt()
}
<com.example.androidplayground.NotiBadgeCountView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:count="7" />
<com.example.androidplayground.NotiBadgeCountView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundColor="@color/colorAccent"
app:count="100" />
https://github.com/toe-lie/public-images/blob/master/NotiBadgeCountView-screenshot.png?raw=true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment