1

I just wanted to know that can we get two different onclick event on setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom) on textview. first onclick on textview and other on drawable or can i get onclick on just drawable not the textview.?? Any help will be appreciated. Thank You.

Praveen Sharma
  • 4,326
  • 5
  • 25
  • 45
gb17
  • 43
  • 1
  • 9

4 Answers4

5

I achieve it by listening to click event by myself. Like below:

    lTextView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP && event.getX() < dpTopx(MainActivity.this, 19) )
            {
                Log.v("Click", "Clicked on back button");
            }
            return true;
        }
    });

where dpToPx is just a function converting dp to pixel. So now the left 19-dp region (containing a left drawable) is clickable.

Yingpei Zeng
  • 515
  • 8
  • 10
2

No, you can't. The compound object is ONE. Even if you decorate it with a drawable.
If you want two separate click events, then you have to use two different objects (i.e.: an ImageView and a TextView).
But it's NOT a best practice.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
1

event are getting only resource Id. so object have only one Id. so only one onClick event can be listen. you can achieve this by wrapping this textView in a parent Layout

Praveen Sharma
  • 4,326
  • 5
  • 25
  • 45
  • 2
    I will try to put a text-view and an image-view together in linear-layout so i can two different on-click events – gb17 Jan 03 '14 at 11:47
  • yes then you can get one event of your textview and another one is for imageview and you can get one event for linearLayout. – Praveen Sharma Jan 03 '14 at 12:22
0

I prefer Layout with image view and textview or edit view

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
         android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/black_border"
        android:orientation="horizontal"
        android:weightSum="3">
 <ImageView
        android:id="@+id/rdv_location_icon"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_gravity="center"
        android:background="@android:color/white"
        android:src="@drawable/icon_location_et"
        android:layout_height="wrap_content"
        />

<TextView
        android:id="@+id/textView4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2.5"
        android:gravity="center"
        android:lines="3"
        android:text="Address"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>
Swap-IOS-Android
  • 4,363
  • 6
  • 49
  • 77