For reference
This is a branch showing the issue (note the project is incomplete). Run it and you'll see the compile time error Unresolved reference: search_src_text
.
What I'm trying to do:
I want to submit text to my ViewModel from the SearchView. When the text is submitted I execute a query using that text.
What you might be thinking:
"Just use an
OnQueryTextListener
."
An OnQueryTextListener
will not submit text if the text is null/empty. I am using the null/empty case to search everything in the database. So that doesn't work.
The work-around I tried:
In the past I've manually searched for the id of the SearchAutoComplete
view inside the SearchView. You can find it like this:
val searchPlate: SearchView.SearchAutoComplete? = search_view.findViewById(R.id.search_src_text)
Then you just attach an OnEditorActionListener
and listen for the IME actions and you're good to go:
searchPlate.setOnEditorActionListener { v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED
|| actionId == EditorInfo.IME_ACTION_SEARCH ) {
val searchQuery = v.text.toString()
viewModel.setQuery(searchQuery)
viewmodel.executeSearch()
}
true
}
Usually this works fine. The only difference (that I can see) is this time I'm using a Dynamic Feature Module. When trying to access R.id.search_src_text
it throws this compile time error:
Unresolved reference: search_src_text
And yes, the layout is in the dynamic feature module /res/ directory.
The code
1. SearchView in layout
<androidx.constraintlayout.widget.ConstraintLayout
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/app_background_color"
>
<androidx.appcompat.widget.SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:iconifiedByDefault="false"
android:queryHint="@string/text_Search"
android:layout_centerHorizontal="true" />
</androidx.constraintlayout.widget.ConstraintLayout>
2. Get reference to search_src_text
val searchPlate: SearchView.SearchAutoComplete? = search_view.findViewById(R.id.search_src_text)
Running the project will yield the error Unresolved reference: search_src_text
.
Another weird thing:
I looped through the View hierarchy until I found the SearchAutoComplete, then attached the listener and it works just fine...
for((index1, child) in search_view.children.withIndex()){
printLogD("ListFragment", "${index1}, ${child}")
for((index2, child2) in (child as ViewGroup).children.withIndex()){
printLogD("ListFragment", "T2: ${index2}, ${child2}")
if(child2 is ViewGroup){
for((index3, child3) in (child2 as ViewGroup).children.withIndex()){
printLogD("ListFragment", "T3: ${index3}, ${child3}")
if(child3 is ViewGroup){
for((index4, child4) in (child3 as ViewGroup).children.withIndex()){
printLogD("ListFragment", "T4: ${index4}, ${child4}")
if(child4 is SearchView.SearchAutoComplete){
child4.setOnEditorActionListener { v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED
|| actionId == EditorInfo.IME_ACTION_SEARCH ) {
val searchQuery = v.text.toString()
printLogD("NoteList", "SearchView: (keyboard or arrow) executing search...: ${searchQuery}")
viewModel.setQuery(searchQuery).let{
viewModel.loadFirstPage()
}
}
true
}
break
}
}
}
}
}
}
}
Any insights would be greatly appreciated.