arimorty / floatingsearchview

A search view that implements a floating search bar also known as persistent search
https://github.com/arimorty/floatingsearchview/blob/master/README.md
Apache License 2.0
3.54k stars 668 forks source link

Does not update when changing themes #335

Closed clevertrevor closed 4 years ago

clevertrevor commented 4 years ago

I am changing my application to use a DayNight theme so it will follow the user's device preferences. However, the FloatingSearchView is the only View not changing themes and is locked to whatever theme the app started in. I've tried invaliding FloatingSearchView and setting a dark/light theme as one of its XML attributes.

Here's the XML declaration.

        <com.arlib.floatingsearchview.FloatingSearchView
            android:id="@+id/floating_search_view"
            android:theme="@style/AppTheme"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            android:translationZ="5dp"
            app:layout_constraintTop_toTopOf="@id/toolbar_container"
            app:floatingSearch_dismissFocusOnItemSelection="true"
            app:floatingSearch_leftActionMode="showSearch"
            app:floatingSearch_searchBarMarginLeft="8dp"
            app:floatingSearch_searchBarMarginRight="8dp"
            app:floatingSearch_searchSuggestionTextSize="14sp"
            app:floatingSearch_showSearchKey="true"
            app:floatingSearch_suggestionsListAnimDuration="250" />
clevertrevor commented 4 years ago

I figured out a hack to prevent the previous theme's colors from leaking into. the current theme. There will be adverse changes in regards to persistence of all the other attributes. Replace usages of FloatingSearchView with the DayNight version.

package com.arlib.floatingsearchview

import android.content.Context
import android.os.Parcelable
import android.util.AttributeSet

class FloatingSearchViewDayNight @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null)
    : FloatingSearchView(context, attrs) {

    // hack to remove color attributes which should not be reused 
    override fun onSaveInstanceState(): Parcelable? {
        super.onSaveInstanceState()
        return null
    }
}