Wednesday, 2 February 2022

Customview inside viewpager in android not changing when nightmode is enabled

We are implementing the nightmode support for android with:

AppCompatDelegate.setDefaultNightMode(if(active) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO)

However it works on normal layout files but we are experiencing issues with our customview. We implemented an abstract view which inflates the right view according to the implementation.

class DrivingModeDashboardView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null
    ) : BaseDrivingModeView(context, R.layout.view_driving_mode_dashboard, attrs) {
        override val speedAdviceView: SpeedAdviceImageView
            get() = adviceSpeed
        override val currentSpeedView: MaxSpeedTextView
            get() = currentSpeed
        override val maxSpeedView: AnimatedMaxSpeedView
            get() = maxSpeed
        override val noLimitView: ImageView
            get() = noLimit
        override val shadowBarLayout: ShadowBarLayout?
            get() = shadowBar
        override val leftContainer: ViewGroup
            get() = drivingViewLeftContainer
        override val routeView: RouteView
            get() = drivingViewDashboardRouteView
    
        override var hideViews: Boolean
            get() = super.hideViews
            set(value) {}
    }

abstract class BaseDrivingModeView @JvmOverloads constructor(
    context: Context,
    @LayoutRes layout: Int,
    attrs: AttributeSet? = null
) : NightmodeRelativeLayout(context, attrs), (SpeedManager.Result) -> Unit {
 init {
        View.inflate(context, layout, this)
        setupView()
    }
}

Which is used in our dashboard fragment with layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dashboardNightmodeWhiteTint_to_fmblack">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/pullToRefreshFragmentDashboard"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/reportsTabLayout">

        <nl.example.views.SmileyView
            android:id="@+id/smileyView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    <nl.example.features.hud.views.custom.HUDHintView
        android:id="@+id/hintHUDDashboardView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/drivingModeDashboardView"
        android:layout_alignParentStart="true"
        android:layout_marginStart="16dp"
        android:layout_marginBottom="-12dp"
        android:visibility="gone" />

    <nl.example.views.DrivingModeDashboardView
        android:id="@+id/drivingModeDashboardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="-1dp" />
</RelativeLayout>

the viewpager implements the FragmentStateAdapter:

class ViewPagerAdapter(manager: FragmentActivity) : FragmentStateAdapter(manager) {

    private val fragmentList = ArrayList<Fragment>()
    private val fragmentTitleList = ArrayList<String>()

    fun addFragment(fragment: Fragment, title: String) {
        fragmentList.add(fragment)
        fragmentTitleList.add(title)
    }

    fun clearFragments() {
        fragmentList.clear()
        fragmentTitleList.clear()
    }

    override fun getItemCount(): Int = fragmentList.size

    override fun createFragment(position: Int): Fragment = fragmentList[position]
}

with:

viewPager.apply {
            offscreenPageLimit = 3
            adapter = viewPagerAdapter
            isUserInputEnabled = false
            registerOnPageChangeCallback(onPageChangeCallback)
        }

That dashboard fragment is put into a viewpager. It seems that none of the fragments inside that viewpager will toggle nightmode when it changes. Anyone got any idea's how to fix this?



from Customview inside viewpager in android not changing when nightmode is enabled

No comments:

Post a Comment