3

Hello guys I'm working on application and my layout structure is as following :

  RelativeLayout:

   CompoundView: 

   CompoundView:
          RelativeLayout:
            Button
            Button
            RecyclerView

   BrowseFragment:
          Only rows

My problem is when I get to first row of browse fragment and first item in it and I want to go up (D-PAD-UP) to focus button it does nothing it works only when I push left ( D-PAD-LEFT). Anyone has solution for this ?

harisk92
  • 1,088
  • 1
  • 14
  • 24
  • You should provide the content of your layout xml file, since you are using the `RelativeLayout`. This will give us a clue of the order of the components. – dan Aug 11 '17 at 12:05
  • @dan I updated structure I don't use anything except rows inside BrowseFragment – harisk92 Aug 11 '17 at 12:25
  • `RelativeLayout` provides attributes like: `android:layout_alignParentTop` or `android:layout_toRightOf`, etc. See [here](https://developer.android.com/guide/topics/ui/layout/relative.html) for a complete list. That is why it is important to see what was the alignment used. – dan Aug 11 '17 at 12:29
  • You are right RelativeLayout is causing the the trouble.When I set centerInParent on button it doesn't get any kind of focus.Is there way to avoid this ? – harisk92 Aug 11 '17 at 13:40

2 Answers2

4

So the problem was in BrowseFrameLayout for some reason and to solve this issue I had to override onFocusSearchListener and manage focus myself.

In BrowseFragment which I extended I have this method :

public void workaroundFocus(){
    if(getView() != null) {
        View viewToFocus  = getActivity().findViewById(R.id.view_to_focus);
        BrowseFrameLayout browseFrameLayout = getView().findViewById(android.support.v17.leanback.R.id.browse_frame);
        browseFrameLayout.setOnFocusSearchListener((focused, direction) -> {
            if (direction == View.FOCUS_UP) {
                return viewToFocus;
            }
            else {
                return null;
            }
        });
    }
}

And then:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    workaroundFocus();
    /*
      Rest of the code
    */
}

And voila it works.

harisk92
  • 1,088
  • 1
  • 14
  • 24
2

Since you are using the RelativeLayout you should layout the components in the order that you would like to be navigated.

Using the XML attributes presented in the RelativeLayout reference document, you will be able to establish an navigation order too:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/reminder" />
    <Spinner
        android:id="@+id/dates"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/times" />
    <Spinner
        android:id="@id/times"
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_alignParentRight="true" />
    <Button
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/times"
        android:layout_alignParentRight="true"
        android:text="@string/done" />
</RelativeLayout>

Here the dates Spinner is below the name EditText and at left of times Spinner, the times component is below the name one and the done Button is below times. See the image below:

enter image description here

See the Build Basic TV Layouts guide for more tv related details.

dan
  • 13,132
  • 3
  • 38
  • 49
  • hi i am facing a similar issue where i am using a RowsSupportFragment, here the header of a row gets the focus instead of the row item – Subhasmith Thapa Jul 08 '21 at 11:44
  • @SubhasmithThapa You should open a separate, new question, with your code details, that way you will get a proper answer. – dan Jul 09 '21 at 08:36
  • i have opened a separate question but just wanted to try my luck. I solved my issue with this answer. Please check. Hope it helps https://stackoverflow.com/questions/68183099/how-to-focus-on-the-listrow-item-which-is-not-focusable-on-firestick-3rd-generat – Subhasmith Thapa Jul 09 '21 at 09:04