3

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!
M. Sherbeeny
  • 109
  • 2
  • 12

4 Answers4

1

Just set onlongclicklistener like this:

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

Hope it helps you

Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
0

first enable the longclickable and add set onlongclicklistener

likeBtn.setLongClickable(true);

likeBtn.setOnLongClickListener(new View.OnLongClickListener() {
           @Override
           public boolean onLongClick(View v) {
               return true;
           }
       });
Rgv
  • 504
  • 5
  • 23
0

Your problem is you are calling click method for activity from your xml. remove that lineand initially set both to false if you want to enable or disable clicks.. replace your xml code with

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

now in your activity override OnClick And onLongClick methods.

 findViewById(R.id.likeBtn).setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(MainActivity.this,"like long clicked",Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    findViewById(R.id.likeBtn).setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(MainActivity.this,"like long clicked",Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    findViewById(R.id.likeBtn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
     // Call LOGIN();
            Toast.makeText(MainActivity.this,"like  clicked",Toast.LENGTH_SHORT).show();
        }
    });

now you can enable and disable it anywhere from activity. hope this helps. :)

Newbiee
  • 592
  • 6
  • 22
0

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.

M. Sherbeeny
  • 109
  • 2
  • 12