2

After reading through a large portion of the official android documentation, I've run into trouble with setting up an Appbar without using a Toolbar object. Funnily, this official page on Menus links to using the Toolbar object in the onCreate() method. Although I've gotten a Toolbar to work in the recent past, I do not need the extra flexibility/customization of a Toolbar object and instead opt to use various menu xml files for my activities. The problem is: my code for the option menu doesn't produce any sort of Appbar... it only produces a blank white screen with the task bar (battery/time bar) on top.

General Notes: I'm not looking to support old SDKs (< 7). Also, the phone I'm using (LG Stylo3 7.0) does NOT have a menu button in hardware. I'm aiming to support minSdkVersion 21, with targetSdkVersion 28. I am also not extending a fragment. I have many other activities written out, though their contents are inaccessible without a proper menu navigation.

The following code/resources are what I've written:

app/[package]/MainActivity.java

package [package];

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import androidx.appcompat.app.AppCompatActivity;    
import [package].R;

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) {
//        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;

Note: I've tried calling super in onCreateOptionsMenu() and also calling it in the return statement. I've also tried extending AppCompatActivity instead of Activity, but I'm not looking to support early SDK versions. I've put break points in the onCreateOptionsMenu() and the Debug tool doesn't seem to stop here. Perhaps onCreateOptionsMenu() isn't being called from onStart()?

res/menu/main_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity" >

    <item android:id="@+id/action_servers"
        android:icon="@drawable/ic_action_server"
        android:title="@string/action_server"
        app:showAsAction="ifRoom" />

    <item android:id="@+id/action_settings"
        android:icon="@drawable/ic_action_settings"
        android:title="@string/action_settings"
        app:showAsAction="ifRoom"/>
</menu>

Note: AndroidStudio is able to preview this xml without any issue.

res/layout/activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <!-- Some commented out stuff for now -->

</androidx.constraintlayout.widget.ConstraintLayout>

Note: I've created a Toolbar object in this layout before which DOES work only this questions isn't about Toolbars. AndroidStudio does allow me to preview this and it is a blank screen.

AndroidManifest.xml

<application
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Note: I've tried various android:themes with/without ActionBar in res/values/styles.xml... notably: (Theme.AppCompat.Light.DarkActionBar and Theme.AppCompat.NoActionBar). The former theme results in a blank white screen, the latter results in a dark grey.

Do let me know if other files I've omitted are needed for further diagnosis. My hypothesis is that it has to do with the constraintLayout I'm using.

These are pages that I've looked at that have not helped in resolving my issue:

  1. Implementing an option menu in Android Studio
  2. Add a menu to an empty activity
  3. Activity does not have an options menu -- Not wanting to use ToolBar!
  4. how to display an option menu when an activity starts
Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
jsonV
  • 1,650
  • 2
  • 9
  • 23

1 Answers1

1

Changing the MainActivity.java file constructor line to extend AppCompatActivity

public class MainActivity extends AppCompatActivity {

and setting the android:theme to a variant of Theme.AppCompat* shows the MainActivity Appbar that I've been struggling to show. What follows is some more research I did to find a proper solution.

Why this works... are the differences between AppCompatActivity and the base class Activity. AppCompatActivity inherits from Activity and using one of these themes tells the app to use the native ActionBar class to provide the app bar. Generally, it seems that using the new Toolbar object is the best way to go as it allows for more customization (as of 2019). Most earlier questions revolving around this topic use outdated/deprecated code such as the ActionBarActivity.

This is achieved by changing the Activity's Layout file to include a Toolbar as per this documentation. The theme for the Toolbar can then be modified appropriately using techniques outside of the scope of this question.

jsonV
  • 1,650
  • 2
  • 9
  • 23