-1

I have a custom button that I build using anko DSL:

fun customButton(label: String): View {
    return UI {
        verticalLayout {
            button {
                id = BTN_SERVICE_ITEM_ID
            }.lparams(dip(64), dip(64))

            textView {
                text = label
            }.lparams(wrapContent, wrapContent)
        }
    }.view
}

then I add setOnClickListener on that button

val customButton = customButton("service 1")

layout.addView(customButton)

customButton.setOnClickListener {
    toast("clicked")
}

when I click the button, the toast is not shown. But when the area of the custom button is clicked, the toast is shown.

I know I can use findViewById to get the button then add setOnClickListener to it. But, is there any method so I can just attach onClickListener to the view?


EDIT:
I already try to add isEnabled=false, isClickable=false, isFocusable=false , isActivated=false and setOnClickListener(null) on the button. Still no luck.

rahmatfajar15
  • 29
  • 1
  • 8
  • your customButton is not a button. It is a layout that contains a button. You're setting a clicklistener on the layout, not the button – Tim Jul 09 '18 at 09:27
  • did you try my answer? –  Jul 09 '18 at 09:53
  • @TimCastelijns I know that. That what I was searching for. But the button ignoring the layout `onClickListener`. I also try to remove the `onClickListener` on the button so it capture the `onClickListener` on the layout. But, still no luck. – rahmatfajar15 Jul 09 '18 at 09:54
  • Total unresponsivness –  Jul 09 '18 at 11:09
  • @mTak dude, what's your problem? Not everyone have same time zone as yours. It's already night in here. And I'm planning to answer all of your questions/suggestions tomorrow. And now I misuse this comment section because of you. – rahmatfajar15 Jul 09 '18 at 13:18
  • You can leave a comment if you're unavailable. And how did your question appear again by a different username? –  Jul 09 '18 at 13:20
  • On where? Can you give me the link. I don't have any other account. – rahmatfajar15 Jul 09 '18 at 13:28
  • This is it: [link](https://stackoverflow.com/questions/51244951/android-click-event-view-with-button-and-text#comment89469645_51244951) –  Jul 09 '18 at 17:40

1 Answers1

0

The easiest solution would be just to pass an instance of View.OnClickListener to your method customButton.

I am not familiar with anko syntax, but it should look similar to this:

fun customButton(label: String, clickListener: View.OnClickListener): View {
    return UI {
        verticalLayout {
            button {
                id = BTN_SERVICE_ITEM_ID
                onClickListener = clickListener
            }.lparams(dip(64), dip(64))

            textView {
                text = label
            }.lparams(wrapContent, wrapContent)
        }
    }.view
}

After all, if you're up to making a dedicated method to create a button, why not to use full of it :)

jujka
  • 1,190
  • 13
  • 18
  • Aahhh. This never cross my mind. Gonna check this tomorrow. – rahmatfajar15 Jul 09 '18 at 13:25
  • 1
    I can't use `onClickListener = clickListener`, but use `this.setOnClickListener { clickListener() }` instead. And change the variable `clickListener` to a function so it become `clickListener: () -> Unit`. And then to use it `val button = customButton("YeY") { toast("YeY") }` – rahmatfajar15 Jul 11 '18 at 03:15