0

I recently got a problem that my android button needs to be tapped two times to work. I already did a lot of researche but none of those solutions helped. Here is my code:

This is how I define the button:

<Button
    android:id="@+id/add_to_list_button"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:focusableInTouchMode="false"
    android:text=""
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

It is a button in a Recycler View Cell. And here is how I set the OnclickListener method in my Adapter! - Not Aktivity or Fragment: (I already defined this button in the adapter)

mAddButton = (Button) itemView.findViewById(R.id.add_to_list_button);

Here is the onBindViewHolder method

holder.mAddButton.setBackgroundResource(R.mipmap.ic_add_hs_24dp);
holder.mAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https:xxx"));
                v.getContext().startActivity(browserIntent);
            }
        });

I wish you guys could help me Identify the problem. I had been working on this for two days. Thank you so much for your help.

mayha
  • 603
  • 6
  • 16
Jacky.S
  • 364
  • 5
  • 17
  • Is the parent(list item) layout selectable as well? – art Jul 11 '18 at 14:33
  • I see you use `android:focusableInTouchMode="false"`, why? Are there any other views with this attribute set to true? –  Jul 11 '18 at 14:37
  • @mTak, No, the reason why I set that is because that is one of the solutions from StackOverflow. It still doesn't work if I remove this attribute – Jacky.S Jul 11 '18 at 14:49
  • Search for other views in the layout having similar attributes that may gain focus on 1st touch –  Jul 11 '18 at 15:04

1 Answers1

0

If your RecyclerView contains components that accept input events eg EditText or a Spinner, then there might be problems capturing click events in the OnItemClickListener or OnItemLongClickListener().

Possible solution is set your EditText/Spinner to disable events so that the click events will be relegated to the button

You can do this by setting

android:focusable="false"

and

android:focusableInTouchMode="false"

TennyApps
  • 189
  • 8