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.
- POS Hardware Machines & their Dedicated Application –> Have you seen the agents at big retail centers using POS machines?
There you can see what is being added to your cart at the same moment and the agent is processing your order.
If you peek at agent’s screen the content will be a bit(or entirely) different. - Presentation Apps –> Have you seen the applications where whole presentation(ppts, slides) are being controlled by some android device?
If you peek at the Android device screen the slides are bit different (like with notes, descriptions, etc) but on the secondary screen (like a projector ) the presentation is entirely different.
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 :
- First, create a layout file that will be displayed on the secondary Display. For simplicity, name this file secondary_display.xml
- Now, we create a class that will extend the Presentation Class. Let’s name this class SecondaryDisplay.
- The Presentation class object we just created will have a default constructor and override the onCreate Method.
- The constructor will take Context & a display as arguments. This will be a reference to the secondary Display.
- The onCreate Method is similar to the Activity’s onCreate Method.
- Now to display this content, you just need to initialize the constructor of the SecondaryDisplay class and call the show() method. (just like we do for a dialog).
CODE :
File Name –> secondary_display.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SecondaryDisplay"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="8dp" android:padding="4dp" android:text="Hello, Presentation Api, \nThis text will be displayed on the secondary display" android:textAppearance="?android:attr/textAppearanceMedium" /> </RelativeLayout> |
File Name –> SecondaryDisplay.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class SecondaryDisplay extends Presentation { /** * Creates a new presentation that is attached to the specified display * using the default theme. * * @param outerContext The context of the application that is showing the presentation. * The presentation will create its own context (see {@link #getContext()}) based * on this context and information about the associated display. * @param display The display to which the presentation should be attached. */ public SecondaryDisplay(Context outerContext, Display display) { super(outerContext, display); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.secondary_display); } } |
With the help of lines below, you can set the Secondary Display Content.
Last Step:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class MainActivity extends AppCompatActivity{ public DisplayManager displayManager = null; public Display[] presentationDisplays = null; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); displayManager = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE); if (displayManager!= null){ presentationDisplays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION); if (presentationDisplays.length > 0){ SecondaryDisplay secondaryDisplay = new SecondaryDisplay(MainActivity.this, presentationDisplays[0]); secondaryDisplay.show(); } } } } |
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