getActionView is deprecated?

Viewed 18646

Today I decide to translate my android app from Java to Kotlin ! :) But I was very surprise when I type this :

val searchItem = menu.findItem(R.id.action_search)
val searchView = MenuItemCompat.getActionView(searchItem) as SearchView

And Android Studio told me : " 'getActionView(MenuItem!):View!' is deprecated. Deprecated in Java "

So before to ask you the solution I ask to Google what is the solution and I believed I find the solution : "Use getActionView() directly."

So I modified my code like this :

val searchView = MenuItemCompat.getActionView() as SearchView

But getActionView() is still crossed so I don't understand at all...

I will be very happy if you can help me :) Thank you !

4 Answers

Use actionView directly in Kotlin, like this:

  override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.actions, menu)
    val searchItem = menu?.findItem(R.id.action_search)
    val searchView = searchItem?.actionView as SearchView
    searchView.animate()
    // TODO: Configure the search info and add any event listeners...
    return super.onCreateOptionsMenu(menu)
}
Related