Android Basics – Phone vs Tablet – Which Layout the Device is using ?

In the last Blog Post you learnt how to create device specific resources like Layout, Menu, Images etc.

On a phone the Employee App will display list of Employees initially and on click of an employee the DetailActivity will be called. Whereas on a Tablet the List and the Detail is shown next to each other.

Question is how to achieve this?

If you take a look at the Layout XML file you will find that for a Tablet you have a FrameLayout.

So you can check in the Activity class that if this attribute exists then its a tablet. If not then call the DetailActivity.

Source Code – Github

Follow the video to understand this better.

Android Basics : Phone vs Tablet – Screen specific Layouts

Most of the time your App will have a different layout on a Phone vs a Tablet. On a device with a larger screen, like a Tablet, you will want to use the extra space.

In your Employee App it will be better to display only list of Employees on a Phone. While on a Tablet you can display Employee List and Employee Details side by side.

So how can you achieve this ?

In Android you can have resources like Layouts, Images etc. specific to Screen Size, Orientation and Aspect Ratio. For this you need to create specific folder and then place the
resource in that.

For example you need to create two layout xml files. One for Phone and one for Tablet. For this you need to create a layout-large folder and put the tablet layout file in that.

The options you have can be categorized into –

Resource Type – Screen Size – Screen Density – Orientation – Aspect Ratio

Resource Type can be layout, menu etc.

Screen Size can be small, normal, large, xlarge

Screen Density can be ldpi, mdpi, hdpi etc.

Orientation can be land, port.

Now you can create folders using these parameters like layout-large.

Follow the video to understand this better.