0

I have the same problem as discussed on Handling click events on a drawable within an EditText, but I use TextView instead of EditText. When I click on the TextView, the method onTouchEvent(MotionEvent event) is called. Is it possible to handle a click only on the icon?

Community
  • 1
  • 1
Maksim Dmitriev
  • 5,985
  • 12
  • 73
  • 138

2 Answers2

0

I think the idea is pretty much the same as described here: use the event coordinates and determine if they are within the drawable's bounds. If they are, perform your action. I don't think there are any major differences between TextView and EditText in this respect.

Community
  • 1
  • 1
Andrii Chernenko
  • 9,873
  • 7
  • 71
  • 89
0

I think that it would be a better idea to use a TextView and an ImageView. Of course, it leads to a more complex layout structure, but I'd prefer two widgets in an XML file rather than too much Java code dealing with the widgets.

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/iv"
    android:text="@string/long_text"
    android:textSize="22sp" />

<ImageView
    android:id="@id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:contentDescription="@string/app_name"
    android:src="@drawable/btn_go" />

Community
  • 1
  • 1
Maksim Dmitriev
  • 5,985
  • 12
  • 73
  • 138