Skip to content Skip to sidebar Skip to footer

Click Event On Edittext's Drawableright Not Working Properly?

I need to open phone's contact book on the click of EditText's drawableRight. Click event on drawableRight is working fine But the problem is, when I click/touch on anywhere on Edi

Solution 1:

Instead of using getRawX(), try replacing that line with

if (event.getX() >= (mobile_number.getWidth() - mobile_number
        .getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {

EDIT: I believe View.getRight() returns the position of the right edge of the View relative to its parent, while TouchEvent.getRawX() returns the absolute X position on the screen.

EDIT AGAIN TO DEMONSTRATE MY POINT:

MainActivity.xml

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.meremammal.www.edittextdrawable.MainActivity"><!-- This layout is only here to demonstrate a situation that breaks the usage of getRawX() --><RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_alignParentRight="true"><EditTextandroid:id="@+id/edit_text"android:layout_width="400dp"android:layout_height="wrap_content"android:text="Hello World!"android:drawableRight="@android:drawable/ic_input_add"/></RelativeLayout></RelativeLayout>

MainActivity.java

publicclassMainActivityextendsAppCompatActivity {

    EditText mEditText;
    Context mContext;

    @Override
    protectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = this;
        mEditText = (EditText) findViewById(R.id.edit_text);
        mEditText.setOnTouchListener(new View.OnTouchListener() {

            privatefloat touchX = 0;
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int drawableLeft = mEditText.getRight() - mEditText
                        .getCompoundDrawables()[2].getBounds().width();
                // This detects the location of touch on ACTION_DOWN, but because it is// using getRawX() and getRight() and the EditText's parent is not at the// left of the screen, it will respond when clicked in the middle of the// EditText. Instead, use getX() and EditText.getWidth()if (event.getAction() == MotionEvent.ACTION_DOWN && event.getRawX() >= drawableLeft) {
                    touchX = event.getRawX();
                    returntrue;
                } elseif (event.getAction() == MotionEvent.ACTION_UP && touchX >= drawableLeft) {
                    Toast.makeText(mContext, "Clicked Button",     Toast.LENGTH_SHORT).show();
                    touchX = 0;
                    returntrue;
                } else {
                    return mEditText.onTouchEvent(event);
                }
            }
        });
    }
}

Solution 2:

You don't have access to the right image as far my knowledge, unless you create custom EditText class. I suggest to use a RelativeLayout, with one editText and one imageView, and set OnClickListener over the image view as below:

<RelativeLayoutandroid:id="@+id/rlSearch"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:drawable/edit_text"android:padding="5dip" ><EditTextandroid:id="@+id/txtSearch"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_toLeftOf="@+id/imgSearch"android:background="#00000000"android:ems="10"/><ImageViewandroid:id="@+id/imgSearch"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:src="@drawable/btnsearch" /></RelativeLayout>

Solution 3:

Make a Linear layout with horizontal orientation and add a edit-text and image-view with proper weight..

Then Give on click for each item separately.....

Solution 4:

Just chage the last return to false. Also you have to remove the comma ; at the end of if statement.

Post a Comment for "Click Event On Edittext's Drawableright Not Working Properly?"