Reputation: 315
I'm just a beginner so I was trying to make an Android app specifically to learn about menu.xml
and ActionBar
. So I'm trying to add MenuItems
, here's my menu.xml
code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:PizzaApp="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<item android:id="@+id/action_order_pizza"
android:icon="@drawable/order_pizza"
android:title="@string/order_pizza"
android:orderInCategory="1"
PizzaApp:showAsAction="ifRoom"/>
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="2"
PizzaApp:showAsAction="never"/>
</menu>
Since I wanted my app to use Material design
, I didn't actually extend
the AppCompatActivity
, but I used the support.v7.app
dependency, just to make the app compatible. Now since support.v7.app
library doesn't contain the showAsAction
function, I used my AppName
in the menu.xml instead of android
. But the problem is that beside action_setting
item, action_order_pizza
is also showing up in the overflow instead of the ActionBar. Please help! And here is the MainActivity
code:
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
Thanks in advance!
Note: If I'm using
android
instead ofPizzaApp
, Android Studio shows an error though still when you run the app, it works fine!
Upvotes: 0
Views: 1488
Reputation: 199815
If you want to use any part of AppCompat such as app:showAsAction
, you should extend AppCompatActivity
and use Theme.AppCompat
. Theme.AppCompat
extends Theme.Material
and will allow you to use material design on all API 9+ devices.
Upvotes: 1