action bar not showing in activity

171 views Asked by At

I wrote a simple menu and added it to the action bar but I can't understand why they are not visible in my activity. I'm using java and android studio, thanks.

Here is my code:

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

the manifest file is:

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyNote"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

the menu.xml file:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:enabled="false"
        android:icon="@drawable/baseline_add_"
        android:title="@string/add"
        app:showAsAction="always" />
</menu>

and this is the result:

(https://i.stack.imgur.com/P6OvG.png)

2

There are 2 answers

0
Gordon leung On

you can have a look at your main style in your themes.xml to check whether the Action Bar is dismissed or not.

You main style name should be like Theme.YourProjectName

and the themes.xml should be located inside your resources root 's value package

Here is the example of Not showing a Action Bar

<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.NoActionBar">

And Here is the example of Showing a Action Bar for you App

 <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
0
vishwas babar On

in AndroidManifest.xml file, inside the Application tag replace this line,

 android:theme="@style/Theme.MyNote"

with,

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

try this I think it's going to work for you, i faced same problem recently.