Office Hours — Today, April 8

Tuesday, April 6

Apr 8
7:20 PM
Mark M.
has entered the room
Mark M.
turned on guest access
7:25 PM
Eric
has entered the room
Mark M.
hello, Eric!
how can I help you today?
Eric
afternoon mark
I am encountering issues implementing a searchview in a toolbar
I cannot see it in my toolbar
Mark M.
how are you adding it to the Toolbar?
7:30 PM
Eric
View paste
via android's actionViewClass attribute
View paste
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/search"
        android:title="@string/search_title"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionViewClass="androidx.appcompat.widget.SearchView" />
</menu>
Mark M.
are you getting the icon? or nothing at all?
Eric
I don't see any icon in my toolbar. I just see a title and back nav
Mark M.
actually, that android:icon value looks wrong -- are you sure there is a framework icon named ic_menu_search?
if that is an icon that you added to your app, it would be @drawable/ic_menu_search
Eric
yes, it compiles and I see a preview in my xml for my menu class
Mark M.
OK, then I recommend two experiments:
1. Humor me, and try changing it to @drawable/ic_menu_search, or to some other icon that is in your app, and see if you get better results
2. Try removing app:actionViewClass="androidx.appcompat.widget.SearchView" and see if you now get the icon (but, obviously, no SearchView)
for #2, if you get the icon, that means that the Toolbar is not liking that attribute in the menu resource -- if you do not get the icon, then the Toolbar is not liking the menu resource at all
7:35 PM
Mark M.
(or, if you do not get the icon, perhaps you are no\t actually adding the menu resource to the Toolbar)
Eric
I tried both and got the same result. An icon still doesnt show to expand a searchview.
Could it be my activity theme? "@style/Theme.AppCompat.NoActionBar"
Mark M.
that seems OK
how are you teaching the Toolbar about this menu resource?
7:40 PM
Eric
View paste
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val inflater = LayoutInflater.from(this)
        val binding = ActivityMomentsBinding.inflate(inflater)

        setSupportActionBar(binding.toolbar)
        binding.toolbar.inflateMenu(R.menu.menu_search)

menu_search is my menu xml that I displayed
Mark M.
I don't think setSupportActionBar() and inflateMenu() work together
if you want inflateMenu(), you should not need setSupportActionBar()
conversely, if you want setSupportActionBar(), I think that you would use the normal function overrides of onCreateOptionsMenu() and onOptionsItemSelected(), instead of inflateMenu()
Eric
ok I got rid of setSupportActionBar.
it works now, awesome mark
Mark M.
glad it worked!
7:45 PM
Eric
it got rid of my upnav, is there a way to add it in via my binding obj?
Mark M.
are you using Jetpack Navigation?
Eric
no
just a regular activity for now
Mark M.
hmmmm... I'm a bit surprised that you had up navigation then to begin with, as I didn't think that setSupportActionBar() did that... but it's been a while since I actively worked with this, so I forget the details
but, you could use setNavigationIcon() on the Toolbar to add it back in
and use setNavigationOnClickListener() to find out when the user taps it
7:50 PM
Eric
worked like a charm!
well that's all for today. I appreciate it as usual
Mark M.
you're welcome!
Eric
bye
Eric
has left the room
8:25 PM
Mark M.
turned off guest access

Tuesday, April 6

 

Office Hours

People in this transcript

  • Eric
  • Mark Murphy