2

I am trying to create an item (Named "ADD")in the action bar. But whenever i run the code, It first displays an item which appears as three vertical dots, when i click on the vertical dots then my item appears.

I have also noticed by clicking my item named "ADD", it changes back to the three vertical dots

Can someone please tell me why is that happening? I want my item "ADD" to be displayed directly on the screen, instead of accessing it by clicking some other item.

JAVA

@Override

    public boolean onCreateOptionsMenu(Menu menu) 
   {

        MenuInflater inflater = getMenuInflater();

        inflater.inflate(R.menu.menu, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
   {

        int id = item.getItemId();

        if (id == R.id.add) 
        {
            Log.i("Action Button Tapped", "Add");
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
mehrdad khosravi
  • 2,228
  • 9
  • 29
  • 34
Aman Reddy
  • 31
  • 2

1 Answers1

0

You might want to consider using a support Toolbar instead of an action bar because

... the native ActionBar behaves differently depending on what version of the Android system a device may be using. By contrast, the most recent features are added to the support library's version of Toolbar, and they are available on any device that can use the support library.

See https://developer.android.com/training/appbar/setting-up.html

Toolbars are more general and more flexible and may make it easier to do what you want.

https://developer.android.com/reference/android/support/v7/widget/Toolbar.html

That said, to move forward with your current implementation, you may want to try setting the "showAsAction" to true XML attribute on your menu item. Something like this:

<menu 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto" >
  <item android:id="@+id/my_add_action"
      android:icon="@drawable/ic_action_add"
      android:title="@string/action_add_label"
      app:showAsAction="always"  />    
</menu>

Setting

app:showAsActionAlways="true" 

is supposed to always place the item in the app bar (aka "action bar"). But, forcing the action onto the bar like that is not really the best practice.

Also, there appears to be some reliability confusion being reported in the v7 appcompat library starting from Android 4.3 (API level 18): Android 4.3 menu item showAsAction="always" ignored

It may also be helpful to look at Item with app:showAsAction not showing for a guide to adding action items and Android 4.3 menu item showAsAction="always" ignored

For completeness, here's documentation where this attribute is explained: https://developer.android.com/guide/topics/resources/menu-resource.html

Community
  • 1
  • 1
albert c braun
  • 2,650
  • 1
  • 22
  • 32