I have to implement custom button looks like next picture
and this,
button.xml (layout resource)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/button_horizontal"
android:layout_marginVertical="@dimen/button_vertical"
android:divider="@drawable/divider"
android:orientation="horizontal"
android:showDividers="middle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/tv_button"
style="@style/BodyLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
XButton.kt
class XButton : AppCompatButton {
constructor(context: Context) : super(context) {
init(
context = context,
attrs = null,
)
}
constructor(
context: Context,
attrs: AttributeSet?,
) : super(
context,
attrs,
) {
init(
context = context,
attrs = attrs,
)
}
constructor(
context: Context,
attrs: AttributeSet?,
defStyleAttr: Int,
) : super(
context,
attrs,
defStyleAttr,
) {
init(
context = context,
attrs = attrs,
)
}
private lateinit var attributes: TypedArray
private fun init(
context: Context,
attrs: AttributeSet?,
) {
setAttributes(
context = context,
attrs = attrs,
)
}
private fun setAttributes(
context: Context,
attrs: AttributeSet?,
) {
attributes = context.obtainStyledAttributes(
attrs,
R.styleable.XButton,
)
}
}
I tried to inflate button.xml to XButton.kt but I can't..
Before, I implement custom button by extends LinearLayout.
Of course I can use my custom views in button.xml when I extends LinearLayout.
But I have to implement this button by extends AppCompatButton.
I can't use findViewById... because this return null object also binding not working.
I want how inflate my button.xml to XButton.kt!!
How to implement my custom button by extends AppCompatButton..?
Try to use view binding <- not working
Try to use findViewById <- return null object