I am following this post http://derekrwoods.com/2013/09/creating-a-static-navigation-drawer-in-android/
I want drawer to be opened when in landscape mode. Here is my onCreate function.
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_order);
drawerList = (ListView) findViewById(R.id.left_drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.content_frame);
if (((ViewGroup.MarginLayoutParams) frameLayout.getLayoutParams()).leftMargin == (int) getResources()
.getDimension(R.dimen.drawer_size)) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
drawerLayout.setScrimColor(Color.TRANSPARENT);
isDrawerLocked = true;
}
// Set the adapter for the list view
// drawerItems = getResources().getStringArray(R.array.drawerOptions);
drawerItems = DummyContent.ITEMS
.toArray(new DummyItem[DummyContent.ITEMS.size()]);
drawerList.setAdapter(new ArrayAdapter<DummyItem>(this,
android.R.layout.simple_list_item_1, drawerItems));
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.ic_drawer, R.string.action_short,
R.string.action_short) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
getActionBar().setTitle("test");
// ((FragmentInterface) fragment).showMenuActions();
invalidateOptionsMenu();
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("Select Option");
// ((FragmentInterface) fragment).hideMenuActions();
invalidateOptionsMenu();
}
};
if (!isDrawerLocked) {
drawerLayout.setDrawerListener(drawerToggle);
}
// Set the drawer toggle as the DrawerListener
DrawerItemClickListener drawerItemClickListener = new DrawerItemClickListener();
drawerList.setOnItemClickListener(drawerItemClickListener);
if (!isDrawerLocked) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
Now the problem is whenever my activity is starting in portrait mode, after rotation drawer is collapsible (can be closed using sliding right to left gesture) even after setting DrawerLayout.LOCK_MODE_LOCKED_OPEN. This problem does not occur when opened directly in Landscape mode.
I had to move that code to my onResume():
@Override
public void onResume() {
super.onResume();
Display display = getWindowManager().getDefaultDisplay();
if (display.getRotation() == Surface.ROTATION_90 || display.getRotation() == Surface.ROTATION_270) {
// Landscape
isDrawerLocked = true;
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
} else {
// Portrait
isDrawerLocked = false;
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With