Secondary Display Control via Android Presentation Class

Save

In this blog, we will learn about Secondary Display Control via Android Presentation Class.

Well, the title line certainly deals with a lot of words that probably most of the developers are not aware of and frankly, even I was not aware of this beauty of Android until last month.

Before we start, let me just tell you a few practical use cases for what you can build after following along.

After reading this blog you will also be able to build something similar.

Before we start talking about code let’s just clear what the words in the title are.

Display

A display is referred to as a screen or area which can show the content.

As a result, an Android device can control any number of displays.

The main point to note is that the application’s code should be able to handle each display separately and these are in sync with the main display.

Main Display

This display refers to the actual screen provided by the manufacturer of the Android device.

Hence, this screen listens to all the user interactions, gestures and motions events.

As a result, this display is referred to as Main Display.

Secondary Display

This display refers to a secondary screen which is somehow (Bluetooth, wired connections, RFC) connected to your Android device.

By Default, this screen is not user interactive and hence it is called as Display and not Screen.

Presentation Class

A presentation is a special kind of dialog whose purpose is to present content on a secondary display. A Presentation is associated with the target Display at creation time and configures its context and resource configuration according to the display’s metrics.

— developer.android.com

For Simple understanding, we need to extend this class to build a view to display on our Secondary Display.

With this understanding, now you can do anything you want on Secondary Display.

Now, in this blog, we will just be making a Welcome TextView on the Secondary Display.

APPROACH :

CODE :

File Name –> secondary_display.xml

File Name –> SecondaryDisplay.java

With the help of lines below, you can set the Secondary Display Content.

Last Step:

With this last change, you have completed the task.

Now, just run your application on a device with  Secondary Display and see the magic.

Keep coding and Keep Sharing 🙂

References –> https://developer.android.com/reference/android/app/Presentation

https://developer.android.com/reference/android/view/Display.html

. . .

Leave a Comment

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


27 comments

  • Anish Hegde
    Is there any software solution to test the code without any multi display harware.I mean any virtual device or something?
    • anchit (Moderator)
      Hello Anish,

      You can easily try this option in any android device or emulators.

      All you need to do is to enable the developer options.

      Then in the developer options menu find the option “Simulate secondary displays”.

      From the list that opens up , you can easily select the width option as per your need.

  • Kim
    Good Job!
    Thank you!!!
    How to controll secondary display?
    • anchit (Moderator)
      Hello,

      I am assuming that by control you want to listen to some of the events on the secondary display.

      So, if that is all about majorly click listeners then you can add them as you add it to any of the views in your code in your secondary display class as well.

      Though, I have not practically implemented any listeners for the secondary display.

      But i guess, this should work fine.

      Still, if you are looking for soemthing else, then feel free to type more over here.

      Regards,
      Anchit

      • Sunil
        Hello Anchit,
        I tried solution suggested by you, but its not working. I want handle on click event of view. So I set click listener on button, but not executing.

        If you know the solution then please let me know.

        Thanks,
        Sunil

        • anchit (Moderator)
          Where is that button ?

          I would need access to your code and then only i can help you out.

  • Suheel Yousuf Wani
    Hello Anish
    Very well explained.
    I have a query, How to send data from main activity to 2nd display, for example if we type some thing on main activity, the same should be get displayed on 2nd display
    • anchit (Moderator)
      Hi,

      This seems to be pretty easy.

      You can either create some method in your secondary display class and update the ui accordingly.

      Or

      You can declare the components public and then access the same in the main activity.

      Both will work.

  • Shejal Dekate
    Hi Anchit,
    I want to have two screens for automotive AVD and transfer the information of the main screen to the secondary screen. How will this connection be made? Also, may I get your mailing information?
    • anchit (Moderator)
      Hi,

      I personally have not worked on automotive AVD.

      As per me the connection to secondary screen in AVD will depend totally on how both of the screens are connected.

      If the connection is physically wired, then you can also go with the points mentioned in the article.

      Regarding mailing details, please do send your query to [email protected]

  • Allison
    Hi, I have multiple activities in my app. I just want to hide my main display for most of the activities. Only in payments screen I want to show some data in second display. For remaining I can just show images or videos.

    To do this should I call presentation in all activities? Or can I call it from base activity?

    • anchit (Moderator)

      i would recommend you to call in the base activity.

      when you need to change the content on the secondary display just override the activity.

  • Snehal
    HI Anchit,
    I want to connect android device to display screens via HDMI and content from android will shown on screen will this code worked for me?
    • anchit (Moderator)
      hi snehal,
      Yes, the code in the blog above should work for you.
      • Snehal
        Thank you
        • anchit (Moderator)
          your welcome
  • Vans
    you are genius! I used your example, imported, thought here goes nothing, and it worked!!! Thank you so muchhhhh!!!
    • anchit (Moderator)

      your welcome

  • Sailee Shuddhalwar
    Hi Anchit,
    I am getting this error
    Unable to add window [email protected] — the specified display can not be found

    while running this example on emulator.
    looking forward for your help.

    • Sailee Shuddhalwar
      Hi Anchit
      Can you please help me out, I found this example very helpful but not able to run this on emulator .
      it would be great if you give me some of your suggestions.
      Thanks you.
      • anchit (Moderator)

        Hi,

        For both points, please first check that the secondary display is fully simulated on your emulator.

        If you are able to successfully simulate the dual-screen, then the article is good enough to work like a charm.

        • Govind
          I have enabled “Add secondary display” in the emulator and I can see 2 displays being launched. But not able to simulate the secondary activity on the second display, followed the above piece of code . Could you please help here?
          • anchit (Moderator)
            I will need to have a look into the code that you are working on.

            Please do share the same.

  • Avanindra Dubey
    Hi Anchit, described in very concise way. Helped a lot.
    Keep doing such things. Thanks.
    • anchit (Moderator)

      Your Welcome

  • pravin
    Can we capture user input/interaction? if yes please add some samples.

    also my project is library based where one base project and other modules are divided into libraries. so how to handle this kind of structure?

    • anchit (Moderator)

      Hi there,

       

      Yes, we can capture user input and interaction if the secondary hardware supports it.

      Sorry, I can’t share samples right now but this is possible with simple logic.

      For library-based project, i would recommend adding this functionality to the base project itself so that you can use it on the go in different libraries as per the use case.

      Rest all can be done as per your project’s requirement.

  • css.php

    All communication is done through a ticket system. I appreciate the regular almost daily updates of what is being worked on, and communicating changes or updates is easy through this system. Mobikul's response timeframe is quick.

    Josh Arnold

    Josh Arnold

    Owner, Shop ATV Escape

    Talk to Sales

    Global

    Live Chat
    Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home