harisk92
harisk92

Reputation: 1118

Leanback focus problems

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 ?

Upvotes: 3

Views: 3372

Answers (2)

harisk92
harisk92

Reputation: 1118

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.

Upvotes: 5

dan
dan

Reputation: 13262

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.

Upvotes: 2

Related Questions