How to make editable text not editable, but clickable in JAVA

Is it possible to make EditText accessible but not editable.

I do not want it to be edited (also the keyboard did not fit, and we could not change the tooltip)

Actually, I want to use Edit Text simply as an image with a tooltip (which cannot be changed). I know that the actual way was to use ImageView and one TextView , but I want it to try with EditText , because instead I will use only one view instead of 2. Also, every thing is dynamic, so no XML.

For the above requirement, the solution in XML is android:editable="false" , but I want to use it in Java.

But,

If I use: -

 et.setEnabled(false); 

or

  et.setKeyListener(null); 

This makes EditText not EDITABLE, but at the same time makes it not clickable.

+45
android android-layout android-edittext
Dec 30 '12 at 12:31
source share
7 answers

The trick here is: -

 et.setFocusable(false); et.setClickable(true); 
+96
Dec 30 '12 at 13:30
source share

you can set EditText using java code as a parry: -

 edittext.setFocusable(false); edittext.setClickable(true); 

or using the below code in the XML file for EditText .

 android:editable="false" android:inputType="none" 
+16
Apr 12 '13 at 5:10
source share

You can use this.

 android:focusableInTouchMode="false" 

so that edittext is not edited in xml. And for an interactive event, you can use the setOnTouchListener () event to do the same thing as ...

 editText.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub //do your stuff here.. return false; } }); 
+6
Dec 30 '12 at 14:17
source share

android:editable deprecated, but you can do stg as below:

 android:editable="false" android:focusable="false" android:focusableInTouchMode="false" 

I used the above code for edittexts, which I use to select pickers. You can use input layout with this.

+2
Apr 18 '16 at 11:18
source share

For me, none of the answers led to a fully working solution.

Showcase of my fix https://www.youtube.com/watch?v=oUA4Cuxfdqc

Guide (noob friendly) , pay attention to a few comments in the code.

Create a Java class for a custom EditText called EditTextDispatched.java

 import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.RelativeLayout; /** * @author Martin Pfeffer * @see <a href="https://celox.io">https://celox.io</a> */ public class EditTextDispatched extends android.support.v7.widget.AppCompatEditText { private static final String TAG = "DispatchingEditText"; private boolean editable = true; public EditTextDispatched(Context context) { super(context); } public EditTextDispatched(Context context, AttributeSet attrs) { super(context, attrs); } public EditTextDispatched(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } // get the current state public boolean isEditable() { return editable; } // set your desired behaviour public void setEditable(boolean editable) { this.editable = editable; } @Override public boolean dispatchTouchEvent(MotionEvent motionEvent) { if (editable) { // default behaviour of an EditText super.dispatchTouchEvent(motionEvent); return true; } // achieve the click-behaviour of a TextView (it cuztom) ((RelativeLayout) getParent()).onTouchEvent(motionEvent); return true; } } 

Link EditTextDispatched to your xml (you will need to change the name pgk):

  <io.celox.brutus.EditTextDispatched android:id="@+id/row_field_value" style="@style/row_field_text_display" android:layout_alignParentBottom="true" android:text="@string/sample_field_value" /> 

To call:

 private void changeEditTextBehaviour(EditTextDispatched value, boolean editable) { value.setEditable(editable); value.setEnabled(editable); } 
0
Mar 26 '17 at 10:52 on
source share
 edittext enable and disable for the edit like this EditText edit = (EditText) findviewby(R.id.edittext); through java edit.setEnabled(false); //non editable edit.setEnabled(true); //editable through xml <EditText android:id="@+id/otp_mpin" android:layout_width="fill_parent" android:layout_height="wrap_content" android:editable="false" android:focusable="false"/> 
0
Jun 20 '17 at 10:55 on
source share

you can also do android:inputMethod="@null" in your EditText and it will not display a cursor or keyboard, and then you can easily capture a click on the listener settings.

-one
Sep 22 '16 at 7:20
source share



All Articles