Wednesday, September 16, 2015

"Hello world" in Smart Tv - Creating an Android TV Sample Project

In the final part of this tutorial, we'll take a first-hand look at some TV-ready code by creating and testing a basic Android TV project.
Before you can develop anything for the Android TV platform, make sure you've updated your SDK to Android 5.0 (API 21) or higher, and your SDK tools to version 24.0.0 or higher.
Once you're up to date, it's time to create your app:
  1. Launch Android Studio.
  2. Select Start a new Android Studio Project.
  3. Give your project a name and a domain. Click Next.
  4. Select TV, and then deselect all the other checkboxes. Although you can create Android TV projects that have a smartphone, tablet and/or Android Wear module, for the sake of simplicity we'll be creating a single-module project. Click Next.
Ensure only the TV checkbox is selected
5. Select Android TV Activity and click Next.
6. Stick to the default settings and click Finish.

Accept all the default activity layout and fragment names and click Finish

Android Studio will then create your project.
Your Android TV projects layout should feel instantly familiar
Now you've created your sample project, we'll take a line-by-line look at the Android Manifest, as this file contains lots of TV-specific code.
Note that although the majority of this code is generated automatically when you create an Android TV project, I've made some minor additions and adjustments that are all clearly marked in the text.
As already mentioned, the android.permission.RECORD_AUDIO permission implies that your app requires android.hardware.microphone. So this line of code effectively prevents your app from being installed on devices that don't have access to a microphone. If your app can function without a microphone, you should make the following addition:
Adding the above code means that users can install your app on devices that don't have access to microphone hardware.
While touchscreen support is found on many Android-powered devices, this isn't the case with Android TV. Your Android TV app must declare that it doesn't requiretouchscreen support.
The above snippet declares that your app uses the Leanback interface we discussed earlier.
If you want your app to run on non-TV devices where Leanback isn't supported, you'll need to change the above line to android:required="false".
The above addition applies the Leanback theme to your project.
App banners represent your app on the Android TV homescreen and are how the user launches your app. Your app banner must be a 320px x 180px xhdpi image and should include text.
If you want to use the same banner across all your activities, you should add theandroid:banner attribute to your manifest's <application> tag as I've done here. If you want to provide a different banner for each activity, you'll need to add anandroid:banner attribute to all of your application's <activity> tags instead.
This snippet declares a launcher activity for TV.
The next step is testing how your app functions from the user perspective. Even if you have access to a physical Android TV, you'll want to test your project across multiple devices, so you should always create at least one Android TV AVD.
To create your AVD:
  1. Launch the AVD Manager, either by clicking the AVD Manager button in the toolbar or by selecting Tools Android AVD Manager.
  2. Click Create New Virtual Device.
  3. Select the TV category.
  4. Choose one of the Android TV devices listed and click Next.
  5. Select your system image and click Next.
  6. Give your AVD a name and click Finish.
To test your sample project, select Run > Run app, followed by your TV AVD. Once the AVD has finished loading, you'll see the Android TV user interface with your app's banner in the bottom-left corner.
Youll see your apps banner in the bottom-left corner of the Android TV user interface
To launch your app, click the banner image. After a short delay, your app will appear in the AVD window.
Your app is split into categories along the left hand side and content along the right

No comments:

Post a Comment