Android-TV navigation component / custom menu doesn't get focus on app-start

68 views Asked by At

Following problem: In my Android-TV-App I am using a custom menu, without leanback.

In my activity_main.xml I added two FragmentContainerViews (1x main_container, 1x settings_container) with each has its own nav_graph:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linLayout_menu"
        android:layout_width="50dp"
        android:layout_height="0dp"
        android:paddingTop="32dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/btn_search"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:paddingStart="16dp"
            android:paddingEnd="16dp"
            android:background="@drawable/selector_menu"
            android:drawableStart="@drawable/ic_search"
            android:drawablePadding="16dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:nextFocusDown="@id/btn_home"
            android:nextFocusUp="@id/btn_settings"
            android:gravity="center_vertical"
            android:text="@string/search"
            android:fontFamily="@font/vag_rounded_thin"
            android:lineSpacingExtra="1.5dp"
            android:letterSpacing="0.008"
            android:textStyle="bold"
            android:textColor="@color/white" />

        <TextView
            android:id="@+id/btn_home"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:paddingStart="16dp"
            android:paddingEnd="16dp"
            android:layout_marginTop="32dp"
            android:background="@drawable/selector_menu_strokeleft"
            android:drawableStart="@drawable/ic_playlist_manager"
            android:drawableTint="@color/white"
            android:drawablePadding="16dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:nextFocusUp="@id/btn_search"
            android:nextFocusDown="@id/btn_tv"
            android:gravity="center_vertical"
            android:text="@string/home"
            android:fontFamily="@font/vag_rounded_thin"
            android:lineSpacingExtra="1.5dp"
            android:letterSpacing="0.008"
            android:textStyle="bold"
            android:textColor="@color/white"
            app:focusOutEnd="true"
            app:focusOutFront="true"/>

        <TextView
            android:id="@+id/btn_tv"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:paddingStart="16dp"
            android:paddingEnd="16dp"
            android:background="@drawable/selector_menu_strokeleft"
            android:drawableStart="@drawable/ic_live_tv"
            android:drawableTint="@color/white"
            android:drawablePadding="16dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:nextFocusUp="@id/btn_home"
            android:nextFocusDown="@id/btn_movies"
            android:gravity="center_vertical"
            android:text="@string/tv"
            android:fontFamily="@font/vag_rounded_thin"
            android:lineSpacingExtra="1.5dp"
            android:letterSpacing="0.008"
            android:textStyle="bold"
            android:textColor="@color/white" />

        <TextView
            android:id="@+id/btn_movies"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:paddingStart="16dp"
            android:paddingEnd="16dp"
            android:background="@drawable/selector_menu_strokeleft"
            android:drawableTint="@color/white"
            android:drawableStart="@drawable/ic_movies"
            android:drawablePadding="16dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:nextFocusUp="@id/btn_tv"
            android:nextFocusDown="@id/btn_series"
            android:gravity="center_vertical"
            android:text="@string/movies"
            android:fontFamily="@font/vag_rounded_thin"
            android:lineSpacingExtra="1.5dp"
            android:letterSpacing="0.008"
            android:textStyle="bold"
            android:textColor="@color/white" />


        <TextView
            android:id="@+id/btn_series"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:paddingStart="16dp"
            android:paddingEnd="16dp"
            android:background="@drawable/selector_menu_strokeleft"
            android:drawableTint="@color/white"
            android:drawableStart="@drawable/ic_series"
            android:drawablePadding="16dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:nextFocusUp="@id/btn_movies"
            android:nextFocusDown="@id/btn_settings"
            android:gravity="center_vertical"
            android:text="@string/series"
            android:fontFamily="@font/vag_rounded_thin"
            android:lineSpacingExtra="1.5dp"
            android:letterSpacing="0.008"
            android:textStyle="bold"
            android:textColor="@color/white" />

        <TextView
            android:id="@+id/btn_settings"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:paddingStart="16dp"
            android:paddingEnd="16dp"
            android:layout_marginTop="32dp"
            android:background="@drawable/selector_menu_strokeleft"
            android:drawableStart="@drawable/ic_settings"
            android:drawablePadding="16dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:nextFocusUp="@id/btn_series"
            android:nextFocusDown="@id/btn_search"
            android:gravity="center_vertical"
            android:text="@string/settings"
            android:fontFamily="@font/vag_rounded_thin"
            android:lineSpacingExtra="1.5dp"
            android:letterSpacing="0.008"
            android:textStyle="bold"
            android:textColor="@color/white" />

    </LinearLayout>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/linLayout_menu"
        app:layout_constraintTop_toTopOf="parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/player_nav_graph"/>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/settings_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:visibility="gone"
        app:defaultNavHost="false"
        app:navGraph="@navigation/settings_nav_graph"/>

</androidx.constraintlayout.widget.ConstraintLayout>

player_nav_graph:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             android:id="@+id/player_nav_graph"
            app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.example.mj_player_tv.ui.HomeFragment"
        android:label="HomeFragment" />
    
    <fragment
        android:id="@+id/moviesFragment"
        android:name="com.example.mj_player_tv.ui.MoviesFragment"
        android:label="MoviesFragment" />
    
    <include app:graph="@navigation/settings_nav_graph" />
</navigation>

settings_nav_graph:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/settings_nav_graph"
    app:startDestination="@id/settingsFragment">

    <fragment
        android:id="@+id/settingsFragment"
        android:name="com.example.mj_player_tv.ui.SettingsFragment"
        android:label="SettingsFragment" >
        <action
            android:id="@+id/action_settingsFragment_to_playlistsFragment"
            app:destination="@id/playlistsFragment" />
    </fragment>
    <fragment
        android:id="@+id/playlistsFragment"
        android:name="com.example.mj_player_tv.ui.PlaylistsFragment"
        android:label="PlaylistsFragment" >
        <action
            android:id="@+id/action_playlistsFragment_to_addPlaylistFragment"
            app:destination="@id/addPlaylistFragment" />
    </fragment>
    <fragment
        android:id="@+id/addPlaylistFragment"
        android:name="com.example.mj_player_tv.ui.AddPlaylistFragment"
        android:label="AddPlaylistFragment" />
    <fragment
        android:id="@+id/playlistSettingsFragment"
        android:name="com.example.mj_player_tv.ui.PlaylistSettingsFragment"
        android:label="PlaylistSettingsFragment" >
        <action
            android:id="@+id/action_playlistSettingsFragment_to_addPlaylistFragment"
            app:destination="@id/addPlaylistFragment" />
    </fragment>
</navigation>

Now the problem is, that if I open the app the focused view is a view inside the settings_container (which actually isn't visibily), instead of the btn_home Textview, which i set in the MainActivity in onCreate with: binding.btn_home.requestFocus(). And i really can't find out why this happens and how this happen. Can someone here tell me where the error is? Maybe using two FragmentContainerViews and/or two nav-graphs? But why does a view that theoretically isn't visibile get the focus?

And another thing, when I change the FragmentContainerView to a Framelayout, everything works fine, except that I can't use (logically) the nav-graph.

So where should I try to start to fix the problem? Or would there be a simplier way to implement my settings-part, using the navigation component? (the settings part should only be displayed on the right part of the screen, with the rest of the screen been visible in the background)

0

There are 0 answers