3

I tried to follow this answer, with the main idea is to override the setContentView in the BaseActivity, the activity that will be extended by all activity so only BaseActivity will have the navigation drawer.

However, my navigation is never shown (even in BaseActivity) after i tried to implement the answer. This is what i did :

This is the navigation drawer's xml :

<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" >

    <!-- Framelayout to display Fragments -->

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

    <!-- Listview to display slider menu -->

    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="120dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/list_background"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

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

This is my overrided setContentView (in BaseActivity) :

@Override
    public void setContentView(int layoutResID) {
        // TODO Auto-generated method stub
        mDrawerLayout= (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_main, null); // Your base layout here
        actContent= (FrameLayout) mDrawerLayout.findViewById(R.id.frame_container);
        //listLayout = (LinearLayout) mDrawerLayout.findViewById(R.id.listLayout);
        getLayoutInflater().inflate(layoutResID, actContent, true);
        //getLayoutInflater().inflate(layoutResID, listLayout, true);
        super.setContentView(layoutResID);
    }

And this is my onCreate on BaseActivity, note that i use getLayoutInflater().inflate instead of setContentView :

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

        //getSupportActionBar().setDisplayShowHomeEnabled(false);
        //getSupportActionBar().setDisplayShowCustomEnabled(true);
        // enabling action bar app icon and behaving it as toggle button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        mTitle = mDrawerTitle = getTitle();

        // load slide menu items
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons from resources
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);

        //mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout= (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
        //mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
        mDrawerList = (ListView) mDrawerLayout.findViewById(R.id.list_slidermenu);

        navDrawerItems = new ArrayList<NavDrawerItem>();

        // adding nav drawer items to array
        // Home
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
        // Find People
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
        // Photos
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
        // Communities, Will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
        // Pages
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
        // What's hot, We  will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));


        // Recycle the typed array
        navMenuIcons.recycle();

        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

        // setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getApplicationContext(),
                navDrawerItems);
        mDrawerList.setAdapter(adapter);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ) {
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            // on first time display view for first nav item
            displayView(0);
        }

    }

And finally, i just need to extend the BaseActivity class and code like usual.

UPDATE

According to the answer, this is my WORKING overrided setContentView :

@Override
    public void setContentView(final int layoutResID) {
        // TODO Auto-generated method stub
        mDrawerLayout= (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_main, null); // Your base layout here
        actContent= (FrameLayout) mDrawerLayout.findViewById(R.id.frame_container);

        getLayoutInflater().inflate(layoutResID, actContent, true);
        super.setContentView(mDrawerLayout);

        mTitle = mDrawerTitle = getTitle();

        // load slide menu items
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons from resources
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);

        mDrawerList = (ListView) mDrawerLayout.findViewById(R.id.list_slidermenu);

        navDrawerItems = new ArrayList<NavDrawerItem>();

        // adding nav drawer items to array
        // Home
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
        // Find People
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
        // Photos
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
        // Communities, Will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
        // Pages
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
        // What's hot, We  will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));


        // Recycle the typed array
        navMenuIcons.recycle();

        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

        // setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getApplicationContext(),
                navDrawerItems);
        mDrawerList.setAdapter(adapter);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ) {
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        //super.setContentView(mDrawerLayout);
        /*getLayoutInflater().inflate(layoutResID, actContent, true);
        super.setContentView(mDrawerLayout);*/
    }
Community
  • 1
  • 1
Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
  • Just you are inflating the drawer layout, but not attaching it to `Activity` view hierarchy. note that just inflating does not mean adding – Gopal Gopi Mar 26 '14 at 05:59
  • @GopalRao sorry, what do you mean?Where should i attach it, in BaseActivity or in the activity that extends the BaseActivity? – Blaze Tama Mar 26 '14 at 06:03

2 Answers2

7

Change your setContentView() like this...

@Override
public void setContentView(int layoutResID) {
    mDrawerLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_main, null); 
    actContent = (FrameLayout) mDrawerLayout.findViewById(R.id.frame_container);
    // set the drawer layout as main content view of Activity.
    setContentView(mDrawerLayout);
    // add layout of BaseActivities inside framelayout.i.e. frame_container
    getLayoutInflater().inflate(layoutResID, actContent, true);
}
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • Thanks for your help. I tried your answer but the navigation drawer is still not showing in both of BaseActivity and Activity that extended it – Blaze Tama Mar 26 '14 at 06:30
  • @BlazeTama Already you have initialized `drawerLayout` in `BaseActivity`, and why are you pointing it to another instance of drawerlaout in your SubActivity? I have seen this statement `mDrawerLayout= (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_main, null);` in ur sub activity – Gopal Gopi Mar 26 '14 at 06:34
  • Its not in my sub activity, its in BaseActivity's onCreate & setContentView – Blaze Tama Mar 26 '14 at 06:37
  • Move all your drawer layout related operations into `setContentView()` ... don't inflate drawer layout twice. you are inflating once in setContentView() and again in `onCreate()`... – Gopal Gopi Mar 26 '14 at 06:37
  • do you mean i need to move the setDrawerListener & mDrawerToggle = new ActionBarDrawerToggle from onCreate to setContentView? – Blaze Tama Mar 26 '14 at 06:38
  • @BlazeTama Yes. Do that. and dont inflate drawer layout twice – Gopal Gopi Mar 26 '14 at 06:40
  • Please see my edited question Sir, now the navigation bar is working BUT the apps dont display the view from my sub activities – Blaze Tama Mar 26 '14 at 08:00
  • @BlazeTama Do u mean, Activity not showing a `View` that is added in `FrameLayout`? – Gopal Gopi Mar 26 '14 at 09:11
  • I did it! Thanks very much for your help, i have been working on this since morning – Blaze Tama Mar 26 '14 at 09:17
4

I know this is already answered, but in case someone struggles with my specific problem:

DrawerLayout can only have 2 childs, content and the navigationView. if you want to include a toolbar you should wrap this inside another layout or outside the drawerlayout:

DrawerLayout
--LinearLayout (or any other layout)
----Toolbar
----Content
--NavigationView

or:

LinearLayout (or any other layout)
--Toolbar
--DrawerLayout
----Content
----NavigationView
Lars Celie
  • 622
  • 5
  • 17