Monday, 18 January 2021

How to select and click on child views of Android ViewPager2 with Dpad/Game Controller

I am investigating Bluetooth Dpad and game controllers in my current Android Project.

I can successfully select and click on all my UI widets within my App.

However I have a ViewPager2 that holds multiple fragments and I am unable to select or click on any of the Fragments widgets.

My ViewPager2 is defined in XML as follows:-

<androidx.viewpager2.widget.ViewPager2
    android:id="@+id/dashboard_pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

<com.google.android.material.tabs.TabLayout
    android:id="@+id/dashboard_dots"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:padding="2dp"
    app:tabBackground="@drawable/dot_selector"
    app:tabGravity="center"
    app:tabIndicatorHeight="0dp" />

In my main activity I configure the ViewPager2 as follows:-

    viewPager = findViewById(R.id.dashboard_pager)

    val pagerAdapter = ScreenSlidePagerAdapter(this@DashboardActivity)
    viewPager.adapter = pagerAdapter

    val tabLayout = findViewById<TabLayout>(R.id.dashboard_dots)

    TabLayoutMediator(tabLayout, viewPager) { _, _ -> }.attach()

My adapter is :-

private inner class ScreenSlidePagerAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {
    override fun getItemCount(): Int = NUM_PAGES

    override fun createFragment(position: Int): Fragment {
        return when (position) {
            0 -> DashboardClockFragment()
            1 -> DashboardSpeedometerFragment()
            2 -> DashboardTimerFragment()
            else -> DashboardTimerFragment()
        }
    }
}

The DashboardClockFragment layout XML resembles this :-

<FrameLayout
    android:id="@+id/dashboard_clock_wrapped"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_gravity="center_vertical"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?attr/selectableItemBackground">

    <TextClock
        android:id="@+id/dashboard_clock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/border"
        android:fontFamily="@font/lcd_digital"
        android:format12Hour="kk:mm:ss"
        android:format24Hour="kk:mm:ss"
        android:gravity="center"
        android:padding="10dp"
        android:textColor="?android:attr/textColorPrimary"
        android:textSize="@dimen/speed_limit_text_size" />

    <TextView
        android:id="@+id/dashboard_clock_paused"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/border"
        android:fontFamily="@font/lcd_digital"
        android:gravity="center"
        android:padding="10dp"
        android:text="@string/start_time"
        android:textColor="?android:attr/textColorPrimary"
        android:textSize="@dimen/speed_limit_text_size"
        android:textStyle="bold"
        android:visibility="gone" />

</FrameLayout>

I am unable to select the TextClock or TextView within the Layout using a Dpad controller.

In touch mode I can click on the child views and everything works as desired.

What have I missed when configuring my layouts to react to Dpad and or Game Controllers?



from How to select and click on child views of Android ViewPager2 with Dpad/Game Controller

No comments:

Post a Comment