```override fun onCreateOptionsMenu(menu: Menu?): ...
# android
z
Copy code
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    val inflater = menuInflater
    super.onCreateOptionsMenu(menu)
    inflater.inflate(R.menu.main_menu, menu)

    val manager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
    val searchItem = menu?.findItem(R.id.search)
    val searchView = searchItem?.actionView as SearchView
    searchView.setSearchableInfo(manager.getSearchableInfo(componentName))
    searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
        override fun onQueryTextSubmit(query: String?): Boolean {
            searchView.clearFocus()
            searchView.setQuery("", false)
            searchItem.collapseActionView()
            Toast.makeText(this@MainActivity, "looking for $query", Toast.LENGTH_LONG).show()
            return true
        }
i have this in the main activity. if I want to reuse this(it's a search bar on the actionbar) in another activity, do i have to copy and paste the code every time? or is there an easier/efficient way to do this?
s
The suggested way would probably be to see if you can combine those activities into one, so you don't have to duplicate, and have the contents be fragments
if that's not possible, I would suggest trying to take the search view stuff and try to make it an independent component, so you just have to instantiate it
z
@Steve - its different screens so can i have fragments with different screens. i am just started to build so wont be a lot of rework. need to read up on fragments since idk how to do that just yet
s
the general suggestion nowadays is to only use activities where they're absolutely needed, and use fragments everywhere else
z
good to know. i'll read up on fragments so i can leverage
i
I'd strongly suggest starting with Navigation rather than raw fragments: https://developer.android.com/guide/navigation
And check out the Navigation code lab for a hands on approach for how it works: https://codelabs.developers.google.com/codelabs/android-navigation/
z
are there any youtube videos you recommend?
i
That first link has an overview video right at the top, plus a ton of video links at the bottom
z
@Ian Lake im going to try this but i dont have a buttom navigation. i only have a top action bar with the app name, a search icon and a menu option list
is that okay?
i
Yep
z
great, going to try now
thanks
@Steve read up on fragments, it's a lot harder to code than activities it seems. will keep learning
😂 1
c
@ZariApps I agree with what Ian said. The "Navigation" architecture component makes it dead easy to work with fragments.
z
Yeah I am pretty new so I'm learning and implimeting slowly. It seems to be successful so far!
👍 1
@Steve @Ian Lake with fragments, its is possible to change menus?
so if you're in one fragment, one menu and when you go to another, another menu?
i
I mean, that's how it works automatically - fragments on the back stack won't have their menu items displayed
z
so i have the title of the page youre on in teh menu
right now, all of them are showing the same title
@Ian Lake
i
Did you read the documentation on how to set up the top bar with Navigation? It specifically points out how to set a label for each destination https://developer.android.com/guide/navigation/navigation-ui#top_app_bar
z
oh shoot no sorry. i have been youtubing to learn and completely forgot abotu that link
i will read now!
im a visual learner sorry
@Ian Lake it works!!!
🎉 1
thank you so much man
sorry i didnt read your document earlier lol
quick question though, my titles of the toolbars are not good
meaning they are like the schema names
trying to fix, i think i know how
yup got it, updating the label in the navigation
this is awesomeeeeeeeeeee
i literally started learning a week ago and i have a pretty good understanding now. thank you to everyone who has helped me
@Ian Lake @Steve @Colton Idle
👍 1
c
Good luck! The docs sometimes still seem to escape me, but its nice that people will point you in the right direction.
z
any reason my menu wont show up in the toolbar?
Copy code
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    val inflater = menuInflater
    super.onCreateOptionsMenu(menu)
    inflater.inflate(R.menu.main_menu, menu)
return true
i
Does your fragment call
setHasOptionsMenu(true)
(say, in
onCreate()
)?
z
current focus is on the title menu and no, i dont see that anywhere
let me try adding it
z
added it, no luck. let me read the doc
Copy code
setSupportActionBar(toolbar)
i was missing this
i converted my actionbar into a toolbar and i guess didnt set that
i
Yeah, the fragment menu APIs plug into the ActionBar
z
@Ian Lake thanks again man. you're the best
i
Just make sure to use the instructions on the Action Bar part of the Navigation UI page as they do differ quite a bit from the Toolbar that isn't an ActionBar instructions
z
will keep an eye out. i am facing a UI error when i click on the search box and click on menu option and backout, it removes the search icon
image.png,image.png,image.png,image.png
see search icon gone....
and search option shoes up in menu when it shouldnt
Copy code
<item
    android:id="@+id/search"
    android:icon="@drawable/ic_search_black_24dp"
    android:title="@string/search"
    app:actionViewClass="androidx.appcompat.widget.SearchView"
    app:showAsAction="ifRoom|collapseActionView" />
@Ian Lake can you help?
i
Probably a better stack overflow question
👍 1
z
will ask, thank you
245 Views