Reputation: 29
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>
Upvotes: 1
Views: 6342
Reputation: 2108
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)
Upvotes: 0
Reputation: 5
Please fallow below steps
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.
Extend the BaseActivity for all the activities which requires navigationDrawer.
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.
Upvotes: 0
Reputation: 2826
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).
Upvotes: 3