Skip to content

Instantly share code, notes, and snippets.

@Dev-Husnain
Last active April 17, 2025 19:33
Show Gist options
  • Select an option

  • Save Dev-Husnain/31e0d4c9fec735bb7b1968a15aeafb4d to your computer and use it in GitHub Desktop.

Select an option

Save Dev-Husnain/31e0d4c9fec735bb7b1968a15aeafb4d to your computer and use it in GitHub Desktop.
CustomView ( The View of different corner radius, backgrounds, rippleColor, shadowColor etc)
class CustomView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
private var cornerRadiusTopLeft = 0f
private var cornerRadiusTopRight = 0f
private var cornerRadiusBottomLeft = 0f
private var cornerRadiusBottomRight = 0f
private var strokeWidth = 0
private var strokeColor = Color.BLACK
private var backgroundColor = Color.WHITE
private var rippleColor = Color.LTGRAY
init {
context.withStyledAttributes(attrs, R.styleable.CustomView, defStyleAttr, 0) {
val cornerRadius = getDimension(R.styleable.CustomView_cornerRadius, 0f)
cornerRadiusTopLeft =
getDimension(R.styleable.CustomView_cornerRadiusTopLeft, cornerRadius)
cornerRadiusTopRight =
getDimension(R.styleable.CustomView_cornerRadiusTopRight, cornerRadius)
cornerRadiusBottomLeft =
getDimension(R.styleable.CustomView_cornerRadiusBottomLeft, cornerRadius)
cornerRadiusBottomRight =
getDimension(R.styleable.CustomView_cornerRadiusBottomRight, cornerRadius)
strokeWidth = getDimensionPixelSize(R.styleable.CustomView_strokeWidth, 0)
strokeColor = getColor(R.styleable.CustomView_strokeColor, Color.BLACK)
backgroundColor = getColor(R.styleable.CustomView_backgroundColor, Color.WHITE)
rippleColor = getColor(R.styleable.CustomView_rippleColor, Color.LTGRAY)
elevation = getDimension(R.styleable.CustomView_elevation, 0f)
}
setupBackground()
clipToOutline = true
}
private fun setupBackground() {
val backgroundDrawable = GradientDrawable().apply {
shape = GradientDrawable.RECTANGLE
setColor(backgroundColor)
setStroke(strokeWidth, strokeColor)
cornerRadii = floatArrayOf(
cornerRadiusTopLeft, cornerRadiusTopLeft,
cornerRadiusTopRight, cornerRadiusTopRight,
cornerRadiusBottomRight, cornerRadiusBottomRight,
cornerRadiusBottomLeft, cornerRadiusBottomLeft
)
}
val rippleDrawable = RippleDrawable(
android.content.res.ColorStateList.valueOf(rippleColor),
backgroundDrawable,
null
)
background = rippleDrawable
}
}
<com.google.android.material.button.MaterialButton
android:id="@+id/btnSend"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/_15sdp"
android:paddingVertical="@dimen/_10sdp"
android:text="@string/send_feedback"
android:textColor="@color/white"
android:textSize="@dimen/_14ssp"
android:textStyle="bold"
app:backgroundTint="@color/primaryColor"
app:cornerRadius="@dimen/_7sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
//put below in your attrs folder
<declare-styleable name="CustomView">
<attr name="cornerRadiusTopLeft" format="dimension" />
<attr name="cornerRadius" format="dimension" />
<attr name="cornerRadiusTopRight" format="dimension" />
<attr name="cornerRadiusBottomLeft" format="dimension" />
<attr name="cornerRadiusBottomRight" format="dimension" />
<attr name="strokeWidth" format="dimension" />
<attr name="strokeColor" format="color" />
<attr name="backgroundColor" format="color" />
<attr name="rippleColor" format="color" />
<attr name="elevation" format="dimension" />
<attr name="shadowColor" format="color" />
<attr name="shadowRadius" format="dimension" />
<attr name="shadowOffsetX" format="dimension" />
<attr name="shadowOffsetY" format="dimension" />
<attr name="shadowCornerRadius" format="dimension" />
<attr name="shadowCornerRadiusTopLeft" format="dimension" />
<attr name="shadowCornerRadiusTopRight" format="dimension" />
<attr name="shadowCornerRadiusBottomLeft" format="dimension" />
<attr name="shadowCornerRadiusBottomRight" format="dimension" />
</declare-styleable>
@developwithishfaq
Copy link

Amazinggg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment