1

Good day, I would like to ask what's the difference between creating a tablet layout variation and creating a folder for layouts (like: "res/layout-w600dp/" )?

I'm so sorry I'm new in making android app for small (cellular phones/android phones) together with large(tablets) devices.

Can anyone help me, please? I'm really having a trouble on what to do to have layout for tablets. I don't know if the layout for tablets will be automatically displayed when app is run on tablet or I have to put some code in the java part. I've read some documentations but I can't fully understand it. sorry.

Thank you for your help.

KoralReef
  • 41
  • 7

1 Answers1

0

If you have two layout files:

res/layout/
    activity_main.xml

res/layout-sw600dp/
    activity_main.xml

And you have an activity that calls setContentView(R.layout.activity_main), you don't have to do anything else: the first version will be loaded on phones and the second will be loaded on tablets.

If each layout file has views with the same ids (maybe they're just arranged or sized differently), then you can just do whatever logic you want and it will work with both.

Normally, though, the tablet file will include views that the phone version does not. In that case, you should check to see if those views exist before trying to use them. Imagine that the tablet version has a view android:id="@+id/right_panel" and the phone version does not. You might write:

View rightPanel = findViewById(R.id.right_panel);

if (rightPanel != null) {
    // do something for tablets
}
Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • Oh, I see. just some clarification sir will creating a layout for tablet is the same as creating a new layout? or I need to create tablet layout variation?. by the way Sir thank you so much Sir for hearing my concern Sir. – KoralReef Oct 20 '18 at 14:55
  • What do you mean by "creating a new layout" vs "tablet layout variation"? I'm happy to help, I'm just not sure what you're asking – Ben P. Oct 20 '18 at 15:41
  • Hello Sir Ben thank you so much for your help. I was really confuse on how to create layout for tablet and with regards to layout directories. I thought layout for tablets could be created either by creating a new xml layout or by clicking "create tablet layout variation" in the design mode(?) (xml file mode?: text or design). – KoralReef Oct 20 '18 at 15:54
  • Ah. I'm not sure about those Android Studio options, but as long as you have a file in a `layout-swXXXdp` folder with the same name as your file in the `layout` folder, everything will work. – Ben P. Oct 20 '18 at 16:15
  • Sir Ben thank you thank you very much for the help Sir. Because of your help I get to understand more about layouts for tablets. Thank you again Sir and have a nice day ahead Sir Ben. – KoralReef Oct 20 '18 at 22:33