Search code examples
androidclickableonlongclicklistener

Android: How to programmatically set longclickable of a view to true while keeping its clickable false?


I have a view that initially starts like this in the xml:

<ImageButton
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:id="@+id/likeBtn"
            android:src="@drawable/like"
            android:onClick="like"
            android:longClickable="true"
            android:clickable="true" />

and I already have a setOnLongClickListenter in the OnCreate of mainActivity, like this:

likeBtn.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                showExtraLike(v);
                return true;
            }
        });

My problem is:

when I run a code like that

likeBtn.setClickable(false);
likeBtn.setLongClickable(false);  

/* some other code here */

likeBtn.setLongClickable(true);

I found that the view becomes clickable also as well !!

I need it to be ONLY LongClickable and NOT clickable for sometime as I'll enable both again after few lines in the code.

Notes:

  • Disabling both and enabling them again works fine.
  • Disabling LongClick only and enabling it again works fine.
  • My only problem is that setting clickable to false seems ineffective when longClickable is true!

Solution

  • I've reached a workaround.

    the clickable and longClickable in XML are both set to true:

    <ImageButton
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:id="@+id/likeBtn"
                    android:src="@drawable/like"
                    android:clickable="true"
                    android:longClickable="true"/>
    

    but I removed all the setClickable and setLongClickable from the mainActivity and made the following in the onCreate to make them do nothing in certain cases:

    // setting click listener for question button
            likeBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (var == 1 || var == 3 || var == 4);
                       // do nothing
    
                    else
                        like(v);
                }
            });
    
            // setting longclick listener for question button
            likeBtn.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (var == 1 || var == 2 || var == 4)
                       likeBtn.setHapticFeedbackEnabled(false);
    
                else{
                    likeBtn.setHapticFeedbackEnabled(true);
                    showExtraLike(v);
                }
    
                    return true;
                }
            });
    

    And it works!

    but, of course, when the longClick acts as disabled, it was still vibrating the device... that's why I added the setHapticFeedbackEnabled.