0

I am trying to make an action bar (through toolbar) with a navigation drawer, however I only manage to get the navigation drawer part working. When the app loads in the emulator, it does seem that the toolbar shows up on the screen for a fraction of a second before it disappears, showing a screen without a toolbar. How do i get the toolbar to show up/not disappear on load?

This is how it looks like:

enter image description here

styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimaryDark</item>
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

</style>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

styles.xml (v21)

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

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- API 14 theme customizations can go here. -->
    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
    <item name="colorPrimary">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimaryDark</item>
</style>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>

</style>
</resources>

MainActivity.java (trimmed away excess codes)

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity implements OnMarkerClickListener {
Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
 toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

...
}

main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"

tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <include
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        layout="@layout/toolbar_layout"
        />

</LinearLayout>

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


</android.support.design.widget.NavigationView>


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainfrag"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/main_layout" />

</FrameLayout>

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

toolbar.xml

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

</android.support.v7.widget.Toolbar>

AndroidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
...
aurelio
  • 207
  • 1
  • 8
  • 18
  • Your `` isn't setup correctly. You need to have one, single content `View`, and the drawer needs to be listed last within ``. Have a look at [this post](http://stackoverflow.com/questions/26440879/how-do-i-use-drawerlayout-to-display-over-the-actionbar-toolbar-and-under-the-st) for a correct example. – Mike M. Sep 03 '16 at 15:12
  • I have no issue with the DrawerLayout but the Toolbar which isn't showing up. Sorry if I'm missing anything as I'm new to android development. – aurelio Sep 03 '16 at 15:50
  • 1
    You need to have only one, single content `View` in the `DrawerLayout`. Your `` and `Toolbar` `` both need to be inside another `ViewGroup`, like a vertical `LinearLayout`. Currently, the `FrameLayout` with the `` is covering the `LinearLayout` with the `Toolbar`. Move the `` to the ``, and delete the ``. – Mike M. Sep 03 '16 at 15:55
  • Try out my Edited answer, I just edited the FrameLayout 's layout_height and layout_weight attributes so that the FrameLayout will not pull the Toolbar out of the Screen. I think this will work for you. – Ajay Sivan Sep 04 '16 at 04:17

2 Answers2

0

main_layout.xml

 <?xml version="1.0" encoding="utf-8"?>
    <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:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            layout="@layout/toolbar_layout"
            />

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/mainfrag"
        android:layout_width="match_parent"
        android:layout_height="0dp"  //Changed Value
        android:layout_weight="1">   //Added

        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout="@layout/main_layout" />

    </FrameLayout>

    </LinearLayout>

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


    </android.support.design.widget.NavigationView>

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

DrawerLayout Should only contain two child Views, first the main content and then the drawer view.

Creating a Navigation Drawer

Ajay Sivan
  • 2,807
  • 2
  • 32
  • 57
0

As Ajay mentioned that DrawerLayout should only contain two layouts so I am editing some of your codes to fulfill the requirements and I think this would work just give it a try

main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<include
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    layout="@layout/other_layout"/>
<android.support.design.widget.NavigationView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/navigation_view"
    android:layout_gravity="start"
    app:menu="@menu/drawer_menu"/>
</android.support.v4.widget.DrawerLayout>

Now add your other layouts to another layout file

other_layout.xml

<LinearLayout
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:orientation="vertical">
<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/toolbar"
    android:background="?attr/colorPrimaryDark"
    android:minHeight="?attr/actionBarSize"
    android:fitsSystemWindows="true"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<FrameLayout
    android:id="@+id/mainfrag"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout="@layout/main_layout" />
</FrameLayout>
</LinearLayout>
Dushyant Suthar
  • 673
  • 2
  • 9
  • 27