Stephane
Stephane

Reputation: 12740

The action bar is not displayed

I have a new app, my first one in fact, for which I'd like to display the navigation drawer.

The app runs fine and displays the user profile after he has logged in to the remote server.

The drawer works fine, opening and closing as expected.

But there should be an action bar. And I don't see it. Also, there is no hamburger drawer icon to be seen either.

The user profile class that is loaded fine, displaying the user profile data:

public class UserProfileActivity extends UserActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile);
    }

}

The user activity is a base class to all user activities:

public class UserActivity extends BaseActivity {

    private String email;
    private String token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user);
        setupDrawer();
    }

}

And finally the base activity:

public class BaseActivity extends AppCompatActivity {

private Toolbar toolbar;
private ActionBarDrawerToggle drawerToggle;    
private DrawerLayout baseLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    checkVersion();
}

@Override
public void setContentView(int layoutResID) {
    baseLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.base, null);
    FrameLayout contentLayout = (FrameLayout) baseLayout.findViewById(R.id.content);
    getLayoutInflater().inflate(layoutResID, contentLayout, true);
    super.setContentView(baseLayout);

    // Set a Toolbar to replace the ActionBar
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (null != toolbar) {
        setSupportActionBar(toolbar);
    }
    ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(true); 
    actionBar.setDisplayHomeAsUpEnabled(true);

    drawerToggle = new ActionBarDrawerToggle(this, baseLayout, toolbar , R.string.drawer_open, R.string.drawer_close) { 
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            //getActionBar().setTitle(mTitle);
            //invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        } 
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            //getActionBar().setTitle(mDrawerTitle);
            //invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        } 
    };
    baseLayout.setDrawerListener(drawerToggle);
    baseLayout.post(new Runnable() {
        @Override
        public void run() {
            drawerToggle.syncState();
        }
    });
}

The adb logcat shows that the toolbar is not null:

E/permapp ( 3292): ========>> toolbar: android.support.v7.widget.Toolbar@41d7b398

The base.xml layout is:

<android.support.v4.widget.DrawerLayout
    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/base"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >    

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <include
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer_menu" 
        />

</android.support.v4.widget.DrawerLayout>

The toolbar.xml layout is:

    <?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:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:background="?attr/colorPrimaryDark">
    </android.support.v7.widget.Toolbar>

The style is done in two files, res/values/styles.xml contains:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar"></style>

and res/values-v19/styles.xml contains:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">true</item>
</style>

I'm having the following dependencies:

<dependency>
  <groupId>com.google.android</groupId>
  <artifactId>android</artifactId>
  <version>4.1.1.4</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>com.android.support</groupId>
  <artifactId>support-v4</artifactId>
  <version>23.0.1</version>
  <type>aar</type>
</dependency>
<dependency>
  <groupId>com.android.support</groupId>
  <artifactId>appcompat-v7</artifactId>
  <version>23.0.0</version>
  <type>aar</type>
</dependency>
<dependency>
  <groupId>com.android.support</groupId>
  <artifactId>design</artifactId>
  <version>23.0.0</version>
  <type>aar</type>
</dependency>

Upvotes: 0

Views: 61

Answers (1)

fweigl
fweigl

Reputation: 22008

   <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <include
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

This is the relevant part of your layout. Both your Toolbar and your FrameLayout are within a RelativeLayout. The FrameLayout simply lies over your Toolbar, covering it. Either replace the RelativeLayout with a LinearLayout or tell the FrameLayout to be below the toolbar (within the RelativeLayout) like

<include
    android:id="@+id/toolbarinclude"
    layout="@layout/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<FrameLayout
    android:layout_below="@id/toolbarinclude"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Upvotes: 1

Related Questions