1

I have 3 activities here and I want each activity to inherit my navigation drawer. I have looked around at many other resources, but I am still a little confused as how to implement it.

The following is my BaseActivity, HomeActivity, and ScheduleActivity

heres my base activity:

public class BaseActivity extends Activity {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;

public DrawerLayout drawer;
ImageView navDrawerBtn;

HashMap<String, List<String>> listDataChild;
List<String> listDataHeader;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
protected void onCreate(Bundle savedInstanceState) {


    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }


    navDrawerBtn = (ImageView)findViewById(R.id.headerDrawer);
    expListView = (ExpandableListView) findViewById(R.id.lvExp);

    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        expListView.setIndicatorBounds(402,465);    
    } else {        
        expListView.setIndicatorBoundsRelative(402,465);    
    } 

    drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
        ;
    prepareListData();



    navDrawerBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if(!drawer.isDrawerOpen(expListView)) {
                drawer.openDrawer(expListView);
                } else {
                    drawer.closeDrawer(expListView);
                }

            }
        });


    //listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);

    // Listview Group click listener
    expListView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
            // Toast.makeText(getApplicationContext(),
            // "Group Clicked " + listDataHeader.get(groupPosition),
            // Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    // Listview Group expanded listener
    expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " Expanded",
                    Toast.LENGTH_SHORT).show();
        }
    });

    // Listview Group collasped listener
    expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " Collapsed",
                    Toast.LENGTH_SHORT).show();

        }
    });

    // Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {

            switch (childPosition) {
            case 0: 
                Intent a = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(a);
                break;

            case 1: 
                Intent b = new Intent(getApplicationContext(), ScheduleActivity.class);
                startActivity(b);
                break;
}
            return false;
            // TODO Auto-generated method stub
 //             Toast.makeText(
//                      getApplicationContext(),
//                      listDataHeader.get(groupPosition)
//                              + " : "
//                              + listDataChild.get(
//                                      listDataHeader.get(groupPosition)).get(
//                                      childPosition), Toast.LENGTH_SHORT)
//                      .show();
//              return false;
        }
    });
 }


/*
 * Preparing the list data
 */
private void prepareListData() {


    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("VRP Medical Bay");
    //listDataHeader.add("");
    //listDataHeader.add("");

    // Adding child data

    List<String> listUnderVRP = new ArrayList<String>();

    listUnderVRP.add("eDataClinical");
    listUnderVRP.add("Schedule");
    listUnderVRP.add("Dictate");
    listUnderVRP.add("View Messages");
    listUnderVRP.add("Reports for Signature");
    listUnderVRP.add("View Billing");
    listUnderVRP.add("View State");


    listDataChild.put(listDataHeader.get(0), listUnderVRP); // Header, Child data
    //listDataChild.put(listDataHeader.get(1), nowShowing);
    //listDataChild.put(listDataHeader.get(2), comingSoon);


}



 }

heres my schedule activity:

public class ScheduleActivity extends BaseActivity {

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


    drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

my navigationdrawer layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="73dp"
    android:background="@color/actionbar" >

    <ImageView
        android:id="@+id/headerDrawer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_drawer"
        android:contentDescription="@string/desc" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/actionHeaderText"
        android:layout_toRightOf="@+id/headerDrawer"
        android:src="@drawable/e_icon"
        android:contentDescription="@string/desc"  />

    <TextView
        android:id="@+id/actionHeaderText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/imageView1"
        android:text="@string/actionbar_title"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/white"
        android:textSize="32sp"
        android:textStyle="bold"
        android:typeface="monospace" />

</RelativeLayout>

<android.support.v4.widget.DrawerLayout

        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!-- The main content view -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <!-- The navigation drawer -->

         <ExpandableListView
            android:id="@+id/lvExp"
            android:layout_width="470dp"
            android:layout_height="match_parent"
            android:groupIndicator="@drawable/group_selector"
             android:transcriptMode="alwaysScroll"
            android:layout_gravity="start"

            android:cacheColorHint="#00000000" >
        </ExpandableListView>
    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

and my schedule layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >   

</LinearLayout>
Sandeep R
  • 2,284
  • 3
  • 25
  • 51
user3243065
  • 29
  • 3
  • 8
  • Implement a BaseActivity with navigation drawer and extend all other activities with the BaseActivity instead of Activity...!!! Hope it will help you...!!! – Rushabh Patel Feb 13 '14 at 05:41
  • [Here](http://naddydroid.blogspot.in/) is a tutorial which does the same. – Naddy Jul 07 '14 at 03:22

3 Answers3

3

in onCreate of Schedule don't call setContentView instead do this:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    LayoutInflater inflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View contentView = inflater.inflate(R.layout.schedule, null, false);
    mDrawer.addView(contentView, 0); 
}

make mDrawer in MainActivity protected. and in R.layout.drawer_layout just keep drawer tag and the element with gravity left(or right).

AruLNadhaN
  • 2,808
  • 5
  • 24
  • 42
0

A better, easier & recommended way is to use 1 Activity & 3 fragments instead of 3 activities.

3 activities means, 3 view, which means 3 times your drawer would be created.

I suggest 1 Activity & 3 fragments (drawer would be created only once, just the main page would keep getting replaced) because that is what API suggests:

http://developer.android.com/training/implementing-navigation/nav-drawer.html

So make your navigation drawer like this:

(with default action bar)...http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

(without default action bar or your own action bar)...Using Navigation Drawer without TitleBar or ActionBar (my answer)

Community
  • 1
  • 1
Pranav Mahajan
  • 2,048
  • 2
  • 23
  • 36
  • what would be the best advise on dealing to different activities with same navigation drawer but will change the text of a navigation drawer, such as home, billing. – user3243065 Feb 13 '14 at 06:15
  • If you want the title to display on the action bar just along the "drawer open" button, then go with the (Drawer with Action Bar) link above. If you want the title to display inside the drawer somewhere or on the centre of action bar, then go with the (Drawer without Action Bar) link above. – Pranav Mahajan Feb 13 '14 at 06:28
  • If you don't want the features of action bar, I would suggest Drawer without Action Bar(or your own action bar). The set-up is much much easier. I'm using that right now in a project with a very complex drawer from both the sides & its working fine. – Pranav Mahajan Feb 13 '14 at 06:34
  • actually i created a navigation drawer wherein inside is an expandablelist. its working well, but my problem is how do i change the title since o te navigation drawer since i am on the billing activity now from home activity. so i gto two title for navigation drawer, billing and home – user3243065 Feb 13 '14 at 06:37
  • Look, if you are not using the default action bar(or using your own action bar) you can always have a TextView inside the drawer or your activity view. On fragment change, you can change the TextView label. – Pranav Mahajan Feb 13 '14 at 07:08
  • Your main page can have a static view (a button & a TextView). Below your static view you can have a which keeps getting replaced. – Pranav Mahajan Feb 13 '14 at 07:13
  • can you give me some example?pls – user3243065 Feb 13 '14 at 08:15
  • Can't post code in comments. Make an xml with Drawer Layout as root layout. Within that make 2 layouts(can be any, relative or linear). The second one is your left drawer. The first one is your main page. In the first one make a button to open the left drawer, & one fragment layout which can be replaced again & again. The button to open the drawer can be placed in such a way that it looks like a part of the action bar. – Pranav Mahajan Feb 13 '14 at 12:00
0

Please fallow below steps

  1. You should keep the navigation drawer code in BaseActivity(a super class for all the activityes in your project which requires navigationDrawer) and set the layout required for navigationDrawer in onCreate() of BaseActivity.

  2. Extend the BaseActivity for all the activities which requires navigationDrawer.

  3. override onCreate method in your Activity and dont forgrt to write super.onCreate() which will create the layout required for navigationDrawer and then add your activity specific layout to the navigationDrawer layout which is created.

Ravitheja
  • 5
  • 7