0

I'm working on an Android application, I have some Textview which onClick show some statistics but I've an icon on that Textview also, I need to make that icons clickable also that shows some other statistics, Is there any way to do that so that one Textview has two onClickListeners() which behaves separately, like the image below text is clickable with textview.setOnClickListener() but need to make icon clickable also with other functionality.

enter image description here

Thanks in Advance

Manish Dubey
  • 4,206
  • 8
  • 36
  • 65

6 Answers6

0

You will want to move the drawable to a separate ImageView if it needds to do something else when clicked than the TextView.

Smokez
  • 382
  • 1
  • 5
0

There is not direct way to achieve it. You have to set setOnTouchListener on the TextView and

setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

Nest the textview and an Imageview in a horizonal LinearLayout. You can specify a background to it so that it looks as one single field. Using onClick method of each view you can display whichever statistics you want to.

Akanksha Hegde
  • 1,738
  • 11
  • 14
0

If you want to implement two click listeners for your textview as well as drawble item then you can go for custom view. See this SO thread for implementing clicklistener for drawable Item.

Community
  • 1
  • 1
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
0

Try below code:-

CustomEditText.java

public class CustomEditText extends EditText
{
  private Drawable dRight;
  private Rect rBounds;

  public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }
  public CustomEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
  public CustomEditText(Context context) {
    super(context);
  }

  @Override
  public void setCompoundDrawables(Drawable left, Drawable top,
      Drawable right, Drawable bottom)
  {
    if(right !=null)
    {
      dRight = right;
    }
    super.setCompoundDrawables(left, top, right, bottom);
  }

  @Override
  public boolean onTouchEvent(MotionEvent event)
  {

    if(event.getAction() == MotionEvent.ACTION_UP && dRight!=null)
    {
      rBounds = dRight.getBounds();
      final int x = (int)event.getX();
      final int y = (int)event.getY();
      //System.out.println("x:/y: "+x+"/"+y);
      //System.out.println("bounds: "+bounds.left+"/"+bounds.right+"/"+bounds.top+"/"+bounds.bottom);
      //check to make sure the touch event was within the bounds of the drawable
      if(x>=(this.getRight()-rBounds.width()) && x<=(this.getRight()-this.getPaddingRight())
          && y>=this.getPaddingTop() && y<=(this.getHeight()-this.getPaddingBottom()))
      {
        //System.out.println("touch");
        this.setText("");
        event.setAction(MotionEvent.ACTION_CANCEL);//use this to prevent the keyboard from coming up
      }
    }
    return super.onTouchEvent(event);
  }

  @Override
  protected void finalize() throws Throwable
  {
    dRight = null;
    rBounds = null;
    super.finalize();
  }
}

xml

<com.example.CustomEditText
    android:id="@+id/txtsearch"
    …
    android:layout_gravity="center_vertical"
    android:background="@layout/shape"
    android:hint="Enter place,city,state"
    android:drawableRight="@drawable/cross" 
/>

activity

CustomEditText et = (CustomEditText) this.findViewById(R.id.txtsearch);

see below link for more

Handling click events on a drawable within an EditText

Setting onClickListner for the Drawable right of an EditText

Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
0

from my project - example

<RelativeLayout
        android:id="@+id/beam_contact_entry_contact"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/beam_contact_entry_invite"
        android:background="?entry_background" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:orientation="vertical"
            android:layout_toRightOf="@+id/contact_info_avatar" >

            <TextView
                android:id="@+id/beam_contact_fragment_entry_text"
                style="?primary_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Alexander Great"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/beam_contact_fragment_entry_text_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="-4dp"
                android:text="mobile"
                android:visibility="gone"
                style="?secondary_text"
                android:textSize="15sp" />


        </LinearLayout>

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/beam_contact_entry_invite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/beam_contact_entry_contact"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/beam_contact_entry_contact"
        android:background="?entry_background"
        android:orientation="horizontal" >   

        <ImageView
            android:id="@+id/beam_contact_fragment_entry_right_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="?entry_background"
            android:duplicateParentState="true"
            android:src="@drawable/sv_svyaznoy_contact" />

    </LinearLayout>
xoxol_89
  • 1,242
  • 11
  • 17