-
-
Notifications
You must be signed in to change notification settings - Fork 527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Search Bar looks broken when used along with a custom header title in Android #1858
Comments
@kkafar hi. I created this patch, but not sure this is the correct approach–I'm quite inexperienced on the native side. This solves the title and search bar being rendered on top of each other. But, in a real device, the search icon ends up aligned incorrectly to the left, if the search bar is blurred. On the emulator, everything works as expected. diff --git a/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/CustomSearchView.kt b/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/CustomSearchView.kt
index 6984b10..17e23f0 100644
--- a/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/CustomSearchView.kt
+++ b/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/CustomSearchView.kt
@@ -4,6 +4,7 @@ import android.content.Context
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.widget.SearchView
import androidx.fragment.app.Fragment
+import kotlin.collections.ArrayList
class CustomSearchView(context: Context, fragment: Fragment) : SearchView(context) {
/*
@@ -13,8 +14,9 @@ class CustomSearchView(context: Context, fragment: Fragment) : SearchView(contex
setOnSearchClickListener - https://developer.android.com/reference/android/widget/SearchView#setOnSearchClickListener(android.view.View.OnClickListener)
setOnCloseListener - https://developer.android.com/reference/android/widget/SearchView#setOnCloseListener(android.widget.SearchView.OnCloseListener)
*/
- private var mCustomOnCloseListener: OnCloseListener? = null
- private var mCustomOnSearchClickedListener: OnClickListener? = null
+
+ private var mCustomOnCloseListeners: MutableList<OnCloseListener> = ArrayList()
+ private var mCustomOnSearchClickedListeners: MutableList<OnClickListener> = ArrayList()
private var mOnBackPressedCallback: OnBackPressedCallback =
object : OnBackPressedCallback(true) {
@@ -40,12 +42,12 @@ class CustomSearchView(context: Context, fragment: Fragment) : SearchView(contex
fun setText(text: String) = setQuery(text, false)
- override fun setOnCloseListener(listener: OnCloseListener?) {
- mCustomOnCloseListener = listener
+ override fun setOnCloseListener(listener: OnCloseListener) {
+ mCustomOnCloseListeners.add(listener)
}
- override fun setOnSearchClickListener(listener: OnClickListener?) {
- mCustomOnSearchClickedListener = listener
+ override fun setOnSearchClickListener(listener: OnClickListener) {
+ mCustomOnSearchClickedListeners.add(listener)
}
override fun onAttachedToWindow() {
@@ -62,14 +64,14 @@ class CustomSearchView(context: Context, fragment: Fragment) : SearchView(contex
init {
super.setOnSearchClickListener { v ->
- mCustomOnSearchClickedListener?.onClick(v)
+ mCustomOnSearchClickedListeners.forEach { it.onClick(v) }
backPressOverrider.maybeAddBackCallback()
}
super.setOnCloseListener {
- val result = mCustomOnCloseListener?.onClose() ?: false
+ mCustomOnCloseListeners.forEach { it.onClose() }
backPressOverrider.removeBackCallbackIfAdded()
- result
+ false
}
maxWidth = Integer.MAX_VALUE
diff --git a/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/ScreenStackFragment.kt b/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/ScreenStackFragment.kt
index 86a7654..391ac4b 100644
--- a/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/ScreenStackFragment.kt
+++ b/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/ScreenStackFragment.kt
@@ -147,6 +147,25 @@ class ScreenStackFragment : ScreenFragment {
return false
}
+ private val subViewsToHideDuringSearch: MutableList<ScreenStackHeaderSubview>
+ get() {
+ val arr: MutableList<ScreenStackHeaderSubview> = ArrayList()
+
+ val config = screen.headerConfig
+ val numberOfSubViews = config?.configSubviewsCount ?: 0
+
+ if (config != null && numberOfSubViews > 0) {
+ for (i in 0 until numberOfSubViews) {
+ val subView = config.getConfigSubview(i)
+ if (subView.type != ScreenStackHeaderSubview.Type.SEARCH_BAR) {
+ arr.add(subView)
+ }
+ }
+ }
+
+ return arr
+ }
+
private fun updateToolbarMenu(menu: Menu) {
menu.clear()
if (shouldShowSearchBar()) {
@@ -154,6 +173,13 @@ class ScreenStackFragment : ScreenFragment {
if (searchView == null && currentContext != null) {
val newSearchView = CustomSearchView(currentContext, this)
searchView = newSearchView
+ newSearchView.setOnSearchClickListener {
+ subViewsToHideDuringSearch.forEach { it.visibility = View.GONE }
+ }
+ newSearchView.setOnCloseListener {
+ subViewsToHideDuringSearch.forEach { it.visibility = View.VISIBLE }
+ false
+ }
onSearchViewCreate?.invoke(newSearchView)
}
menu.add("").apply { |
Exact problem i am facing, same with the headerRight icons, i cant seem to find a solid way, atm i got:
But this is causing a small flicker on open |
@tboba thanks for the #1903 which also addresses this issue. Changing the visibility of header elements, Instead of pushing them to the side, was my approach to the problem too. Unfortunately, with On these devices, the search icon is pushed significantly to the left after the blur whether a custom header element is used or not. Here are some screenshots of the problem. It is tested on Xiaomi Redmi: No custom element - Before focus: No custom element - After blur: |
Hi, I've tested it with the latest version and could not reproduce the issue. I'll try again using Xiaomi/Huawei device in upcoming days. In the meantime, can anyone confirm that the problem still persists? |
@alduzy can confirm that the issue still persists with 3.32.0 on a Huawei device. |
@ha3 Could you provide a new reproduction snack? I've just tried the last provided snack on Huawei, Redmi and Xiaomi device and wasn't able to reproduce the issue. Also, there's a |
Description
In Android, when search bar is enabled along with a custom header title, they are rendered on top of each other. The problem persists after search bar is focused. It works correctly if a string is provided for the
headerTitle
.Steps to reproduce
Snack or a link to a repository
https://snack.expo.dev/@hozdemir/groaning-apples
Screens version
3.23.0
React Native version
0.72.3
Platforms
Android
JavaScript runtime
Hermes
Workflow
React Native (without Expo)
Architecture
Paper (Old Architecture)
Build type
Debug mode
Device
Real device
Device model
Redmi Note 7
Acknowledgements
Yes
The text was updated successfully, but these errors were encountered: