MichelReap
MichelReap

Reputation: 5770

Unfocusable element, focusable on touch

I have an editText that I want to fill in automatically with info from other controls in the form, and at the same time allow to change its contents, discouraging the user from doing so though.

So I set this control to not focusable so when you press actionNext it moves on to the next control. However if you click the edit text, I want to allow the user to change its contents.

This is what I did:

  mNameEditText.setFocusable(false);
        mNameEditText.setFocusableInTouchMode(false);
        mNameEditText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mNameEditText.setFocusableInTouchMode(true);
                mNameEditText.requestFocusFromTouch();
                mNameEditText.setFocusable(true);
            }
        });

However this behaves very weirdly, the result is that when you click, you can edit, but suddenly the next control (an AutoCompleteTextView) is not focusable anymore! Actually it looks like the focus remains on the edit text and goes to the autocompletetextview at the same time, like so:

enter image description here

How can I fix this?

Upvotes: 7

Views: 694

Answers (2)

Philippe Banwarth
Philippe Banwarth

Reputation: 17735

If you want the automatic focus change to skip some views, you can use a combination of the nextFocus* attributes.

Something like :

<EditText
    android:id="@+id/txt1"
    android:imeOptions="actionNext"
    android:nextFocusForward="@+id/txt3"
    android:nextFocusDown="@+id/txt3"
    ... />

<!-- skipped view -->
<EditText
    android:id="@+id/txt2"
    android:imeOptions="actionNext"
    ... />

<EditText
    android:id="@+id/txt3"
    android:imeOptions="actionNext"
    android:nextFocusUp="@id/txt1"
    ... />

nextFocusForward is the one used for actionNext. I believe the other attributes are mostly useful in non touch mode (e.g. with a hardware keyboard)

Upvotes: 0

Lars Blumberg
Lars Blumberg

Reputation: 21381

  • Make a sub class of your text edit view
  • Use it in your view
  • Set its enabled property to false
  • Overwrite the touch handlers within your subclass: when the text edit view is touched, enable the view now and focus it. When the focus is lost, disable the text edit view again. Do all that in the subclass.
  • Style the text edit view properly so that the user has the impression that it's editable

Upvotes: 1

Related Questions