Monday, March 11, 2024

Android java: develop android tv application

Add dependency build.gradle.kts

...
dependencies {
    implementation(libs.material)
    implementation("androidx.leanback:leanback:1.2.0-alpha04")
    // leanback-preference is an add-on that provides a settings UI for TV apps.
    implementation("androidx.leanback:leanback-preference:1.2.0-alpha04")
}
...

Add requirement in to AndroidManifest.xml

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    >

    <!-- Leanback MUST -->
    <uses-feature android:name="android.software.leanback" android:required="false" />
    <uses-feature android:name="android.hardware.touchscreen" android:required="false" />

    <!-- MUST android:icon="@mipmap/ic_launcher" -->
    <!-- MUST android:banner="@drawable/mybannerandroidtv" 320x180 -->
    <application
        android:icon="@mipmap/ic_launcher"
        android:banner="@drawable/mybannerandroidtv"
...
        <activity
            android:name=".TV.MainActivity"
            android:exported="true"
            android:theme="@style/Theme.Leanback"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
            </intent-filter>
        </activity>
...

If you use Java, do not create activity from TV -> Empty Activity. It will use kotlin. Just create empty layout and java for main activity.

res -> layout -> fragment_tv_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"/>
</LinearLayout>

com.dedetok.tutorialandroidtv.TV.FragmentTVMain

package com.dedetok.tutorialandroidtv.TV;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.leanback.app.BrowseSupportFragment;

import com.dedetok.tutorialandroidtv.R;

//public class FragmentTVMain extends Fragment {
public class FragmentTVMain extends BrowseSupportFragment {

    /*
    // extends Fragment
    public FragmentTVMain() {
        super(R.layout.fragment_tv_main);
    }
     */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
}

activity_main_tv.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <!-- Screen elements that can render outside the overscan safe area go here -->

    <!-- Nested RelativeLayout with overscan-safe margin -->
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="27dp"
        android:layout_marginBottom="27dp"
        android:layout_marginLeft="48dp"
        android:layout_marginRight="48dp">

        <!-- Screen elements that need to be within the overscan safe area go here -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World" />
    </RelativeLayout>
</RelativeLayout>

com.dedetok.tutorialandroidtv.TV.MainActivity.java

package com.dedetok.tutorialandroidtv.TV;

import com.dedetok.tutorialandroidtv.R;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

public class MainTVActivity extends FragmentActivity {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tv_main);
    }

Note com.dedetok.tutorialandroidtv.TV.FragmentTVMain:

If you want to use fragment instead of BrowseSupportFragment, remove comment tag on fragment and add comment tag to BrowseSupportFragment 

Additional info:

to run your application on Android TV / Google TV

  1. enable developer mode your Android TV/GoogleTV
  2. in developer options enable usb debugging 
  3. in developer options enable internet adb / wireless adb (depend on system)
  4. run $ ./adb connect [your android tv/google tv ip], for example:
    $ ./adb connect 192.168.1.144
    connected to 192.168.1.144:5555
  5. your android tv/google tv will available on android studio your android studio


No comments:

Post a Comment