package net.lacolaco.smileessence.view import android.content.Context import android.content.res.TypedArray import android.graphics.Canvas import android.graphics.Paint import android.util.AttributeSet import android.widget.RelativeLayout import net.lacolaco.smileessence.R class ColoredRelativeLayout @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0) : RelativeLayout(context, attrs, defStyleAttr, defStyleRes) { private val paint = Paint() private val highlightColors = IntArray(4) private var showAccent = false init { setWillNotDraw(false) val ta = context.obtainStyledAttributes(attrs, R.styleable.ColoredRelativeLayout) highlightColors[0] = ta.getColor(R.styleable.ColoredRelativeLayout_highlight_none, R.color.black) highlightColors[1] = ta.getColor(R.styleable.ColoredRelativeLayout_highlight_type1, R.color.orange) highlightColors[2] = ta.getColor(R.styleable.ColoredRelativeLayout_highlight_type2, R.color.green) highlightColors[3] = ta.getColor(R.styleable.ColoredRelativeLayout_highlight_type3, R.color.metro_blue) setHighlight(HIGHLIGHT_NONE) paint.color = ta.getColor(R.styleable.ColoredRelativeLayout_accent_color, R.color.red) paint.style = Paint.Style.STROKE paint.strokeWidth = 3f ta.recycle() } override fun onDraw(canvas: Canvas) { super.onDraw(canvas) if (showAccent) canvas.drawLine(1f, 0f, 1f, measuredHeight.toFloat(), paint) } fun setAccentVisibility(yes: Boolean) { showAccent = yes } fun setHighlight(type: Int) { setBackgroundColor(highlightColors[type]) } companion object { val HIGHLIGHT_NONE = 0 } }