Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not changing focus when calling setVisibility(View.VISIBLE)

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

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

The XML layout of the LinearLayout I can calling setVisible on:

<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 that is calling the setVisibility:

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

Thanks!

UPDATE

Thanks to my friend below, the following code works. It basically makes all the descendants of the LinearLayout unable to gain focus, which means when made visible the screen does not scroll to them as they do not gain focus. Only way to date I have found to keep this from happening...

        ((ViewGroup) fieldView).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        fieldView.setVisibility(View.VISIBLE);
        ((ViewGroup) fieldView).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
like image 884
Stephen McCormick Avatar asked Oct 09 '15 03:10

Stephen McCormick


1 Answers

You might try the android:descendantFocusability with 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.
like image 84
Radek O Avatar answered Sep 29 '22 16:09

Radek O