Skip to content

Instantly share code, notes, and snippets.

@kavan-mevada
Created September 8, 2018 05:21
Show Gist options
  • Save kavan-mevada/f25bb4f3d4455cecf22c3a50642905a5 to your computer and use it in GitHub Desktop.
Save kavan-mevada/f25bb4f3d4455cecf22c3a50642905a5 to your computer and use it in GitHub Desktop.
Chip like TextView [ Rounded Corners ]
package /* Package Name */
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.widget.TextView
class TextViewChip(context: Context, attrs: AttributeSet) : TextView(context, attrs) {
private val path = Path()
var newWidth = 0
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.setPadding((height/2f).toInt(), paddingTop, (height/2f).toInt(), paddingBottom)
super.onSizeChanged(w, h, oldw, oldh)
// compute the path
val radius = h / 2f
path.reset()
path.addRoundRect(RectF(0F, 0F, width.toFloat(), height.toFloat()), radius, radius, Path.Direction.CW)
path.close()
}
override fun draw(canvas: Canvas) {
if (newWidth<=0){
newWidth = measuredWidth+height
width = newWidth
}
val flag = canvas.save()
canvas.clipPath(path)
super.draw(canvas)
canvas.restoreToCount(flag)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment