I am working on an android project and I'm trying to integrate the new Navigation Drawer using the example from http://developer.android.com/training/implementing-navigation/nav-drawer.html.
Its mostly working except for one thing, the action bar up button doesn't display the menu, but if I swipe my finger over the main activity screen from the edge, then the menu appears, so I know there's nothing wrong with the actual menu, its just the action bar button.
Below is the code
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mTitle;
private CharSequence mDrawerTitle;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList = (ListView)findViewById(R.id.left_drawer);
MenuItemAdapter menuAdapter = new MenuItemAdapter(this);
menuAdapter.add(new MenuItem("Hello"));
menuAdapter.add(new MenuItem("World"));
menuAdapter.add(new MenuItem("Parsnips"));
menuAdapter.add(new MenuItem("Turnips"));
mDrawerList.setAdapter(menuAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_closed)
{
public void onDrawerClosed(View view)
{
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView)
{
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null)
{
selectItem(0);
}
}
@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;
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
android.app.Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARGS_PLANET_NUMBER, position);
fragment.setArguments(args);
android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title)
{
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
public static class PlanetFragment extends android.app.Fragment
{
public static final String ARGS_PLANET_NUMBER = "planet_number";
public PlanetFragment()
{
}
@Override
public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflator.inflate(R.layout.fragment_planet, container, false);
int i = getArguments().getInt(ARGS_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];
//((TextView)rootView.findViewById(R.id.fragment_text_view)).setText(planet);
getActivity().setTitle("Planet: " + planet);
return rootView;
}
}
private class MenuItem
{
public String menuName;
public MenuItem(String menuName)
{
this.menuName = menuName;
}
}
public class MenuItemAdapter extends ArrayAdapter<MenuItem>
{
public MenuItemAdapter (Context context)
{
super(context,0);
}
public View getView (int position, View convertView, ViewGroup parent)
{
convertView = null;
if (convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_menu_item, null);
}
/*TextView title = (TextView)convertView.findViewById(R.id.row_title);
title.setText(getItem(position).menuName);
title.setVisibility(View.VISIBLE);*/
EditText title = (EditText)convertView.findViewById(R.id.row_edittext);
title.setHint(getItem(position).menuName);
title.setVisibility(View.VISIBLE);
return convertView;
}
}
}
Thanks for any help you can provide.