77

I'm having hard time understanding descendantFocusability. Very particulary afterDescendants.

Could someone show me an example of when this would be useful?

StackOverflower
  • 5,463
  • 13
  • 58
  • 89

1 Answers1

117

Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.

Must be one of the following constant values.

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

You can check the complete example here.

The snippet is :

public void onItemSelected(AdapterView<?> parent, View view,
        int position, long id) {
    ListView listView = getListView();
    Log.d(TAG, "onItemSelected gave us " + view.toString());
    Button b = (Button) view.findViewById(R.id.button);
    EditText et = (EditText) view.findViewById(R.id.editor);
    if (b != null || et != null) {
        // Use afterDescendants to keep ListView from getting focus
        listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
        if(et!=null) et.requestFocus();
        else if(b!=null) b.requestFocus();
    } else {
        if (!listView.isFocused()) {
            // Use beforeDescendants so that previous selections don't re-take focus
            listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
            listView.requestFocus();
        }
    }

}

As per the above snippet, afterDescendants is used to prevent listview from getting focus, so that either EditText or Button can request focus.

Note: The link provided above is broken. Please refer to my Gist for the code

Pranav Karnik
  • 3,318
  • 3
  • 35
  • 47
  • Hey Pranav, I am still quite unsure of why it is really useful. Can you please give some real life example? – Suyash Chavan Jun 14 '19 at 12:16
  • 15
    @SuyashChavan say you have a layout full of editTexts. When your activity resumes the first editText takes focus and the softInputKeyboard pops up. If you don't want that to happens and you want the user to decide which view to fill in first, you add the attribute android:descendantFocusability="beforeDescendants" to the parent view and your problem is solved. – Nikos Hidalgo Jul 15 '19 at 15:07