0

I am using an ImageView as a NEXT button in my Android app which is responsible for loading the next page of results into the current activity. However, despite that I bind a click listener to it, I cannot seem to capture click events on this ImageView. Here is the XML:

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="50"
    android:gravity="left"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/listBackIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/back_icon"/>
    <TextView
        android:id="@+id/listBackLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Prev"
        android:textSize="16dip"/>
</LinearLayout>

And here is the relevant Java code:

ImageView forwardIconView = (ImageView)findViewById(R.id.listBackIcon);
// not sure if necessary; doesn't fix it anyway
forwardIconView.setClickable(true);
forwardIconView.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        ++pageNumber;
        try {
            params.put("page", pageNumber);
        } catch (JSONException e) {
            // do something
        }
        ConnectionTask task = new ConnectionTask();
        task.execute(new String[0]);
    }
});

I spent about an hour researching this on Stack Overflow. I found a few places which claimed that ImageView could directly be made clickable, but most things recommended workarounds using other types of widgets.

Does anything about my layout/code stand out as being a culprit for this behavior?

Update:

I also tried binding a click listener to the TextView at the same level as the ImageView and this too failed to capture clicks. So now I am suspecting that the views are being masked by something. Perhaps something is capturing the clicks instead of the views.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • well, i use imageview click offen.. i don't think its usefull to write `forwardIconView.setClickable(true);` .. make sure ur another view is not over it, which taking control and getting onClick event before it.. – SRB Bans May 22 '17 at 16:03
  • 1
    @SRBbans Based on the fragment of layout I showed in my question, what could be capturing the click event instead of the two views? – Tim Biegeleisen May 22 '17 at 16:05
  • may be your some other view drawn over it... like dinamic views or something... but invisible and getting the click behaviour. just guessing .. – SRB Bans May 22 '17 at 16:10
  • have you checked by adding log inside onClick() method? – Ferdous Ahamed May 22 '17 at 16:13
  • Sanity check: are you sure you're looking at/clicking on the right thing? Your description is of a "NEXT" button, and the `ImageView` is `forwardIconView`, but its ID is `listBackIcon`. – Mike M. May 22 '17 at 17:44
  • Try to debug it and check if it reaches the onClickListener, if not, maybe it is a layout or view issue. – Daniel May 22 '17 at 17:44
  • It is the correct thing and elsewhere in the same activity I successfully bind a click listener to other text views. I will try to see what is covering these image views. – Tim Biegeleisen May 22 '17 at 23:41
  • 1
    A quick test to check if those are indeed being covered would be to temporarily replace one of those `View` types with `Button`, and see if you get any visual feedback when clicking. Beyond that, ya know, mcve. – Mike M. May 23 '17 at 00:17
  • @MikeM. I just took your advice and added a button, and it _does_ respond when clicked. – Tim Biegeleisen May 23 '17 at 06:24
  • What happens when you set an `OnClickListener` on it? Actually, how were you determining that the `OnClickListener` on the `ImageView` wasn't working? Did you put a break point there, or use a log print or `Toast` or something? – Mike M. May 23 '17 at 06:29
  • @MikeM. I actually added a listener to the button, and it worked. But the listeners and text and image views do not work. Strange. I suspect that the layout containing them is eating the events. Maybe the layout width of `0dp` is somehow responsible. – Tim Biegeleisen May 23 '17 at 06:30
  • Nah, the 0 width just means the parent `LinearLayout` should use the weight to determine the actual width. If you can see the child `View`s, it doesn't really have 0 width. You say you _added_ a `Button`? What happens if you just directly change ` – Mike M. May 23 '17 at 06:43

4 Answers4

0

I would set it up like this:

private ImageView nextButton;
nextButton = (ImageView)view.findViewById(R.id.back_button);  

Util.loadImage(getActivity(),R.drawable.some_image,nextButton); //Here i would load the image, but i see you do it in XML.

 nextButton.setOnClickListener(nextButtonListener);

 nextButton.setEnabled(true);

 View.OnClickListener nextButtonListener = new View.OnClickListener() {
    @Override
    public void onClick(View view) {

    Log.v(TAG, "ImageView has been clicked. do something.");

    }
};

This works for me.

0

Why not use android:drawableLeft attribute for the textview instead of using imageView​ and textview both in a linearlayout .

Haris ali
  • 773
  • 1
  • 13
  • 19
0
<ImageView
    android:id="@+id/listBackIcon"
    ...
    android:clickable="true"

Or you can try overriding onTouchListener with ACTION_DOWN event filter, not onClickListener. Also check for parrents with android:clickable="false", they could block childs for click events.

Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36
  • I will check the `clickable` properties of parents, thanks for the suggestion. – Tim Biegeleisen May 23 '17 at 01:35
  • @TimBiegeleisen, notice that your views can take focus and your wanted view can get it back if you have a very loaded view. Also try using an `ImageButton` instead `ImageView`. Also you can listen to your `LinearLayout` parent by making it `clickable=true` and his childs unclickables. – Cătălin Florescu May 23 '17 at 08:02
0

What seemed to work for me was the accepted answer from this SO question, which suggests adding the following the every child element of the LinearLayout which I pasted in my question:

android:duplicateParentState="true"

I don't know exactly what was happening, but it appears the click events were not making it down to the TextView and ImageView. Strangely, the click events were reaching a Button, when I added one for debugging purposes. If someone has more insight into what actually happened, leave a comment and this answer can be updated.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360