6

I want to make an Editext only focus when I programmably tell it to and not allow the user to press on the one he wants. Is this possible?

I tried

android:longClickable="false"
android:clickable="false"

But neither worked.

For some reason people are thinking the following solves my problem, but that person is trying to make an edit text not editable, where I am trying to make an edit text only not focusable by clicking

EditText not editable

Community
  • 1
  • 1
Adam Katz
  • 6,999
  • 11
  • 42
  • 74

9 Answers9

7

Try with

    android:focusable="false"
    android:focusableInTouchMode="false"
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
5

I ran into the same issue and solved it by using:

.setEnabled(false) and .setClickable(false)

so use this for your code:

.setEnabled(false);
.setClickable(false);
jmckie
  • 241
  • 4
  • 11
  • 2
    it still stealing clicks from the parent so it is still technically clickable even when it is not doing anything – bio007 Jan 29 '20 at 13:47
1

Not sure if you ever got this answered, but I had to do the same thing and although this may not be concise, I know it works:

First set the keyListener to null:

myEditText.setKeyListener(null);

Next set an OnTouch listener to the EditText to do whatever action you want:

myEditText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(motionEvent.getAction() == MotionEvent.ACTION_UP){
                    //Do whatever action you need to do here. 
                }
                return false;
            }
});

From there, you can simply insert your actions into the OnTouch listener ACTION_UP action tag. In my case, I needed to popup a Dialog with a listview for them to choose something and then set the respective text into the EditText.

Hope that helps.

PGMacDesign
  • 6,092
  • 8
  • 41
  • 78
1

PROGRAMATICALLY

I ran into the same issue and solved it by using:

.setEnabled(false), .setClickable(false) and .setFocuseable(false)

so use this for your code:

et.setEnabled(false);
et.setClickable(false);
et.setFocuseable(false);

USING XML

android:focusable="false"
Alex Cuadrón
  • 638
  • 12
  • 19
0

Try with this code

EditText et = (EditText) findViewById(R.id.your_edit_text);
et.setFocusable(false);
et.setClickable(true);
Navas pk
  • 331
  • 3
  • 17
0

Immediate parent of the EditText should steal the focus.For example:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="beforeDescendants"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="enter.." />
</LinearLayout>
AAP
  • 179
  • 1
  • 3
0
editText.isEnabled = false // makes it`s color grey
editText.setTextColor(Color.parseColor("#DE000000")) //change color if needed
Noldor
  • 1
  • 2
0

may be you need to use this line

        android:inputType="none"

in your xml this line will prevent keyboard from showing

Noah Mohamed
  • 114
  • 1
  • 1
  • 8
-1

In the xml file try using the following code-

    android:clickable="false"
    android:focusableInTouchMode="true"

use it on the EditText. I haven't tried it but hope it works.

AshAR
  • 228
  • 1
  • 14