Skip to content
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

SearchMode.OnSubmit not working with EditText #407

Open
Zineddine231 opened this issue Jun 6, 2024 · 1 comment
Open

SearchMode.OnSubmit not working with EditText #407

Zineddine231 opened this issue Jun 6, 2024 · 1 comment

Comments

@Zineddine231
Copy link

Zineddine231 commented Jun 6, 2024

Describe the bug 🐛
i'm using algolia search and instant search library in my project, The problem is that when i set the searchMode to AsYouType the search works perfectly fine and i get the expected results, but when i change it to onSubmit the search will not initiate.

To Reproduce 🔍
first i thought that the problem was in the search view from the material components, and since algolia doesn't support it i've changed it to a basic EditText. But even with edit text and it is the same problem.
my plan is to find out how to make it work with the editText and after that use searchView.editText, because the SearchView has a child EditText.

Here's how i declare the edit text in my layout XML file:
`
<EditText

    android:id="@+id/search"

    android:layout_width="match_parent"

    android:layout_height="@dimen/_42sdp"

    android:hint="edittext hint"

    android:imeOptions="actionSearch"

    android:maxLines="1"

    android:singleLine="true" />`

and this is the backend side:

`
private lateinit var connection: ConnectionHandler
private lateinit var recyclerView: RecyclerView
private lateinit var editText: EditText
private lateinit var searchBoxView: SearchBoxViewEditText

// Initialize the HitsSearcher
private val searcher = HitsSearcher(
    applicationID = ApplicationID("############"),
    apiKey = APIKey("######################"),
    indexName = IndexName("###"),
)
private var adapterKt = AdapterKt()

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val view = inflater.inflate(R.layout.fragment_search_kt, container, false)

    editText = view.findViewById(R.id.search)
    searchBoxView = SearchBoxViewEditText(editText)

    recyclerView = view.findViewById(R.id.recyclerView)
    val layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
    layoutManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS
    recyclerView.layoutManager = layoutManager
    recyclerView.adapter = adapterKt

    val searchBox = SearchBoxConnector(searcher, searchMode = SearchMode.OnSubmit)
    connection = ConnectionHandler(searchBox)
    connection += searchBox.connectView(searchBoxView)


    connection += searcher.connectHitsView(adapterKt) { response ->
        val hits = response.hits.deserialize(PostModelKt.serializer())
        adapterKt.setHits(hits) as List<PostModelKt>
    }

    searcher.searchAsync()

    return view
}`

PS: When using the searchView from ViewAppCompat the search works perfectly fine with onSubmit search mode.

@Zineddine231 Zineddine231 changed the title SearchMode.OnSubmit not working SearchMode.OnSubmit not working with EditText/material_SearchView Jun 6, 2024
@Zineddine231 Zineddine231 changed the title SearchMode.OnSubmit not working with EditText/material_SearchView SearchMode.OnSubmit not working with EditText Jun 6, 2024
@Zineddine231
Copy link
Author

After some research in the instant search library and a comparision between the two functions: SearchBoxViewAppCompat and SearchBoxViewEditText it turned out that the EditText doesn't have a specific method to handle input submission unlike the SearchView from ViewAppCompat.

THE SOLUTION : is to set an EditorActionListener for the EditText:

editText.setOnEditorActionListener(TextView.OnEditorActionListener(
            fun(v: TextView, actionId: Int, event: android.view.KeyEvent?): Boolean {
                if(actionId == EditorInfo.IME_ACTION_SEARCH){
                    val input = searchView.text.toString()
                    searcher.setQuery(input)
                    searcher.searchAsync()
                    return true
                }
                return false
            }
        ))

PS: We still going to need to set the searchMode = SearchMode.OnSubmit , because if you don't the search will initiate as you type in the EditText

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant