Create a new action by clicking the settings icon.

If I click on the icon settings, it should create a new activity. But it does show a search popup. Below I post codes and screenshots related to this.

enter image description here

enter image description here

MainActivity.java:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
       switch (item.getItemId()) 
       {
         case R.id.search:
            Intent intent = new Intent(this, SettingsActivity.class);
      //      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
            return true;
         default:
            return super.onOptionsItemSelected(item);
       }
    }

main.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" >

    <item
        android:id="@+id/search"
        android:icon="@drawable/ic_action_settings"
        android:title="@string/search"/>

</menu>

If I click on this icon, a new action must be created. But it does show a popup search. I do not need this popup. He just needs to switch to a new action by clicking on the icon.So setting far I made such code.

+4
source share
3 answers

You need to add an element tag

android:showAsAction="always"

Then there are no pop-ups.

+5
source

Change xml to:

<item
    android:id="@+id/search"
    android:icon="@drawable/ic_action_settings"
    app:showAsAction="always"
    android:title="@string/search"/>

: showAsAction = "always", NAP.

+2

You override onOptionsItemSelected, but you write the code to start the activity in the menu item Seachon click. Instead, you will need to do this in settingsclick

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
   switch (item.getItemId()) 
   {
       case R.id.settings:
           Intent intent = new Intent(this, SettingsActivity.class);
           startActivity(intent);
           return true;
       default:
           return super.onOptionsItemSelected(item);
    }
}

Hope this helps.

0
source

All Articles