Cristian Flaviu
Cristian Flaviu

Reputation: 325

Get specific MenuItem inside onOptionsItemSelected and setting it as SHOW_AS_ACTION_NEVER

I have a Menu with 4 Menu Items and when one is clicked I want to move it to the top(MenuItem.SHOW_AS_ACTION_ALWAYS) and the previous one selected should go down(MenuItem.SHOW_AS_ACTION_NEVER ). I have managed to add the selected one to the top but I don't know how to move the previous one selected down.

My idea was to get all the others and set them as MenuItem.SHOW_AS_ACTION_NEVER.I tried to select one like this

  val javaItem = findViewById<MenuItem>(R.id.javaMenuItem)

but it says it expecting a View

This is my Menu

<?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/javaMenuItem"
        android:title="@string/javaItem" />
    <item
        android:id="@+id/pythonMenuItem"
        android:title="@string/pythonItem" />

    <item
        android:id="@+id/rubyMenuItem"
        android:title="@string/rubyItem" />
    <item
        android:id="@+id/javaScriptItem"
        android:title="@string/javaScriptItem" />
</menu>

This is what I have done

override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val notSelectedTextView = findViewById<TextView>(R.id.middleTextMessageTextView);
        notSelectedTextView.visibility = View.GONE

        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    
            when (item.itemId) {
                R.id.javaMenuItem -> {
                    getDataFromApi("Java")
                    setListView();
                }
                R.id.pythonMenuItem -> {
                    getDataFromApi("Python")
                    setListView();
                }
                R.id.rubyMenuItem -> {
                    getDataFromApi("Ruby")
                    setListView();
                }
                R.id.javaScriptItem -> {
                    getDataFromApi("JavaScript")
                    setListView();
                }
            }
    
            return super.onOptionsItemSelected(item)
        }

Upvotes: 1

Views: 627

Answers (1)

Zain
Zain

Reputation: 40810

You can save the menu in a local field, and for each call of onOptionsItemSelected(), you can iterate on all menu items, and set them to MenuItem.SHOW_AS_ACTION_NEVER

Assuming the menu is called R.menu.my_menu, save the menu to a local var:

var mMenu: Menu? = null
override fun onCreateOptionsMenu(menu: Menu): Boolean {
    menuInflater.inflate(R.menu.my_menu, menu)
    mMenu = menu
    return true
}

Then iterate on menu items to set their action to SHOW_AS_ACTION_NEVER before setting the clicked item to SHOW_AS_ACTION_ALWAYS

override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val notSelectedTextView = findViewById<TextView>(R.id.middleTextMessageTextView);
        notSelectedTextView.visibility = View.GONE

        // iterate over menu items
        for (i in 0 until mMenu?.size()!!) {
            val menuItem: MenuItem = mMenu!!.getItem(i)
            menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER)
        }

        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    
            when (item.itemId) {
                R.id.javaMenuItem -> {
                    getDataFromApi("Java")
                    setListView();
                }
                R.id.pythonMenuItem -> {
                    getDataFromApi("Python")
                    setListView();
                }
                R.id.rubyMenuItem -> {
                    getDataFromApi("Ruby")
                    setListView();
                }
                R.id.javaScriptItem -> {
                    getDataFromApi("JavaScript")
                    setListView();
                }
            }
    
            return super.onOptionsItemSelected(item)
        } 

Upvotes: 1

Related Questions