Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android toolbar filling entire screen

So I am attempting to implement a navigation drawer using the appcompat library. I am using a toolbar as my action bar. My issue is that my toolbar is filling the entire screen.

This being the problem

Here is my toolbar.

 <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:minHeight="?attr/actionBarSize"
     android:background="?attr/colorPrimary" />

And my main activity.

<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" tools:context=".MainActivity">



    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <include layout="@layout/toolbar_main"/>

        <!--- Main Layout -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <!--- Nav Drawer -->
        <ListView
            android:id="@+id/navigation_drawer"
            android:layout_width="@dimen/drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#FFF"/>
    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

And finally my source.

 public class MainActivity extends ActionBarActivity {

    private Toolbar mToolbar;
    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawerLayout;
    private CharSequence mTitle;
    private ListView mDrawerList;
    private String[] mAddresses;
    private CharSequence mDrawerTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAddresses = getResources().getStringArray(R.array.addresses);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        //mToolbar.setNavigationIcon(R.drawable.ic_myaccount);

        setSupportActionBar(mToolbar);
        mTitle = mDrawerTitle = getTitle();
        //Set up the nav drawer
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,mToolbar,R.string.open_drawer,R.string.close_drawer);
        mDrawerLayout.setDrawerListener(mDrawerToggle);


        //Drawer List
        mDrawerList = (ListView) findViewById(R.id.navigation_drawer);
        mDrawerList.setAdapter(new ArrayAdapter<>(this, R.layout.drawer_list_item, mAddresses));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        mDrawerToggle.syncState();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState){
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }
}

EDIT:

If I move the toolbar include outside of the DrawerLayout, then this happens in addition to the navigation drawer no longer opening.

like image 882
ColinMKH Avatar asked Dec 11 '22 00:12

ColinMKH


1 Answers

For the first problem:

You are using a LinearLayout.The default orientation is horizontal.

You should add android:orientation="vertical" to your root element.

For the second problem:

Remove android:fitsSystemWindows="true" or move the Toolbar inside the first element in DrawerLayout. Pay attention the DrawerLayout must have 2 views inside.

    <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" tools:context=".MainActivity"
            android:orientation="vertical">

       <android.support.v4.widget.DrawerLayout>

        <!--- Main Layout -->
        <LinearLayout android:orientation="vertical">

          <include layout="@layout/toolbar_main"/>

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



        </LinearLayout>
        <ListView />

      </android.support.v4.widget.DrawerLayout>
   </LinearLayout>
like image 155
Gabriele Mariotti Avatar answered Jan 17 '23 08:01

Gabriele Mariotti