0

I am trying to add Toolbar and a right drawer to the well-known Navigation Drawer Example by Google, but for some reason the Toolbar is not visible:

app screenshot

My complete project is available at GitHub.

Here is the XML layout file activity_main.xml (I do not use include and a separate toolbar.xml file - because there is just one single Activity in the app):

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <android.support.v7.widget.Toolbar 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFF"
        android:theme="@style/ThemeOverlay.AppCompat.Dark" />

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

    </RelativeLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>

    <ListView
        android:id="@+id/right_drawer"
        android:layout_width="180dp"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#CCC"/>
</android.support.v4.widget.DrawerLayout>

And here is the MainActivity.java with added mToolbar and mActionList:

public class MainActivity extends AppCompatActivity {
    private Toolbar mToolbar;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ListView mActionList;
    private ActionBarDrawerToggle mDrawerToggle;

    private String[] mPlanetTitles;
    private String[] mActions;
    private TypedArray mIcons;

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

        mPlanetTitles = getResources().getStringArray(R.array.planets_array);
        mActions = getResources().getStringArray(R.array.music_actions);
        mIcons = getResources().obtainTypedArray(R.array.music_icons);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mActionList = (ListView) findViewById(R.id.right_drawer);

        mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView view = (TextView) super.getView(position, convertView, parent);
                view.setCompoundDrawablePadding(16);
                view.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_stars_white_24dp, 0, 0, 0);
                return view;
            }
        });

        mActionList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mActions) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView view = (TextView) super.getView(position, convertView, parent);
                view.setCompoundDrawablePadding(16);
                int res = mIcons.getResourceId(position, R.drawable.ic_menu_black_24dp);
                view.setCompoundDrawablesWithIntrinsicBounds(res, 0, 0, 0);
                return view;
            }
        });

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                mToolbar,
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
                ) {
            public void onDrawerClosed(View view) {
                mToolbar.setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                mToolbar.setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }
    }

Please advise, what is missing here - why isn't the Toolbar visible?

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416

1 Answers1

1

You have added Toolbar within RelativeLayout and will need to specify its position or else the FrameLayout will occupy the entire screen. Just add android:layout_below="@id/toolbar" in the Framelayout to position the FrameLayout below the toolbar.

Corrected code below:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFF"
    android:theme="@style/ThemeOverlay.AppCompat.Dark" />

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_below="@id/toolbar"    //added this
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

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

Since you have just 2 views which need to be vertically oriented you can just replace RelativeLayout with LinearLayout with vertical orientation.

Psypher
  • 10,717
  • 12
  • 59
  • 83
  • 1
    DrawerLayout is designed to hold basically 2 views, one is the main view which should cover the whole screen and would be with match_parent attribute and the second the drawers whose positions are controlled using gravity. So you cannot get rid of the RelativeLayout/LinearLayout. – Psypher Jul 15 '15 at 10:48
  • Last question if you don't mind - I have applied your suggestion to use `LinearLayout` and [it works well](https://github.com/afarber/android-newbie/tree/q33/NavigationDrawerExample). But I don't know how/where to add a button for the right drawer. I can only display it by swiping. – Alexander Farber Jul 15 '15 at 10:56
  • 1
    If your question is regarding adding button in the right drawer, then you have to add in the listview "right_drawer". You will have to create a custom adapter to hold the buttons within listview. You can google how to create custom adapter for listviews. – Psypher Jul 15 '15 at 11:03
  • No, I have understood ListViews well. I am not accustomed to Drawer and Toolbar though. How to add a "burger" button in Toolbar which would open/close the right Drawer with music actions? – Alexander Farber Jul 15 '15 at 11:04
  • 1
    Use this code....`ActionBar ab = getSupportActionBar(); ab.setHomeAsUpIndicator(R.drawable.ic_menu); ab.setDisplayHomeAsUpEnabled(true);` . Here R.drawable.ic_menu is a drawable resource which in your case is a burger icon. In the method onDrawerOpened add code to play music – Psypher Jul 15 '15 at 11:08
  • Actually I had this code already and it doesn't change anything - there is a burger button for the left (navigation) drawer, but not for the right (music actions) drawer. Also if I delete that code - nothing changes. – Alexander Farber Jul 15 '15 at 11:10
  • K..Thats interesting...I never thought of adding in the right drawer. All I think of is to create a button on the toolbar with the burger icon background and on click of it open the drawer. You can post this as a separate question to get more answers who tried it out before – Psypher Jul 15 '15 at 13:49
  • Thanks, I've just posted this as separate question here: http://stackoverflow.com/questions/31432292/right-drawer-how-to-add-toolbar-button-and-enable-mini-variant-in-closed-state – Alexander Farber Jul 15 '15 at 13:50