Fatima
Fatima

Reputation: 869

Toolbar in DrawerLayout takes up the whole area?

I've been trying to use the new design library and I have a toolbar instead of deprecated action bar. The problem is that the toolbar takes up the whole area in in the activity. I've been trying out different examples and replaced my layout files with them, I've tried different values for width and height. I have no clue why it's acting like this. and none works! What may be causing this?

Any help is highly appreciated.

activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                        xmlns:app="http://schemas.android.com/apk/res-auto"
                                        android:id="@+id/drawer_layout"
                                        android:layout_width="match_parent"
                                        android:fitsSystemWindows="true"
                                        android:layout_height="match_parent">
    <include
            android:id="@+id/tool_bar"
            layout="@layout/app_bar"
            ></include>
<RelativeLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

</RelativeLayout>


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

main activity :

 public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {



Toolbar toolbar;
View root;
NavigationView nav_draw;
DrawerLayout drawer_layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    root = getLayoutInflater().inflate(R.layout.activity_main, null);
    setContentView(root);

    setupToolbar();

    drawer_layout = (DrawerLayout)findViewById(R.id.drawer_layout);
    nav_draw = (NavigationView) findViewById(R.id.nav_draw);
    nav_draw.setNavigationItemSelectedListener(this);

getSupportFragmentManager().beginTransaction()
        .replace(R.id.container, new Fragment())
        .commit();

}
private void setupToolbar(){
    Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);
    // Show menu icon
    final ActionBar ab = getSupportActionBar();
    ab.setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    ab.setDisplayHomeAsUpEnabled(true);
}
}

app_bar layout file

<?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_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

I have alse used this links layouts: http://hmkcode.com/material-design-app-android-design-support-library-appcompat/

Upvotes: 0

Views: 1861

Answers (3)

natario
natario

Reputation: 25194

DrawerLayouts should have two childs:

  • the content child;
  • the navigation drawer child,

in this particular order. By <include>ing your Toolbar you are adding a third child and that is not allowed. You should merge the Toolbar and your content frame in a single layout:

<android.support.v4.widget.DrawerLayout>

    <LinearLayout
        android:id="@+id/main_container"
        android:orientation="vertical">

        <include
            android:id="@+id/tool_bar"
            layout="@layout/app_bar" />
        <RelativeLayout android:id="@+id/container" />

    </LinearLayout>

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

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

Upvotes: 5

Er.Rohit Sharma
Er.Rohit Sharma

Reputation: 696

You can use toolbar just as a normal view. So you dont need to use settoolbar method. Simply write following code in you layout:

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

<android.support.v7.widget.Toolbar
    android:id="@+id/mainToolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentTop="true"
    android:background="@drawable/bottom_line_shape">
 //here you can add as many views as you want just like a simple layout can

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

Upvotes: 0

Michał Hawryszko
Michał Hawryszko

Reputation: 1406

Just add this toolbar to your container, the code below worked perfectly for me:

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

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/mainToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentTop="true">

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

Upvotes: 1

Related Questions