Multiple Displays

Multiple Displays – Part 4

In the previous article we got our app (which shows the characteristics of our displays) backwardly compatibile, but it still lacks the ability to handle connection and disconnection of an external display while the MainActivity is running. In this article we’ll add the ability to do precisely that.

Detecting display connection changes is actually pretty easy, but it is important to do it in order to properly clean things up. It is for this reason that I’ve marked the code, up to this point, as not being fit for production. To get notified of display connection changes we need to register an appropriate listener on our DisplayManager. Let’s begin by defining our listener, which implements the DisplayListener interface:

Once again, DisplayListener is specific to SDK 17 and later, so we need the appropriate annotation. Also we need to ensure that it only on devices running 4.2 or higher, so we’ll create and register it in our multiInit() method:

We should also clean up after ourselves, and unregister the listener in onDestroy() including the appropriate version checks and annotations, of course:

Our app will now correctly handle the connection and disconnection of the external display.

Another thing that we’ll now look at is why the external display has a different theme to the main display, and how we can change this. It is worth remembering that Presentation actually extends Dialog – so is an implementation of DialogFragment. Normally you can control the themeing of a Dialog by using the appropriate constructor, and the same applies to Presentation. What we need to do is change the existing constructor to include the theme option:

Then we just need to change wherever this is instantiated to pass in an appropriate theme:

The neat thing about this is that it enables us to us a slightly different theme on the presentation display. For example, in this case I have used android.R.style.Theme_Holo_Light_NoActionBar because I don’t want the ActionBar to show. Had I used android.R.style.Theme_Holo_Light the ActionBar would display on the external presentation display. Normally we won’t want to show the ActionBar on an external display because it is not a touch screen, so the concept of an ActionBar (which is effectively an interactive control) appearing on a non-interactive display seems a little strange. Having said that, the ActionBar implementation in this app does not actually contain any interactive elements, but that’s not really an issue, here.

Android always seems to have multiple ways of doing certain things, and in the final article in this series we’ll have a look at an alternate mechanism for managing external displays.

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 4 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.

3 Comments

  1. Hey Mark,

    “Presentation actually extends Dialog – so is an implementation of DialogFragment”
    Presentation extends Dialog – yes, but Dialog is not an implementation of DialogFragment, isn’t it?

    Have a great day
    Tom

Leave a Reply

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