Android Studio menu designer using wrong SearchView

Viewed 27

I was having an error when trying to create a search interface in an Android App. The error was 'android.widget.SearchView cannot be cast to androidx.appcompat.widget.SearchView'. I tracked it down to the fact that the following code was failing to cast.

searchView = menu.findItem(R.id.app_bar_search).actionView as SearchView

The import I was using was

import androidx.appcompat.widget.SearchView

It turned out the item in the Android Studio Muenu Designer was using android.widget.SearchView, not androidx.appcompat.widget.SearchView.

So I changed that and it all worked.

My question is why was Android Studio using the wrong SearchView, the min API level is set to 17 so it uses comparability Libraries everywhere else. Does this seem like a bug or is there some setting I need to setup?

1 Answers

It not a wrong searchView. Its not a bug. It is because of Android library shifted appcompat to androidx.
Pleaset take a look at this link.
It automatically import androidx you can change it to appcompat. No need to setup.

Related