Multiple Displays

Multiple Displays – Part 1

At the time of writing, Android 4.2 has just been released and this contains an interesting new feature which enables us to independently control multiple displays. In this series of articles we’ll look at the new APIs and see how we can control different displays independently.

The basis for our multi-display app will be a simple utility running which will display the characteristics of the display. So before we dive in to the multi-display APIs let’s get this simple utility working.

Let’s start by creating a new app named DisplayCharacteristics with a package name of com.stylingandroid.displaycharacteristics, and with a minSdk level of 7 (Donut) and a targetSdk of 17 (Jelly Bean MR1). At this point you may be thinking: Why set a minSdk of 4 when the new APIs are in 17? The reason being we want the utility to be backwardly compatible, and we’ll demonstrate some techniques for doing this within the articles.

The first thing that we ned to do is define our layout. We want to display some basic information about the display, in table form so we’ll create this in res/layout/activity_main.xml:

This should be pretty self-explanatory, but please refer to the TableLayout article for more information.

We’re now going to make full use of the Android resource management framework to do some of the work for us. The first two fields are for the pixel density and display size. What we’ll do is create strings.xml in each of values-ldpi, values-mdpi, values-hdpi, values-xhdpi, & values-xxhdpi. Each of these will contain a single string named density, the value of which will match the pixel density. For example, in values-mdpi it will be:

We’ll do a similar thing for a string named size in values-small, values-normal, values-large, & values-xlarge which will have a value matching the screen size. For example values-xlarge will be:

The thing worth noting is that the names of these strings correspond to the first two fields in our layout. This means that at runtime, the resource management framework will automatically select the correct string based upon the display for which the layout is being inflated. So a chunk of the work has already been done for us!

The remaining fields will need to be connected up manually. We do this in our MainActivity class:

All that we’re doing here is grabbing the display metrics, and populating the TextViews accordingly. We’ve implemented this as a private method as we’ll need to reuse it later on when we come to connecting up multiple displays.

If we run this we can see the following characteristics on a Galaxy Nexus:

On A Nexus 7:

And on a Nexus 10:

Rotating the displays on these devices show that everything is recalculated following device rotation, although for reasons of brevity I shall not include screen shots here.

So we now have a basic app which shows the characteristics of the main display. If you connect the HDMI out from the Nexus 10 to an external display, it will simply mirror the main display, and the characteristics shown will be identical. In the next part of this series we’ll begin looking at how we can connect up external displays so that they behave independently from the main display.

The source code for this article can be found here.

© 2012, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 Multiple Displays – Part 1 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *