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?

  • 1
    At a high level, the standard answer I know is: "use Model-View-Controller (or some variant thereof), with one View for phones and another View for tablets." If only one View has to deal with a DrawerLayout, that's fine. – Ixrec Mar 7 '15 at 20:56

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.