Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove focus without setting focus to another control?

I like my UIs to be intuitive; each screen should naturally and unobtrusively guide the user on to the next step in the app. Barring that, I strive to make things as confusing and confounding as possible.

Just kidding :-)

I've got three TableRows, each containing a read-only and non-focusable EditText control and then a button to its right. Each button starts the same activity but with a different argument. The user makes a selection there and the sub-activity finishes, populating the appropriate EditText with the user's selection.

It's the classic cascading values mechanism; each selection narrows the available options for the next selection, etc. Thus I'm disabling both controls on each of the next rows until the EditText on the current row contains a value.

I need to do one of two things, in this order of preference:

  1. When a button is clicked, immediately remove focus without setting focus to a different button
  2. Set focus to the first button when the activity starts

The problem manifests after the sub-activity returns; the button that was clicked retains focus.

Re: #1 above - There doesn't appear to be a removeFocus() method, or something similar

Re: #2 above - I can use requestFocus() to set focus to the button on the next row, and that works after the sub-activity returns, but for some reason it doesn't work in the parent activity's onCreate().

I need UI consistency in either direction--either no buttons have focus after the sub-activity finishes or each button receives focus depending on its place in the logic flow, including the very first (and only) active button prior to any selection.

like image 887
InteXX Avatar asked May 24 '11 23:05

InteXX


People also ask

How do I remove focus in editText?

Remove focus but remain focusable: editText. setFocusableInTouchMode(false); editText. setFocusable(false); editText.

What is Focusableintouchmode?

Focusable in touch mode is a property that you can set yourself either from code or XML. However, it should be used sparingly and only in very specific situations as it breaks consistency with Android normal behavior.


1 Answers

Using clearFocus() didn't seem to be working for me either as you found (saw in comments to another answer), but what worked for me in the end was adding:

<LinearLayout      android:id="@+id/my_layout"      android:focusable="true"      android:focusableInTouchMode="true" ...> 

to my very top level Layout View (a linear layout). To remove focus from all Buttons/EditTexts etc, you can then just do

LinearLayout myLayout = (LinearLayout) activity.findViewById(R.id.my_layout); myLayout.requestFocus(); 

Requesting focus did nothing unless I set the view to be focusable.

like image 125
actionshrimp Avatar answered Sep 17 '22 13:09

actionshrimp