ANDROIDTV first create (go: Support original)

Source: Internet
Author: User

Reprint Address:http://blog.csdn.net/aa2967277/article/details/50617677

Introduction to ANDROIDTV Application development

At present, there is not enough introduction to ANDROIDTV on the Internet. In this series of tutorials, you will learn how to develop ANDROIDTV applications. The main purpose of this article is to understand the UI implementation of ANDROIDTV.

The UI of the ANDROIDTV is very different from that of the Android mobile app, and in ANDROIDTV we can only use the ↑↓←→ key to manipulate the app, not touch the screen. To meet this requirement, the Android Open source project provides the Leanback support library (android.support.v17.leanback) We can implement the user interface for ANDROIDTV based on Leanback Library, this tutorial mainly explains the use of Leanback library.

This series of tutorials are developed based on Androidstudio, please download and install using Androidstudio download

此教程使用人群:
    • Experience in developing Android applications
    • Skill Level-Intermediate
Create a ANDROIDTV application 1. Open Androidstudio

New Project

Select Androidtv

Select "Add No Activity" and finish

Androidstudio Auto-Complete creation of the program

2. Add an Activity

First, we want to add an activity, right-click on the Created project "Com.corochann.androidtvapptutorial", select →new→activity→blank Activity, select Launcher Activity.
is automatically named Mainactivity.
Androidstudio will create two files, one mainactivity.class and Activity_main.xml.

We can also find the Android TV activity when we create the activity, but we don't choose this because it creates a lot of files at the same time. Of course, if you can understand them, they are very helpful, but it is difficult for us to understand these things in the first contact. In this series of articles, we will step through the creation of these files to understand their usage.
Next, we'll start the UI design.

3. Add a Fragment

Right-click your package name
New→java class→name:mainfragment

Similarly, if we choose to create a fragment→blank fragment, it will also create a lot of code.

First, we compile our activity_main.xml so that it contains only mainfragment.

<?xml version= "1.0" encoding= "Utf-8"?><FragmentXmlns:android="Http://schemas.android.com/apk/res/android"xmlns:app= "Http://schemas.android.com/apk/res-auto" xmlns:tools= "Http://schemas.android.com/tools" android:id=" @+id/main_browse_fragment " Android:name= "com.corochann.androidtvapptutorial.MainFragment" android:layout_width= "match_parent" android:layout_height= "match_parent" tools:context=. Mainactivity "tools:deviceids=" TV " tools:ignore= "Mergerootframe"/>     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

Modify the Mainfragment as follows
We let mainfragment inherit Browsefragment,browsefragment is provided by ANDROIDSDK, we can see through this tutorial how it is a standard UI for ANDROIDTV.

Package Com.corochann.helloandroidtvfromscrach;import android.os.Bundle; import android.support.v17.leanback.app.BrowseFragment; import android.util.Log; public class mainfragment extends BrowseFragment {private static final String TAG = MainFragment.class.getSimpleName ();  @Override public void onactivitycreated (Bundle savedinstancestate) {log.i (TAG,  " Onactivitycreated "); super.onactivitycreated (Savedinstancestate);}}       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

Run this program

Browsefragment consists of headersfragment and rowsfragment, in our program, the black part of the right is headersfragment, the left part is rowsfragment, Below we will talk about how to design headersfragment and rowsfragment.

4. Add a method to Mainfragment.java setupuielements ()

We will add a setupuielements () method to add more information to the program.

   @OverridePublicvoidOnactivitycreated (BundleSavedinstancestate) {Log. I (TAG, "onactivitycreated");Super. onactivitycreated (Savedinstancestate);Setupuielements (); } private voidSetupuielements () {//setbadgedrawable (Getactivity (). Getresources (). Getdrawable (R.drawable.videos_by_ Google_banner)); settitle ( "Hello Android tv!"); //Badge, when set, takes precedent //over title setheadersstate (headers_enabled); setheaderstransitiononbackenabled (true); //set FastLane (or headers) background color setbrandcolor ( Span class= "Hljs-function" >getresources (). getcolor (R.color.fastlane_background)); //set search icon color setsearchaffordancecolor (getresources (). getcolor (R.color.search_opaque)); }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st

In the code above, we set the app's title and icon, and Brandcolor.
Color information from Color.xml, let's now create a
New→values Resource File
File name: Color.xml
The code is as follows:

<?xml version="1.0" encoding="utf-8"?><resources>    <color name="fastlane_background">#0096e6</color> <color name="search_opaque">#ffaa3f</color></resources>
    • 1
    • 2
    • 3
    • 4
    • 5

To run the program:

5. Compiling Android Manifest

You will find that there is no icon for your app on ANDROIDTV, we need to make some announcements in Android manifest

<?xml version= "1.0" encoding= "Utf-8"?><ManifestXmlns:android="Http://schemas.android.com/apk/res/android"Package="Com.corochann.androidtvapptutorial" ><!--TV app need to declare touchscreen not required--<Uses-featureAndroid:name="Android.hardware.touchscreen"Android:required="False"/><!--True:your app runs on only TV False:your apps runs on phone and TV--<Uses-featureAndroid:name="Android.software.leanback"Android:required="True"/><Applicationandroid:allowbackup="True"android:icon="@mipmap/ic_launcher"Android:label="@string/app_name"Android:theme="@style/theme.leanback" ><ActivityAndroid:name=". Mainactivity "android:icon="@drawable/app_icon_your_company"Android:label= "@string/app_name" android:logo= "@drawable/app_icon_your_company" > <intent-filter> Span class= "Hljs-tag" ><action android:name=" Android.intent.action.MAIN "/> <category android:name= "Android.intent.category.LEANBACK_LAUNCHER"/> Span class= "Hljs-tag" ></intent-filter> </activity> </application> </MANIFEST>         
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

Put the App_icon_your_company under Main/res/drawble/folder and run the program.

Show the activity icon in launcher

Set Intent-filter inside the manifest

<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
    • 1

At the same time, you have to declare it in the activity

<activity            android:name=".MainActivity"            android:icon="@drawable/app_icon_your_company"            android:label="@string/app_name"            android:logo="@drawable/app_icon_your_company" >            ...
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

Display the application icon on launcher

Display by setting the application

<application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:theme="@style/Theme.Leanback" >        ...
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

Note: The activity icon and the application icon are different

If you want to get a clearer understanding of the manifest in Androidtv, please check the official instructions for FQ.
This code can be downloaded from GitHub.
GitHub Address

ANDROIDTV first create (go: Support original)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.