I am implementing a very simple example of NavigationDrawer
. The example works fine, but I have a strange problem. The icon for NavigationDrawer (generally ic_drawer
) does not show up in the ActionBar
. I have tried using different icon images, but didn't achieved the desired result. Here's the code. The project has only one activity named MainActivity.java
.
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mListView;
private ActionBarDrawerToggle mDrawerToggle;
private String[] array = new String[] { "Hello", "hola", "namaste",
"salaam" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
mListView = (ListView) findViewById(R.id.list);
getActionBar().setDisplayHomeAsUpEnabled(true);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_dropdown_item_1line, array);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getApplicationContext(), array[arg2],
Toast.LENGTH_SHORT).show();
}
});
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.open, R.string.close) {
@Override
public void onDrawerClosed(View drawerView) {
// TODO Auto-generated method stub
invalidateOptionsMenu();
}
@Override
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if (mDrawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
}
And if I comment out the line with code getActionBar().setDisplayHomeAsUpEnabled(true);
, the code stops working, meaning NavigationDrawer
does not open.
For reference, the xml layout for this activity is:
activity_main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="change aaaega, hum laaenge" />
<ListView
android:id="@+id/list"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
Any suggestions ?
Thanks.