Without changing focus when calling setVisibility (View.VISIBLE)

I have a scenario where, when an action occurs, I want certain fields to be visible. This works fine, however, when I call setVisible (View.VISIBLE) on a LinearLayout that contains several other fields, like TextEdit, the focus goes to TextEdit (or at least the screen scrolls to it).

Is there a way to not change focus when calling setVisibility (View.VISIBLE)?

LinearLayout XML Layout I can call setVisible in:

<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="true" android:focusableInTouchMode="true"> <TextView android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="18sp" android:textStyle="normal" android:paddingLeft="5dp" android:paddingRight="5dp" android:paddingBottom="5dp" android:text="" /> <EditText android:id="@+id/entry" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textMultiLine" android:gravity="right" /> <View android:layout_marginTop="8dp" android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/grey" android:visibility="gone" /> </LinearLayout> 

The code that initializes it:

 private TextView mName; private EditText mEntry; 

...

  private void initialize() { LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.field_multiline_entry, this); // setup fields mName = (TextView)findViewById(R.id.name); mEntry = (EditText)findViewById(R.id.entry); mEntry.addTextChangedListener(textWatcher); } 

And the code calling setVisibility:

  if(sectionFieldView != null && sectionFieldView.getVisibility() != View.VISIBLE) { sectionFieldView.setVisibility(View.VISIBLE); } 

Thanks!

UPDATE

Thanks to my friend below, the following code works. Basically, this leads to the fact that all LinearLayout descendants cannot get focus, which means that when visible, the screen does not scroll to them, because they do not receive focus. Only for today I have found that this does not happen ...

  ((ViewGroup) fieldView).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); fieldView.setVisibility(View.VISIBLE); ((ViewGroup) fieldView).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); 
+6
source share
1 answer

Can you try android:descendantFocusability with the beforeDescendants parameter on your LinearLayout?

 Constant Value Description beforeDescendants 0 The ViewGroup will get focus before any of its descendants. afterDescendants 1 The ViewGroup will get focus only if none of its descendants want it. blocksDescendants 2 The ViewGroup will block its descendants from receiving focus. 
+4
source

All Articles