0

I want to create Navigation Drawer in all of my Activities (not fragments).

Tricky part is it should be one common Drawer. i.e. the list items must be same. In drawer the current opened activity should be highlighted bold & disabled in the ListView, while other activities should be in normal text and enabled.

After going thru many examples, like this, I've come up with 2 approaches:

  1. Either make different Navigation Drawer in each activity with same ListView and dynamically make the currently opened list item bold & disabled. Or,

  2. As mentioned in the link above, use a BaseActivity, define a DrawerLayout there and use it in other activities. (Issue is somehow I do not to setContentView(layout) in BaseActivity. It does not feel right using layout in activity that is not shown anywhere)

Please suggest

Thank You

Community
  • 1
  • 1
collin
  • 297
  • 1
  • 5
  • 11
  • The usual use case for an item in the Drawer is to open a certain Fragment in the Activity that's hosting the Drawer. If you're opening an Activity from it then you should display the Up caret in that Activity to demonstrate that you're in a sub-Activity and there's a way to navigate up the hierarchy. So, showing the Drawer in that Activity would be a mistake navigation-wise. – Egor Mar 12 '14 at 20:00
  • You could have an abstract getLayoutId() function in your BaseActivity that the subclasses implement. As long as each layout you create has the same DrawerLayout id's then it will work. I used just such a solution to update the ui of an application without performing a complete rewrite due to time constraints. I did not feel good about it. @Egor makes very good points about the problematic nature of what you are trying to do. – jjhorgan Mar 12 '14 at 21:25
  • @jjhorgan what will the abstract getLayoutID() method will do? Can you please explain with example (possibly as answer). – collin Mar 13 '14 at 08:28

1 Answers1

0

Allow subclassed activities to provide the content view layout id.

public class BaseActivity extends Activity{

       public void onCreate(Bundle b){
                super.onCreate(b);

                setContentView(getLayoutId())

                //set up drawer content here

    }



       protected abstract int getLayoutId();
}

public class AppActivity extends BaseActivity{

    public void onCreate(Bundle b){

      super.onCreate(b)

       //do not call setContentView in this subclass let the super class do that

       //set up activity specific content here
    }

    public int getLayoutId(){
        return R.layout.subactivity_layout;
    }
}

For this to work correctly the layout for each activity must include identical DrawerLayout elements. Specifically the id value of the drawer layout and the child view used as the drawer content must be the same in each file. Since android does not support inheritance in layout files this means having duplicate elements in the layout xml for each activity. You may be able to use the <merge> tag to include the existing layouts from your project into the new layout files.

jjhorgan
  • 107
  • 1
  • 5