1

When a child view gains focus, I would like to receive onFocusChange(...) event for both the View and it's parent ViewGroup.

<LinearLayout
    ...
    android:focusable="true"
    android:descendantFocusability="beforeDescendants">

    <TextView
        ...
        android:focusable="true"/>

    <TextView
        ...
        android:focusable="true"/>

</LinearLayout>

However, making children focusable stops the parent from receiving focus. The parent's attributes seem to be ignored.

Is there any documentation on the compound behaviour of focusable views?

How can I receive focus event for the parent in addition to it's views?

Shreeya Chhatrala
  • 1,441
  • 18
  • 33
Marc
  • 145
  • 1
  • 3
  • 11

2 Answers2

0

Not sure if this is what you were looking for, but I don't think you can have multiple focus. The definition of focus is to focus on one thing. Read here.

Can two views both have focus in Android

Focus issue with multiple EditTexts

Bqin1
  • 467
  • 1
  • 9
  • 19
0

Since Android is one screen at a time only, the concept of focusing, as we know it with desktop apps is slightly different. You don't focus one window and take focus from another (there are other lifecycle events for that) If a parent would receive the focus event of it's child, then all hierarchical parents should receive it too, which as you understand is useless. Focusing is happening on one view at a time.

To achieve what you're after you can just forward the event to the view's parent when receiving it in your OnFocusChangeListener, but there's a risk that it might cause undesired behavior

Nick
  • 585
  • 4
  • 11
  • Then what is the purpose of descendantFocusability attribute? It specifically has 'beforeDescendants' value. Hierarchical parents that declare themselves as focusable should. I assume (I can't find documentation) that focus event goes down the tree, as setting 'blocksDescendants' stops the children from receiving the event and does give it to the parent. – Marc Aug 08 '17 at 15:30
  • if I remember correctly (otp) beforeDescendants usually takes another tap to focus a child inside a focusable parent, while blockDescendants should not allow it to focus at all (text inputs do focus anyway)ie, these flags are particularly useful when having inputs inside a listview. still this doesnt mean that two views can be focused at the same time. you should give more details about what exactly you're trying to do, I should be able to help you – Nick Aug 08 '17 at 16:23
  • Using keys / TV remote the focus simply enters a child view, bypassing the parent when I set 'beforeDescendants'. I do not want 2 views to have focus, I want to get onFocusChange event for the parent prior to it delegating to children, similar to how clickEvents can be consumed or passed further down the tree. – Marc Aug 10 '17 at 15:48