Home > Software design >  button does not respond to taps/clicks
button does not respond to taps/clicks

Time:01-03

I'm trying perform an action on button click with this code.

            val button = findViewById<ExtendedFloatingActionButton>(R.id.button)
            button.setOnClickListener {
                // perform action
            }

But it has no effect, because the button is inside this linear layout. how can I fix this?

  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:id="@ id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginEnd="8dp"

        android:text="test"

        app:icon="@drawable/star"
        />

  </LinearLayout>

when I remove the layout, the button works fine. the layout (which I really need) is causing the issue

CodePudding user response:

From the comments under the Question.

Please consider this pseudocode, I just want to make the structure clear

<ConstraintLayout>
  <LinearLayout
    width = "match_parent"
    height = "match_parent">

    <!-- all your linear layout views -->

  </LinearLayout>

  <FloatingActionButton
    constraintBottomToBottomOf="parent"
    constraintEndToEndOf="parent"
    marginEnd="16dp"
    marginBottom="16dp"
    ... />

</ConstraintLayout>

CodePudding user response:

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1" />

try to declare fixed width for this view. Like 50dp or add weight to elements in your horizontal linear layout. This view may be in front of your button and when you click button you actually click this view

  • Related