3

I have a few imageview which have onclicklistener. If I press one (not release), I can press click others or I can click them same time. I do not want it. Everytime when I press one of them others should be disable to click.

imageview1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                getMethod();

            }
        });

I guess, I tried setClickable(false); but it did not work properly, if I clicked one button after that it worked.

John simit
  • 1,305
  • 2
  • 11
  • 14
  • see this link it may help you http://stackoverflow.com/questions/12111862/disabling-multitouch-in-android – Senthil May 28 '13 at 08:51
  • `android:splitMotionEvents="false"` is not enough alone. I use one `onClick()` and `switch case` for view. It works fine. enable again `onResume` Here are the samples http://stackoverflow.com/questions/3320115/android-onclicklistener-identify-a-button http://stackoverflow.com/questions/3505841/onclick-listener-in-android http://stackoverflow.com/questions/6133236/can-you-use-the-same-onclicklistener-for-different-buttons – John simit May 28 '13 at 10:29
  • @Johnsimit: See my answer, you want `setEnabled(false)` in an `onTouchListener`. Otherwise you'll be able to push two buttons at the same time. You'll also have to re-enable them later, but this is the best way to do it. – Cornholio May 28 '13 at 10:36
  • @Cornholio, How can I use one `onTouchListener` for all imageviews like linked above comments? Because if it works, it will make easier to me... – John simit May 28 '13 at 12:12
  • @Johnsimit - I'll add some code to my answer. – Cornholio May 28 '13 at 16:09

3 Answers3

3

Try using onTouchListener instead of onClickListener and calling setEnabled(false); on the other views there. Here's a fairly basic example:

OnTouchListener onTouchListener = new OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            imageView1.setEnabled(false);
            imageView2.setEnabled(false);
        }
        return true;
    }
};

And then apply it to the image views with:

imageView1.setOnTouchListener(onTouchListener);

That should work. One thing is, though, that while you'll only be able to push one button no matter what, you also won't be able to push anything after you let go - but, you can fix that by adding some logic to see if the view actually got clicked or if the user touched it, changed their mind and slid away. The (event.getAction() == MotionEvent.ACTION_DOWN) check will be true even if the user is just scrolling.

Cornholio
  • 985
  • 1
  • 5
  • 22
  • There's also something called an `OnFocusChangeListener` that I've never used, but might suit your purposes better. – Cornholio May 28 '13 at 16:39
3
//button on which press u want to disable others
button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
              button2.setEnabled(false);  //button which u want to disable
              button3.setEnabled(false);  //button which u want to disable
            }
        });

//update fixed a spelling error
King
  • 256
  • 1
  • 11
PankajSharma
  • 1,529
  • 14
  • 27
1

try to disable the button and

button.setEnable(false);

enable the button

button1.setEnable(true);
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50