Question

I am building a news app. In phone mode it should have a navigation drawer which slides out when you tap on a hamburger button or slide your finger. That navigation drawer has a fragment with static list in it.

While in a tablet mode, when you rotate the screen to landscape, that fragment with the list would be permanently visible.

What is the general approach to developing these kinds of apps?

If I decide to make a separate layout for the tablet landscape mode without a DrawerLayout, it would probably cause an error because the Navigation Drawer fragment class is referring to a DrawerLayout in XML, and if I decide to have DrawerLayout in tablet-landscape mode and call the openDrawer method every time I rotate the screen to the landscape mode, it would overlay the main content.

How should I handle it?

Was it helpful?

Solution

Every time you rotate the phone, current activity is re-created. That is, onCreate() is called. Just determine whether or the device is

a) Tablet / Smartphone https://stackoverflow.com/questions/9279111/determine-if-the-device-is-a-smartphone-or-tablet

b) Portrait / Landscape https://stackoverflow.com/questions/3674933/find-out-if-android-device-is-portrait-or-landscape-for-normal-usage

Only after determining this should you call setContentView(). Depending on the situation, inflate activity with the appropriate layout.

In your case, when in landscape mode in a tablet, inflate with a layout that does not use DrawerLayout and don't expect DrawerLayout (in the corresponding Java code) because you don't need it. The required layout should just have the fragment containing the list at its left side.

Licensed under: CC-BY-SA with attribution
scroll top