How to make edit text not editable but clickable in JAVA

AndroidAndroid LayoutAndroid Edittext

Android Problem Overview


Is it possible to make EditText clickable but not editable.

I don't want it to be editable (Neither should the keyboard come up nor should we be able to change the hint)

Actually I want to use Edit text simply as an image with a hint (which cannot be changed). I know the actual way was to use an ImageView and one TextView, but I want it to try with EditText because I'll then be using 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 this in Java.

But,

If I use :-

et.setEnabled(false);

or

 et.setKeyListener(null);

It makes the EditText not EDITABLE but at the same time it makes it non clickable as well.

Android Solutions


Solution 1 - Android

The trick here is :-

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

Solution 2 - Android

As editable is deprecated. you can use android:inputType as none in designing xml or as InputType.TYPE_NULL in coding.

> Designing XML

<android.support.v7.widget.AppCompatEditText
    android:id="@+id/edt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="false"
    android:inputType="none"
    android:text="EditText"/>

>Coding

edt.setClickable(true);
edt.setFocusable(false);
edt.setInputType(InputType.TYPE_NULL);

And below is the click event of edittext

edt.setOnClickListener(new View.OnClickListener() {
	@Override
	public void onClick(View v) {
		Toast.makeText(YourActivity.this,"edt click", Toast.LENGTH_LONG).show();
	}
});

Solution 3 - Android

you can set EditText by using java code as fallowing :-

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

or by using below code in XML file for EditText.

android:editable="false" 
android:inputType="none"  

Solution 4 - Android

You can use this..

android:focusableInTouchMode="false"

to make edittext not editable in xml.And for clickable event you can use setOnTouchListener() event to do the same thing as like..

editText.setOnTouchListener(new OnTouchListener() {
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
                        //do your stuff here..
		return false;
	}
});

Solution 5 - Android

Edittext enable and disable for the edit like this

EditText edit = (EditText) findviewby(R.id.edittext);

>Through java code

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"/>

Solution 6 - Android

android:editable is deprecated but you can do stg like below:

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

I used the cade above for edittexts which i use pickers to set data in. You can use input layout with this.

Solution 7 - Android

As of 2020, if you also care about accessibility, you should avoid setting et.setFocusable(false); since that will make the EditText not reachable using Keyboard navigation.

Here is my solution:

Kotlin

editText.isFocusableInTouchMode = false

editText.isLongClickable = false //this will prevent copy and paste

editText.setOnClickListener {
   // handle the click
   // this will automatically make the edittext clickable = true
}

Solution 8 - Android

For me none of the answers resulted in a completely working solution.

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

Guide (noob-friendly), please note the 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's cuztom)
        ((RelativeLayout) getParent()).onTouchEvent(motionEvent);
        return true;
    }

}

Reference the EditTextDispatched in your xml (you will have to alter the pgk-name):

  <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 invoke:

private void changeEditTextBehaviour(EditTextDispatched value, boolean editable) {
    value.setEditable(editable);
    value.setEnabled(editable);
}

Solution 9 - Android

Maybe you can try

editText.setEnabled(false);

Solution 10 - Android

I know this question has already been answered but to give a more current (i.e. 2018) answer here is the setup I use to achieve the requested (i.e. non-input or editable) effect:

    val etUserLoc: EditText = themedEditText(R.style.Pale_Blue_Text_Bold_13) {
                id = R.id.et_comp_user_keys_var_user_location
                inputType = InputType.TYPE_NULL
                isClickable = true
                isFocusable = false
                textColor = resources.getColor(R.color.magenta)  // overrides the default theme color
                text = Editable.Factory.getInstance().newEditable(resources.getString(R.string.loc_swakopmund))
            }.lparams(width = matchConstraint, height = matchParent) {   }

Yes, this is ANKO since I no longer use XML in any of my Android Projects. The 'inputType' takes care of the 'isEditable' deprecation.

Solution 11 - Android

you can also do android:inputMethod="@null" in your EditText and it will not show the cursor or the keyboard and then you can easily grasp the click through setting listener.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAamir ShahView Question on Stackoverflow
Solution 1 - AndroidAamir ShahView Answer on Stackoverflow
Solution 2 - AndroidNirav BhavsarView Answer on Stackoverflow
Solution 3 - AndroidabhiView Answer on Stackoverflow
Solution 4 - AndroidridoyView Answer on Stackoverflow
Solution 5 - AndroidKishore ReddyView Answer on Stackoverflow
Solution 6 - AndroidsavepopulationView Answer on Stackoverflow
Solution 7 - AndroidBennyView Answer on Stackoverflow
Solution 8 - AndroidMartin PfefferView Answer on Stackoverflow
Solution 9 - Android默默磊OKView Answer on Stackoverflow
Solution 10 - AndroidSilSurView Answer on Stackoverflow
Solution 11 - AndroidSaadurRehmanView Answer on Stackoverflow