Ad

How To Create A Clickable Edittext?

I know a there is a lot of similar questions out there, but none of them work for me. I want to make a EditText that acts like a Button. I want it to be not editable (and no keyboard should popup). However, when I set an onClickListener(), the EditText doesn't react to my onClick() event. How should I do this correctly?

In my xml I disabled the focusableInTouchMode and I set the clickable to be true. My resulting EditText is not editable, but it doesn't response to my onClickListener().

My xml of the EditText:

<EditText
            android:id="@+id/taskviewer_date"
            android:focusableInTouchMode="false"
            android:clickable="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="normal"
            android:textColorHint="@color/colorShadow"
            android:inputType="none"
            android:hint="No Due Date"/>

My Code:

EditText text = (EditText) findViewById(R.id.taskviewer_date);
text.setOnClickListener(
    new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("taskviewer", "OnClickListener called !");
        }
    }
);
Ad

Answer

Use onTouch events or put a clickable FrameLayout over your EditText to catch click events. Make FrameLayout's visibility Gone when you want your edittext to be editable

Ad
source: stackoverflow.com
Ad