0

I have to implement custom button looks like next picture

enter image description here

and this,

enter image description here

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

Tmdhoon2
  • 11
  • 1
  • 3
  • Is there some reason you can't use one of the many pre-defined [button styles](https://m2.material.io/components/buttons/android)? It's a lot easier to use what exists... – Tyler V Aug 01 '23 at 04:17
  • I know.... but there is no icon button.. – Tmdhoon2 Aug 01 '23 at 04:33
  • Yes there is... Look in the link i posted for "Implementing an icon-only toggle button" – Tyler V Aug 01 '23 at 04:44
  • Or there are examples [here](https://stackoverflow.com/questions/54713087/how-to-center-icon-in-a-materialbutton-which-has-no-text), just set the icon and no text on the button – Tyler V Aug 01 '23 at 04:48
  • Or a plain old [`ImageButton`](https://developer.android.com/reference/android/widget/ImageButton) – Tyler V Aug 01 '23 at 04:59

0 Answers0