Create a new project and call it Configuration Qualifiers
and follow the next steps:
res
folder in the project explorer and navigate to New | Android resource directory. Type layout-land
and click on OK.layout_main.xml
file, change the text of the default TextView widget from Hello world
to Hello portrait!
.layout-land
folder and navigate to New | Layout resource. Name the file layout_main.xml
. Add a single Plain TextView widget and change the text
property to Hello landscape!
.layout_main.xml
.If we got a reference to TextView
(or any other widget) in our Java code, the same exact code would work as long as the id
values in the different layouts were the same.
Effectively, we have two different layouts controlled by the same Java code.
What the last two mini apps and our discussion on configuration qualifiers have shown us is certainly very useful in a number of situations. Unfortunately, however, configuration qualifiers and detecting attributes in code only solve the problem in the view layer of our MVC pattern. As discussed in the Real-world apps section, our apps sometimes need to have a different behavior as well as layout. This perhaps implies multiple branches of our code in the controller layer and perhaps summons nightmarish visions of having huge great if
or switch
blocks with different code for each different scenario.
Fortunately, this is not how it's done. For such situations (actually for most apps), Android has Fragments.
3.144.18.4