-1

I'm building an app with AndroidStudio. I want to display an ActionBar in my application.

I want to add a menu into my AppCompactActivity. This is the menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_save"
        android:showAsAction="always"
        android:title="Salva"/>
</menu>

This is the layout that I can see from Android Studio preview.

enter image description here

This is my AppCompactActivity:

public class setting extends AppCompatActivity{
    public Program program;
    public TextView textTargetUri;
    public TextView labelUrl;
    public ImageView targetImage;
    private static final int SELECT_PICTURE = 1;

    private String selectedImagePath;
    private ImageView img;
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        img = (ImageView)findViewById(R.id.ImageView01);

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_setting, menu);
        return super.onCreateOptionsMenu(menu);
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
        case R.id.action_search:
            getSchermataHome();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
}

But this is the layou that I can see: enter image description here

How can I fixed it?

3
  • when you click on the setting in last picture do you see the save icon or not ?
    – Umair
    Commented Nov 30, 2017 at 11:47
  • No I can see only the text never icon
    – bircastri
    Commented Nov 30, 2017 at 11:49
  • The ActionBar in an AppCompatActivity is actually a Toolbar underneath, so just follow the accepted answer on the linked duplicate.
    – Mike M.
    Commented Nov 30, 2017 at 11:50

2 Answers 2

1

Try using the following code

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">


<item
    android:id="@+id/action_search"
    android:icon="@android:drawable/ic_menu_save"
    android:title="Salva"
    app:showAsAction="ifRoom|always"/>
</menu>

change always to ifRoom|always with app attribute

0

Change your android:showAsAction="always" to app:showAsAction="always"

So now your code would be

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_save"
        app:showAsAction="always"
        android:title="Salva"/>
</menu>

Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices.

Not the answer you're looking for? Browse other questions tagged or ask your own question.