AndroidStudio project that supports both Android and AndroidTV
android, android-tv
Solution
Yes, it is possible to have one APK file for both phone/tablet and Android TV app because TV apps use the same structure as those for phones and tablets. This similarity means you can modify your existing apps to also run on TV devices or create new apps based on what you already know about building apps for Android.
Before you begin building apps for TV, you must: Update your SDK tools to version 24.0.0 or higher. The updated SDK tools enable you to build and test apps for TV. Update your SDK with Android 5.0 (API 21) or higher. The updated platform version provides new APIs for TV apps. Create or update your app project. In order to access new APIs for TV devices, you must create a project or modify an existing project that targets Android 5.0 (API level 21) or higher.
- Declare a TV Activity
An application intended to run on TV devices must declare a launcher activity for TV in its manifest using a CATEGORY_LEANBACK_LAUNCHER intent filter. This filter identifies your app as being enabled for TV, and is required for your app to be considered a TV app in Google Play. Declaring this intent also identifies which activity in your app to launch when a user selects its icon on the TV home screen.
If you are modifying an existing app for use on TV, your app should not use the same activity layout for TV that it does for phones and tablets. The user interface of your TV app (or TV portion of your existing app) should provide a simpler interface that can be easily navigated using a remote control from a couch. For guidelines on designing an app for TV, see the TV Design guide. For more information on the minimum implementation requirements for interface layouts on TV, see Building TV Layouts.
The following code snippet shows how to include this intent filter in your manifest:
<application
android:banner="@drawable/banner" >
...
<activity
android:name="com.example.android.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.android.TvActivity"
android:label="@string/app_name"
android:theme="@style/Theme.Leanback">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
</activity>
</application>
- Declare Leanback support
Declare that your app uses the Leanback user interface required by Android TV. If you are developing an app that runs on mobile (phones, wearables, tablets, etc.) as well as Android TV, set the required attribute value tofalse. If you set the required attribute value to true, your app will run only on devices that use the Leanback UI.
<manifest>
<uses-feature android:name="android.software.leanback"
android:required="false" />
...
</manifest>
- Declare touchscreen not required
The manifest of your TV app must declare that a the android.hardware.touchscreen feature is not required. This setting identifies your app as being able to work on a TV device, and is required for your app to be considered a TV app in Google Play. The following code example shows how to include this manifest declaration:
<manifest>
<uses-feature android:name="android.hardware.touchscreen"
android:required="false" />
...
</manifest>
- Provide a home screen banner
An application must provide a home screen banner for each localization if it includes a Leanback launcher intent filter. The banner is the app launch point that appears on the home screen in the apps and games rows. Describe the banner in the manifest as follows:
<application
...
android:banner="@drawable/banner" >
...
</application>
Use the android:banner attribute with the tag to supply a default banner for all application activities, or with the tag to supply a banner for a specific activity.
See Banners in the UI Patterns for TV design guide.
- Add TV Support Libraries
The Android SDK includes support libraries that are intended for use with TV apps. These libraries provide APIs and user interface widgets for use on TV devices. The libraries are located in the/extras/android/support/ directory. Here is a list of the libraries and their general purpose:
- v17 leanback library - Provides user interface widgets for TV apps, particularly for apps that do media playback.
- v7 recyclerview library - Provides classes for managing display of long lists in a memory efficient manner. Several classes in the v17 leanback library depend on the classes in this library.
- v7 cardview library - Provides user interface widgets for displaying information cards, such as media item pictures and descriptions.
Problem
I'm trying to create an AndroidStudio project that supports both Android (phone/tablet) and AndroidTV. The goal is to have similar functionality on the Phone/tablet and AndroidTV, while one not needing the other to operate, and reusing code. When I create a new project, do I: - a) Put a check mark on both "Phone and Tablet" as well as "TV"? - b) Check mark on "Phone and Tablet" and retrofit based on this guide: https://developer.android.com/training/tv/start/start.html - c) Check mark on "TV" and retrofit a Main Activity? I've tried b) but both the phone (5.0) and ADT-1 just get the same layout. Below is my manifest file (MainActivity is placed before MainActivityTV). When I run it, both phone and ADT-1 load the phone layout. But, when I put MainActivityTV before MainActivity, both my phone and ADT-1 load the leanback layout. How do I make it so that phone only loads MainActivity, and the ADT-1 only loads MainActivityTV? ``` <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.androidretrofitwithtv" > <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-feature android:name="android.hardware.touchscreen" android:required="false" /> <uses-feature android:name="android.software.leanback" android:required="false" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:banner="@drawable/app_icon_your_company" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivityTV" android:label="@string/app_name" android:configChanges="keyboard|keyboardHidden|navigation" android:theme="@style/Theme.Leanback"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LEANBACK_LAUNCHER" /> </intent-filter> </activity> <activity android:name=".DetailsActivity" /> <activity android:name=".PlaybackOverlayActivity" /> <activity android:name=".BrowseErrorActivity" /> </application> </manifest> ```