Yes, you can. It's simple! I do this because legacy (pre API Level 13) systems do not support the newer layout folder filters. Specifically, I need to distinguish QVGA (small) from WQVGA (normal) and other normal resolutions.
Just add a variety of layout files to your layout and layout-land folders, e.g.
layout
my_layout_default.xml
my_layout_qvga.xml
layout-land
my_layout_default.xml
my_layout_qvga.xml
Then, select the appropriate layout at run time:
protected void onCreate(Bundle savedInstanceState)
{
// Some code that uses DisplayMetrics to determine the screen type
ScreenType st = Util.getScreenType(this);
super.onCreate(savedInstanceState);
setContentView(st == ScreenType.ST_QVGA ? R.layout.my_layout_qvga : R.layout.my_layout_default);
// ...
You can add a tag to each layout to discover which one was actually loaded, if you like, e.g.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_root_view"
android:tag="st_port_QVGA"
FrameLayout flRoot = (FrameLayout)findViewById(R.id.my_root_view);
Object objTag = flRoot.getTag();
if (objTag instanceof String)
{
String strTag = (String)objTag;
System.out.println("Tag = " + strTag);
}
BTW, there's no reason why you cannot combine this technique with the usual resource qualifiers, e.g. add files like my_layout_default.xml to other folders.
R.layout.my_layout_qvga
was chosen? the one inlayout
? orlayout-land
? – Swaim