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:
[xml]
[/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:
[xml]
[/xml]
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:
[xml]
[/xml]
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:
[java] public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populate(findViewById(R.id.main), getWindowManager()
.getDefaultDisplay());
}
private static void populate(View v, Display display)
{
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
float density = metrics.density;
TextView actual =
(TextView) v.findViewById(R.id.actual);
if (actual != null)
{
actual.setText(String.format(“%dx%d”,
metrics.widthPixels,
metrics.heightPixels));
}
TextView df =
(TextView) v.findViewById(R.id.density_factor);
if (df != null)
{
df.setText(String.format(“%f”, density));
}
TextView dp =
(TextView) v.findViewById(R.id.device_pixels);
if (dp != null)
{
dp.setText(String.format(“%dx%d”,
((int)((float) metrics.widthPixels / density)),
((int)((float) metrics.heightPixels / density))));
}
}
}
[/java]
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.
Copyright © 2012 Styling Android. All Rights Reserved.
Information about how to reuse or republish this work may be available at http://blog.stylingandroid.com/license-information.
I’ve also written a demo app for this to show how resources work in different configurations. The app is here: https://play.google.com/store/apps/details?id=nl.qbus.sizemeup and the source is here: https://bitbucket.org/hvisser/sizemeup
It might be helpful in the context of this post too!