Skip to content

Commit

Permalink
Allow textSize change. Improve clickable chip foreground. Update preview
Browse files Browse the repository at this point in the history
  • Loading branch information
jahirfiquitiva committed Jun 7, 2018
1 parent a5a6b3c commit 01886b3
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class MainActivity : AppCompatActivity() {
findViewById<ChipView?>(R.id.action_chip)
}

private val strokeChip: ChipView? by lazy {
findViewById<ChipView?>(R.id.stroke_chip)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand All @@ -44,6 +48,8 @@ class MainActivity : AppCompatActivity() {

actionChip?.setOnClickListener { showToast("You pressed the chip with action icon") }
actionChip?.setOnActionClickListener { showToast("You pressed the action icon") }

strokeChip?.setOnClickListener { showToast("You pressed the stroked chip") }
}

private fun showToast(text: String) {
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@
android:layout_height="wrap_content"
app:chipText="I am Chip"
app:chipActionIcon="@drawable/ic_close"
app:chipElevation="2dp" />
app:chipElevation="2dp"
app:chipTextSize="18sp" />

<com.jahirfiquitiva.chip.ChipView
android:id="@+id/stroke_chip"
android:layout_margin="4dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:chipText="I am Chip"
app:chipText="I am Chip with a really long text and my text will be ellipsized"
app:chipBgColor="#81ecec"
app:chipStrokeWidth="1dp"
app:chipStrokeColor="#80000000" />
Expand Down
70 changes: 65 additions & 5 deletions library/src/main/kotlin/com/jahirfiquitiva/chip/ChipView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ package com.jahirfiquitiva.chip
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.PorterDuff
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.Icon
import android.graphics.drawable.RippleDrawable
import android.os.Build
import android.support.annotation.CallSuper
import android.support.annotation.ColorInt
Expand All @@ -31,6 +34,7 @@ import android.support.annotation.StringRes
import android.support.v4.content.ContextCompat
import android.support.v7.widget.CardView
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
Expand All @@ -53,15 +57,21 @@ open class ChipView : LinearLayout {
textView?.text = value
}

var textSize: Float
get() = textView?.textSize ?: 0.0F
set(value) {
textView?.textSize = value
}

var radius: Float
get() = cardView?.radius ?: 0.0F
set(value) {
cardView?.radius = value
}

@ColorInt
var bgColor: Int = 0
private set(value) {
private var bgColor: Int = 0
set(value) {
field = value
internalSetBackground()
}
Expand Down Expand Up @@ -104,6 +114,11 @@ open class ChipView : LinearLayout {
val styles = context.obtainStyledAttributes(attrs, R.styleable.ChipView, 0, 0)
try {
text = styles.getString(R.styleable.ChipView_chipText) ?: ""

val txtSize = styles.getDimensionPixelSize(R.styleable.ChipView_chipTextSize, 0)
if (txtSize > 0)
setTextSize(TypedValue.COMPLEX_UNIT_PX, txtSize.toFloat())

setTextColor(
styles.getColor(
R.styleable.ChipView_chipTextColor,
Expand Down Expand Up @@ -156,6 +171,12 @@ open class ChipView : LinearLayout {
textView?.setTextColor(colors)
}

@CallSuper
open fun setTextSize(unit: Int, size: Float) {
textSize = size
textView?.setTextSize(unit, size)
}

@CallSuper
open fun setIcon(@DrawableRes drawable: Int) {
setIcon(ContextCompat.getDrawable(context, drawable))
Expand Down Expand Up @@ -200,6 +221,17 @@ open class ChipView : LinearLayout {
actionIconView?.setImageIcon(actionIcon)
}

override fun getBackground(): Drawable? = cardView?.background

@ColorInt
fun getBackgroundColor(): Int = bgColor

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun getBackgroundTintList(): ColorStateList? = cardView?.backgroundTintList

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun getBackgroundTintMode(): PorterDuff.Mode? = cardView?.backgroundTintMode

@CallSuper
open fun setBackgroundColorFromRes(@ColorRes color: Int) {
setBackgroundColor(ContextCompat.getColor(context, color))
Expand Down Expand Up @@ -227,15 +259,43 @@ open class ChipView : LinearLayout {
}

private fun internalSetBackground() {
if (strokeWidth > 0) {
cardView?.background = GradientDrawable().apply {
val fgResId: Int = try {
val attrs = intArrayOf(R.attr.selectableItemBackground)
val typedArray = context.obtainStyledAttributes(attrs)
val fgRes = typedArray?.getResourceId(0, 0) ?: 0
typedArray?.recycle()
fgRes
} catch (ignored: Exception) {
0
}

val bgDrawable: Drawable? = if (strokeWidth > 0) {
GradientDrawable().apply {
shape = GradientDrawable.RECTANGLE
cornerRadii =
floatArrayOf(radius, radius, radius, radius, radius, radius, radius, radius)
setColor(bgColor)
setStroke(strokeWidth, strokeColor)
}
} else cardView?.setCardBackgroundColor(bgColor)
} else cardView?.background

@ColorInt
val fgColor: Int = if (bgColor.isColorDark()) bgColor.lighten() else bgColor.darken()

val fgDrawable: Drawable? = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ->
RippleDrawable(ColorStateList.valueOf(fgColor), null, bgDrawable)
fgResId != 0 -> ContextCompat.getDrawable(context, fgResId)
else -> null
}

if (strokeWidth > 0) {
cardView?.setCardBackgroundColor(Color.TRANSPARENT)
cardView?.background = bgDrawable
} else {
cardView?.setCardBackgroundColor(bgColor)
}
cardView?.foreground = fgDrawable
}

@CallSuper
Expand Down
48 changes: 31 additions & 17 deletions library/src/main/kotlin/com/jahirfiquitiva/chip/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,41 @@
package com.jahirfiquitiva.chip

import android.content.res.Resources
import android.graphics.Color
import android.support.annotation.ColorInt
import android.support.annotation.FloatRange

/**
* Created by Allan Wang
* Taken from his awesome library: KAU (https://github.com/AllanWang/KAU/)
*/

internal inline val Float.dpToPx: Float
get() = this * Resources.getSystem().displayMetrics.density

internal inline val Int.dpToPx: Int
get() = toFloat().dpToPx.toInt()

internal inline val Float.pxToDp: Float
get() = this / Resources.getSystem().displayMetrics.density

internal inline val Int.pxToDp: Int
get() = toFloat().pxToDp.toInt()

internal inline val Float.dpToSp: Float
get() = this * Resources.getSystem().displayMetrics.scaledDensity

internal inline val Int.dpToSp: Int
get() = toFloat().dpToSp.toInt()

internal inline val Float.spToDp: Float
get() = this / Resources.getSystem().displayMetrics.scaledDensity

internal inline val Int.spToDp: Int
get() = toFloat().spToDp.toInt()
internal fun Int.isColorDark(minDarkness: Float = 0.6F): Boolean =
((0.299 * Color.red(this) + 0.587 * Color.green(this) + 0.114 * Color.blue(
this)) / 255.0) < minDarkness

@ColorInt
internal fun Int.lighten(
@FloatRange(from = 0.0, to = 1.0)
factor: Float = 0.25F
): Int {
val (red, green, blue) = intArrayOf(Color.red(this), Color.green(this), Color.blue(this))
.map { (it * (1f - factor) + 255f * factor).toInt() }
return Color.argb(Color.alpha(this), red, green, blue)
}

@ColorInt
internal fun Int.darken(
@FloatRange(from = 0.0, to = 1.0)
factor: Float = 0.25F
): Int {
val (red, green, blue) = intArrayOf(Color.red(this), Color.green(this), Color.blue(this))
.map { (it * (1f - factor)).toInt() }
return Color.argb(Color.alpha(this), red, green, blue)
}
8 changes: 7 additions & 1 deletion library/src/main/res/layout/chip.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
android:layout_height="wrap_content"
android:clipToPadding="false"
android:clipChildren="false"
android:layout_gravity="center"
android:gravity="center"
android:animateLayoutChanges="true">

<android.support.v7.widget.CardView
Expand All @@ -33,6 +35,8 @@
android:clipToPadding="false"
android:clipChildren="false"
android:animateLayoutChanges="true"
android:layout_gravity="center"
android:gravity="center"
app:cardCornerRadius="@dimen/default_chip_radius"
app:cardElevation="@dimen/default_chip_elevation">

Expand All @@ -56,6 +60,7 @@
android:layout_marginLeft="@dimen/default_chip_icon_horizontal_margin"
android:layout_marginStart="@dimen/default_chip_icon_horizontal_margin"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:visibility="gone"
android:focusable="false"
android:clickable="false"
Expand All @@ -70,7 +75,7 @@
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:maxLines="1"
android:maxLength="@integer/default_max_chip_text_length"
android:ellipsize="end"
android:textSize="@dimen/default_chip_text_size"
android:focusable="false"
android:clickable="false" />
Expand All @@ -83,6 +88,7 @@
android:layout_marginEnd="@dimen/default_chip_icon_horizontal_margin"
android:layout_marginRight="@dimen/default_chip_icon_horizontal_margin"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:background="?selectableItemBackgroundBorderless"
android:visibility="gone"
android:focusable="true"
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<attr name="chipBgColor" format="color|reference" />
<attr name="chipText" format="string|reference" />
<attr name="chipTextColor" format="color|reference" />
<attr name="chipTextSize" format="dimension|reference" />
<attr name="chipElevation" format="dimension|reference" />
<attr name="chipRadius" format="dimension|reference" />
<attr name="chipIcon" format="reference" />
Expand Down
19 changes: 0 additions & 19 deletions library/src/main/res/values/integers.xml

This file was deleted.

Binary file modified preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 01886b3

Please sign in to comment.