Using configuration qualifiers – mini app

Create a new project and call it Configuration Qualifiers and follow the next steps:

  1. Right-click on the res folder in the project explorer and navigate to New | Android resource directory. Type layout-land and click on OK.

    Tip

    If you don't see your new directory in the project explorer, you need to change the view of the project explorer through its drop-down selector at the top of the project explorer window. Click on the drop-down selector and choose Project.

  2. In the layout_main.xml file, change the text of the default TextView widget from Hello world to Hello portrait!.
  3. Right-click on the 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!.
  4. Run the app and rotate the device between landscape and portrait orientations. Note that the OS automatically uses the appropriate version of 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.

The configuration qualifier's summary

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.18.4