Prevent Click-through of Android ImageVIew?

26,779

Solution 1

Simply call rlTest.setClickable(false). This will prevent the click to be propagate to the children

Solution 2

you can set the image to be

android:clickable="true"

Solution 3

There is a much cleaner way

You can use:

android:onClick="preventClicks"

in XML and in your Activity

public void preventClicks(View view) {}

This works with fragments. Example inside this Activity has multiple fragments overlapping one another, just by adding the XML attribute in the background of your fragment it will still call the Activity.preventClicks and will prevent touches on fragments behind it

Solution 4

The following solution works in the general case:

_container.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // NOTE: This prevents the touches from propagating through the view and incorrectly invoking the button behind it
        return true;
    }
});

It basically blocks any touches from propagating through the view by marking the touch event as handled. This works on both UI controls and layout containers (ie: LinearLayout, FrameLayout etc.).

The solution to set "clickable" as false did not work for me for layout containers either in code or in the view XML.

Solution 5

I assume that you are using onClickListeners.

How about using onTouchListener instead of onClickListeners. By doing this you will have a control over how deep down in your hierarchy the touch even can be visible. For example, if you have toch listeners on a relative-layout(RL) and a image-view(IV)(contained in RL), and you assign touchListeners to both. Now if you return true from IV's touch event, the lower down member RL will not receive the touch event. However if you return false from from IV's touch event, the lower down member RL will receive the touch event.

Hope this helps!

Share:
26,779

Related videos on Youtube

Apqu
Author by

Apqu

Software developer based in Suffolk, UK. Specialising in: ASP.NET (C#) Xamarin Development HTML / CSS JavaScript / JQuery SQL Server 2000 - 2019

Updated on April 29, 2021

Comments

  • Apqu
    Apqu about 3 years

    I have an ImageView overlay inside of a RelativeLayout and want to prevent any clicks from going through the ImageView to the Buttons etc that are behind it (thus in effect disabling the entire RelativeLayout).

    Is the a simpler way of doing this then iterating the RelativeLayout views and setting them to disabled as I currently am doing using this code:

    RelativeLayout rlTest = (RelativeLayout ) findViewById(R.id.rlTest);
    for (int i = 0; i < rlTest.getChildCount(); i++) {
           View view = rlTest.getChildAt(i);
           view.setEnabled(true);
    }
    
  • Apqu
    Apqu about 10 years
    Thanks for your answer, the child views are still receiving the click/tap event do I need to also remove the handler for the "click" (TapListener).
  • Apqu
    Apqu about 10 years
    Ah awesome, thanks, that did the trick. I guess this is much less resource intensive than iterating all the views too!
  • Muhammad Babar
    Muhammad Babar over 9 years
    but setting onClickListener again make it clickable!
  • 林奕忠
    林奕忠 almost 9 years
    Don't do this way. Because I found BUG in this way that ACTION_UP MotionEvent will miss.
  • Mr.Lee
    Mr.Lee over 7 years
    Unfortunately this isn't an ideal solution when dealing with Talkback mode. Talkback will announce it as clickable although nothing is hooked up to the view.
  • yongsunCN
    yongsunCN over 7 years
    This works only if the lower view is not a button. Otherwise the button is still picking up the click event.