2

I'm trying create an ActionBar for my Android app with Android Studio, but when I execute, the items don't appear. I set the showAsAction to always or ifRoom but nothing happened.

Menu code:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/itmp"
        android:icon="@mipmap/ic_launcher"
        android:title="Profile"
        android:showAsAction="always"/>

    <item android:id="@+id/itap"
        android:icon="@mipmap/ic_launcher"
        android:title="My order"
        android:showAsAction="ifRoom" />

    <item android:id="@+id/ithc"
        android:icon="@mipmap/ic_launcher"
        android:title="Historic"
        android:showAsAction="ifRoom" />

    <item android:id="@+id/itsr"
        android:icon="@mipmap/ic_launcher"
        android:title="Exit"
        android:showAsAction="ifRoom"/>
</menu>

MainActivity code:

public class MainScreen_Activity extends AppCompatActivity {

GridView grid;
Button c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mainscreen);

    c = (Button) findViewById(R.id.btnmaincar);
    c.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_actionbar, menu);
    return super.onCreateOptionsMenu(menu);
}

}

when I execute the app in my smartphone anything doesn't appear. Only an option icon with all the items.

hata
  • 11,633
  • 6
  • 46
  • 69
FelipeRsN
  • 887
  • 1
  • 9
  • 19

1 Answers1

3

When using AppCompatActivity, you need to use app:showAsAction instead of android:showAsAction.

Your menu XML should look more like this:

<?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/itmp"
        android:icon="@mipmap/ic_launcher"
        android:title="Profile"
        app:showAsAction="always"/>

</menu>
Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120