Reputation: 13
Is there a way to select a different layout for an Android Activity based on the type of the device?
I try to be more clearer. I have two layout: home_layout_phone.xml and home_layout_tablet.xml. They are equal but the objects have different proportions, and in the HomeActivity.java I want to do something like this:
setContentView(R.layout.home_layout_phone.xml); //if the device is a phone
setContentView(R.layout.home_layout_tablet.xml); //if the device is a tablet
How can I do this? I'm using AndroidStudio 1.5.1. Thanks for your answers and sorry for my bad English, I'm Italian.
Upvotes: 1
Views: 1222
Reputation: 1311
You can have multiple layout for several device configurations by using configuration qualifiers.
Android supports several configuration qualifiers that allow you to control how the system selects your alternative resources based on the characteristics of the current device screen. A configuration qualifier is a string that you can append to a resource directory in your Android project and specifies the configuration for which the resources inside are designed.
You may have something like:
List item layout/home_layout.xml (for phones)
List item layout-xlarge/home_layout.xml (for bigger devices)
http://developer.android.com/intl/es/guide/practices/screens_support.html#qualifiers
Upvotes: 0
Reputation: 944
Use one layout file name. For instance, home_layout.xml
. This will be your phone layout. Now to create a different layout for a tablet, in Android Studio right-click the layout folder and select New > Layout resource file. Name this file the same name (home_layout
) and under available qualifiers, select how you will determine what is a tablet. For instance, you can choose when a width exceeds 600 dp. As seen below:
When you setContentView, simply:
setContentView(R.layout.home_layout);
And the proper layout will be chosen based on the width of the device in this case.
Upvotes: 1