Creating Different Layout for Android Phone and Tablet

Creating different layout for android phone and tablet

Basically you have to make different layouts for both android phone and tablets. Android is smart enough to differentiate. For example for large screen you can just make a new folder namer Layout-large. and put your tablet xml in it. Android will pick xml from here and in case of phone it will pick from simple layout folder.
The configuration qualifiers you can use to provide size-specific resources are small, normal, large, and xlarge. For example, layouts for an extra large screen should go in layout-xlarge/.

I would recommend if both phone and tablet screens and totally different you can make two different apks and load on same Id on google play. This way your application will be light weight and will be be fast. Google play automatically can detect that application is for tablet or or phone.You need not to worry about that.

Layout for tablets in Android

I know this is an old question, but for the sake of it...
According documentation, you should create mutiple asset folders like this

res/layout/main_activity.xml           # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml # For 10” tablets (720dp wide and bigger)

How can i made the layout that will work in both Tablet and phone?

Make your resource like this.

res/layout/my_layout.xml // layout for normal screen size ("default")

res/layout-small/my_layout.xml // layout for small screen size

res/layout-large/my_layout.xml // layout for large screen size

res/layout-xlarge/my_layout.xml // layout for extra large screen size

res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation

res/drawable-mdpi/my_icon.png // bitmap for medium density

res/drawable-hdpi/my_icon.png // bitmap for high density

res/drawable-xhdpi/my_icon.png // bitmap for extra high density

For more reference. reference1.

Add this in your manifest.xml

<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens= "true"
android:anyDensity="true"
/>

Hope this will help you.

How to create a Layout for Tablet

The documentation you are looking for is here . The value you should be using is 600dp as described in the docs

res/layout-w600dp/main_activity.xml # For 7” tablets or any screen with 600dp
# available width (possibly landscape handsets)

So basically you can create a layout-w600dp folder inside of the res directory to be used as the layout for tablets.

Create custom layout for tablet and phone

You have to use different layout folders for tablet and phone.

Default folder is res/layout.

For tablet just create a new folder as res/layout-large, and place tablet mode layout there. Remember these layout xml files need to have the same name. Android system will now look for layout-large folder layouts when application is run under large screens.

You have official documentation about supporting multiple screen sizes (more specifically) here.

<<<<<---------- EDIT : ---------->>>>>

Use under activity to control code under different screen size:

int screenSize = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;

String toastMsg;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
toastMsg = "Large screen";
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
toastMsg = "Normal screen";
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
toastMsg = "Small screen";
break;
default:
toastMsg = "Screen size is neither large, normal or small";
}

Android Studio: multiple layouts, specifically landscape tablet

Click Create Other.. Select Orientation from Available Qualifiers box. Click >> button. In Screen Orientation select Landscape. In directory name paste this : layout-sw600dp-land

A new folder will be created with the same name having activity_main.xml in sw600dp(tablet 7" size) in landscape mode. Configure as per your requirement.

android phone and tablet layout

You can make use of the resource buckets.

Basically you will have to create two layout files with the same name in two different resource folders:

  • res/layout/your_layout.xml: this will be your default layout for any other devices than the below mentioned
  • res/layout-sw600dp/your_layout.xml: this layout will be picked by devices with the smallest width of 600 dp (tablets fall into this category)

The system is intelligent enough to pick the right layout based on the running device as long as you name your layouts the same.

How to separate Android layout for TV and phone/tablet?

How can I provide different layouts for phone and TV?

If this is an official Android TV environment, AFAIK res/layout-television/ should work. See the legendary Table 2.



Related Topics



Leave a reply



Submit