Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement onClick only for a TextView compound drawable

I need to have some text with a drawable on the left and I want to execute some code when the user clicks/touches the image (only the image, not the text), so I used a LinearLayout with a TextView and an ImageView which is clickable and launches an onClick event. The XML parser suggests me to replace this with a TextView with a compound drawable, which would draw the same thing with far less lines of XML.. My question is "can I specify I want to handle an onClick event only on the drawable of the TextView and not on the TextView itself? I've seen some solutions which involves writing your own extension of TextView, but I'm only interested in being able to do it within the layout resource, if possible, otherwise I'll keep the following XML code:

<LinearLayout         xmlns:android="http://schemas.android.com/apk/res/android"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:gravity="center"         android:orientation="horizontal" >          <TextView             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:gravity="bottom"             android:paddingTop="10dp"             android:paddingLeft="10dp"             android:paddingRight="10dp"             android:text="@string/home_feedback_title"             android:textColor="@android:color/primary_text_dark"             android:textStyle="bold"              android:paddingBottom="4dp"/>           <ImageView             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:src="@drawable/action_feedback"              android:clickable="true"             android:onClick="onClickFeedback"             android:contentDescription="@string/action_feedback_description"/> </LinearLayout> 
like image 786
Gianni Costanzi Avatar asked May 19 '12 14:05

Gianni Costanzi


2 Answers

Its very simple. Lets say you have a drawable on left side of your TextView 'txtview'. Following will do the trick.

TextView txtview = (TextView) findViewById(R.id.txtview); txtview.setOnTouchListener(new OnTouchListener() {     @Override     public boolean onTouch(View v, MotionEvent event) {         if(event.getAction() == MotionEvent.ACTION_UP) {             if(event.getRawX() <= txtview.getTotalPaddingLeft()) {                 // your action for drawable click event               return true;             }         }         return true;     } }); 

If you want for right drawable change the if statement to:

if(event.getRawX() >= txtview.getRight() - txtview.getTotalPaddingRight()) 

Similarly, you can do it for all compound drawables.

txtview.getTotalPaddingTop(); txtview.getTotalPaddingBottom(); 

This method call returns all the padding on that side including any drawables. You can use this even for TextView, Button etc.

Click here for reference from android developer site.

like image 70
Vishnuvathsan Avatar answered Sep 20 '22 09:09

Vishnuvathsan


You can go either way. Using the compound drawable is faster though because it was intended to be an optimization. It uses less ram because you reduce 3 views into 1 and it's faster layout because you lose 1 depth.

If I were you I'd consider stepping back to see if both the text and the image intercepting the touch to do whatever action is possibly a good thing. In general having a larger touch region makes it easier to press. Some users may actually be inclined to touch the text instead of the image.

Lastly if you go that route of merging the 2 you might want to consider using a Button instead of a TextView. You can style the button to not have the rectangle around it. They call it a borderless button. It's nice because you get visual feedback that you clicked on a actionable item where as an ImageView or TextView normally aren't actionable.

How to Create Borderless Buttons in Android

like image 45
Jeremy Edwards Avatar answered Sep 20 '22 09:09

Jeremy Edwards