I'm developing an app which will use the navigationDrawer. I think I set all the things right, but the sidebar doesn't contains my items. How can I make this work?
Main.java:
private DrawerLayout drawerLayout;
private ListView drawerList;
private ActionBarDrawerToggle drawerToggle;
private String appTitle;
// slide menu items
private String[] menuTitles;
private TypedArray menuIcons;
private ArrayList<DrawerItem> drawerItems;
private DrawerAdapter drawerAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
setContentView(R.layout.activity_main);
appTitle = getResources().getString(R.string.app_name);
createDrawer();
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
}
/**
* Displaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new SecondFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_container, fragment).commit();
// update selected item and title, then close the drawer
drawerList.setItemChecked(position, true);
drawerList.setSelection(position);
setTitle(menuTitles[position]);
drawerLayout.closeDrawer(drawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// display view for selected nav drawer item
displayView(position);
}
private void createDrawer(){
// load slide menu items
menuTitles = getResources().getStringArray(R.array.drawer_items);
// nav drawer icons from resources
menuIcons = getResources().obtainTypedArray(R.array.drawer_icons);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.list_slidermenu);
drawerItems = new ArrayList<DrawerItem>();
for (int i=0; i<menuTitles.length; i++){
drawerItems.add(new DrawerItem(menuTitles[i], menuIcons.getIndex(i)));
}
menuIcons.recycle();
drawerAdapter = new DrawerAdapter(context, drawerItems);
drawerList.setAdapter(drawerAdapter);
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
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) {
getActionBar().setTitle(appTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(appTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
drawerToggle.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
drawerToggle.onConfigurationChanged(newConfig);
}
main_activity.xml:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="idlike.doctor.Main" >
<FrameLayout
android:id="@+id/fragment_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="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:divider="@color/Blue"
android:dividerHeight="1dp"
android:background="@color/Black"/>
</android.support.v4.widget.DrawerLayout>
drawer_item.xml:
<?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="wrap_content"
android:orientation="horizontal"
android:background="@drawable/drawer_selector" >
<ImageView
android:id="@+id/icon"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_gravity="center_vertical" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="@color/Orange"
android:textSize="30sp"
android:gravity="center_vertical" />
</LinearLayout>
[SOLVED] I wrote a line adding the items to the adapter:
for (int i=0; i<menuTitles.length; i++){
drawerItems.add(new DrawerItem(menuTitles[i], menuIcons.getIndex(i)));
}
becomes:
for (int i=0; i<menuTitles.length; i++){
drawerItems.add(new DrawerItem(menuTitles[i], menuIcons.getResourceId(i, -1)));
}
this error should prevent the icons to be displayed but for some reason even the text was not displayed....