Artin GH
Artin GH

Reputation: 566

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>

Upvotes: 0

Views: 5571

Answers (4)

Rouhollah Ahmadi
Rouhollah Ahmadi

Reputation: 19

for android studio flamingo (2022.2.1):

open "manifest.xml"

replace:

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

with:

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

Upvotes: 1

zekromWex
zekromWex

Reputation: 290

First you have to add a toolbar in your MainActivity.xml, inside your Relative Layout and over your ListView. You can create a new file for that like naming it 'Toolbar.xml' and creating the content like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="Main Activity"/>

then include it in your MainActivity.xml:

<?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">

<include layout="@layout/toolbar" id="@+id/toolbar">

<ListView
    android:id="@+id/list"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>

</RelativeLayout>

Then you have to instantiate the toolbar in your app first. Like this:

yourToolbar = (Toolbar) findViewById(R.id.yourToolbar);
setSupportActionbar(yourToolbar);

You can also check out these forums.

Toolbar.inflateMenu seems to do nothing

How to set menu to Toolbar in Android

Hope this helps!

Upvotes: 0

R.R.M
R.R.M

Reputation: 790

Change you theme from :

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

to :

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

Upvotes: 0

Sasi Kumar
Sasi Kumar

Reputation: 13288

Change your app theme

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

into

  android:theme="@style/AppTheme"

Upvotes: 3

Related Questions