Search code examples
javaandroidandroid-actionbar

missing action bar in empty activity


I want to learn how to work with toolbar in android and trying to add menu in action bar but when I ran my previews started project, noticed that default empty activity menu bar now missing! I don't know why this happened.

here is my MainActivity.xml code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ir.cupcode.artin.contact.MainActivity">
<ListView
    android:id="@+id/list"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>

</RelativeLayout>

and I have this block of code for menu bar in MainActivity.java :

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

here is my menubar.xml code

 <?xml version="1.0" encoding="utf-8"?>

<item
    android:id="@+id/action_refresh"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:icon="@drawable/ic_action_test"
    android:title="Refresh"/>
<item
    android:id="@+id/action_settings"
    android:title="Settings">
</item>

In addition I get an error for using android:showAsAction="always" that says should use app:showAsAction with the appcombat library

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_appicon2"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
    </application>


Solution

  • Change your app theme

      android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    

    into

      android:theme="@style/AppTheme"