<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mario Viviani on Medium]]></title>
        <description><![CDATA[Stories by Mario Viviani on Medium]]></description>
        <link>https://medium.com/@Mariuxtheone?source=rss-760db44e8e7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*bF78S0hnWZpLRUMUIrLitg.jpeg</url>
            <title>Stories by Mario Viviani on Medium</title>
            <link>https://medium.com/@Mariuxtheone?source=rss-760db44e8e7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 10:15:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Mariuxtheone/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Step-By-Step Tutorial: Adding Voice Controls with the Android Media Session API on Amazon Fire TV]]></title>
            <link>https://medium.com/amazon-appstore/step-by-step-tutorial-adding-voice-controls-with-the-android-media-session-api-on-amazon-fire-tv-f6dea9c5ee77?source=rss-760db44e8e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6dea9c5ee77</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[fire-tv]]></category>
            <category><![CDATA[amazon]]></category>
            <category><![CDATA[alexa]]></category>
            <category><![CDATA[streaming]]></category>
            <dc:creator><![CDATA[Mario Viviani]]></dc:creator>
            <pubDate>Wed, 31 Oct 2018 13:01:25 GMT</pubDate>
            <atom:updated>2018-10-31T13:01:25.995Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/955/0*6GZ1wZv9txn9qXSc" /></figure><p>Amazon Fire TV allows customers to enjoy media content like movies and TV in a lot of different ways. Using your voice, for example, you can use speech to interact with your living environment.</p><p>On top of using the voice remote that ships with Amazon Fire TV, starting this year in the US, UK, and Germany, you can<strong> connect an Amazon Fire TV and an Echo device</strong> and then <strong>use your voice</strong> to control video playback.</p><p>Voice controls in-app are achieved by integrating the <strong>Android Media Session API</strong> in your application. Follow along with my step-by-step instructions here or watch this video tutorial:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FLWJTImRmHrQ%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DLWJTImRmHrQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FLWJTImRmHrQ%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/eee3a94ed5659fd6cde061a85c7266bc/href">https://medium.com/media/eee3a94ed5659fd6cde061a85c7266bc/href</a></iframe><h3>Add the voice control permission to your Android manifest</h3><p>The first step is to add the voice control permission to your Android manifest. This will allow Amazon Fire TV to identify your application as voice enabled.</p><pre>&lt;uses-permission android:name=&quot;com.amazon.permission.media.session.voicecommandcontrol&quot;/&gt;</pre><p>There are quite a lot of commands that are available through Media Session and they are all related to media playback. There are controls like play, pause, fast-forward, etc. Here’s a short list:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/0*srZAHU93cpSipit0" /></figure><h3>Understanding the main components of the Media Session API implementation</h3><p>There are two main components: the Media Session and the Media Player itself. The Media Session provides a set of callbacks which are linked to all the actions that are available to the media player, like play, pause, skip to next, etc. The voice controls directly map into these Media Session callbacks, meaning that every time a customer executes a voice command, the Media Session callback is invoked.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/773/0*4ysRnCLJfQgclvqg" /></figure><p>Let’s assume, for example, the customer says “Alexa, pause.” The <strong><em>onPause()</em> </strong>callback of the Media Session is then invoked. We’ll then need to invoke the actual Media Player method pause from within the Media Session callback. In a way, Media Session acts as the middleman between the Alexa command and the actual media player in your app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/835/0*7jjRgAYqKBtYfB7E" /></figure><p>There are<strong> five main steps </strong>to implement Media Session correctly in your fire TV application.</p><ol><li>Initialize the video player.</li><li>Initialize the Media Session.</li><li>Configure the actions our application is capable of perform, for example play, pause, next, etc.</li><li>Properly manage the Media Session inside the activity life cycle.</li><li>Set up the Media Session called bags. That’s where the magic happens, where we connect the voice commands themselves and the actual Media Player inside our app.</li></ol><p><strong>Step 1 — Initialize the video player.</strong></p><p><strong>onCreate()</strong></p><pre>@Override<br>protected void onCreate(Bundle savedInstanceState) { <br>super.onCreate(savedInstanceState);    <br><br>// Set the video player view    <br>setContentView(R.layout.activity_media_session);    <br>mVideo = (VideoView) findViewById(R.id.video_view);<br>...<br> }</pre><p>The first thing we have to do is in the <strong><em>onCreate()</em></strong> method of our activity, where we set the content to our layout and then we initialize the video player itself. There are a lot of different media players that you can use in your Fire TV application. For the sake of this tutorial, we will use the standard Android VideoView, which provides all the most common callbacks that you would expect in a media player (play, pause, rewind, fast-forward, etc).</p><p><strong>onStart()</strong></p><pre>@Override<br>protected void onStart() {<br>    super.onStart();     <br><br>//Initialize the media controller for the video player    <br>    final MediaController mediaController = new MediaController(MediaSessionActivity.this);     <br>    mediaController.hide();     <br><br>//Set the URI of the video     <br>    mVideo.setMediaController(mediaController);     <br><br>//Set the URI of the video     mVideo.setVideoURI(“myvideoURL.mp4”);      <br>    mVideo.requestFocus();<br><br>...</pre><p>Inside the onStart() method of the application, we initialize the Media Controller. The Media Controller is the component that displays on-screen the current playback status of the media and some basic UI for the controls. We pass the Media Controller to the media player using setMediaController(). We then pass the video URI, which is the address of the media that we want to play. In this case, we have hard-coded the MP4 file; in a real-world scenario, you would probably fetch this address from your back-end service. Finally, we request focus for the video.</p><p><strong>Step 2 — Initialize the Media Session</strong></p><p>It is good practice to initialize the Media Session once a video player is ready to play content. Most video players provide a listener which advertises that the video player is ready to play content. In this case, VideoView provide us the onPreparedListener class which provides the onPrepared() callback, signaling that a video player is ready to go.</p><pre>mVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {         <br><br>    @Override<br>    public void onPrepared(MediaPlayer mp) {<br><br>//Initialize the the media session<br>    mMediaSession = new MediaSession(getApplicationContext(), TAG);         <br><br>//Assign the Callbacks to the Media Session     <br>    mMediaSession.setCallback(getMediaSessionCallback());<br><br>//Set the flags that allow the app to take over the remote controls      <br>    mMediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |<br>                         MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);<br><br>    ...</pre><p>We initialize the new Media Session, which receives the application context as parameter and a string, which is a tag to uniquely identify the Media Session. Then we set the callbacks using the method setCallback(). Don’t worry now about these callbacks, as further down this tutorial we will see how we will define this Media Session callbacks. Finally, we set the Media Session flags, HANDLES_MEDIA_BUTTONS and HANDLES_TRANSPORT_CONTROLS. These flags map the basic remote controls buttons to our Media Session. It is important to note that even though customers might use voice to control the playback, they might also use the physical remote control.</p><p><strong>Step 3 — Add the actions to the Media Session</strong></p><pre>@Overridepublic void onPrepared(MediaPlayer mp) {<br>...    <br>PlaybackStateCompat state = new PlaybackStateCompat.Builder()         <br>        .setActions(PlaybackState.ACTION_PLAY_PAUSE                 <br>                    | PlaybackState.ACTION_PLAY                 <br>                    | PlaybackState.ACTION_PAUSE                 <br>                    | PlaybackState.ACTION_FAST_FORWARD                 <br>                    | PlaybackState.ACTION_REWIND                 <br>                    | PlaybackState.ACTION_SKIP_TO_NEXT                 <br>                    | PlaybackState.ACTION_SKIP_TO_PREVIOUS)<br>        <br>        .setState(PlaybackState.STATE_PLAYING, mVideo.getCurrentPosition(), 1.0f)        <br>        .build();<br><br>        mMediaSession.setPlaybackState(state);<br>        mMediaSession.setActive(true);<br>}</pre><p>In the <strong><em>onPrepared() </em></strong>method where we initialized the Media Session, we now create a new variable which is the <strong>PlaybackState</strong>. This component is used to tell to the underlying Media Session what the current status of the playback is and also defines which actions can be performed by the Media Session.</p><p>This is why we use the method<strong> setActions() — </strong>to set all the actions that we want our application to be able to perform, like play, pause, fast-forward, etc.</p><p>We then set the PlaybackState to <strong>STATE_PLAYING. </strong>We pass the current position (<strong>mVideo.getCurrentPosition(),</strong> which by default returns 0) and then we pass a float which indicates the speed of the playback, with 1.0f meaning normal speed.</p><p>Finally, we pass the PlaybackState to the Media Session using <strong>MediaSession.setPlaybackState(state) </strong>and activate the Media Session using <strong>setActive(true)</strong>.</p><p><strong>Step 4 — Properly manage the Media Session in activity life cycle</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/0*IRemwiHSFX-uqo3f" /></figure><p>When the customer interacts via voice with their Fire TV, <strong>an overlay appears on the screen to show the Alexa interaction</strong>. At this stage, we need to pause the playback and gracefully manage the behavior of our application.</p><p><strong>onPause()</strong></p><pre>@Override<br><br>protected void onPause() {<br><br>// Pause the activity<br>super.onPause();<br><br>// Pause the video player<br><br>mPlaybackState = PlaybackState.STATE_PAUSED;<br>mVideo.pause();<br><br>//deactivate the media session<br>    mMediaSession.setActive(false);    <br>    mMediaSession.setPlaybackState(new PlaybackState.Builder()<br>            .setState(mPlaybackState, mVideo.getCurrentPosition(),1.0f)<br>            .setActions(getActions())<br>            .build());<br> }</pre><p>In the <strong><em>onPause()</em></strong> method of activity, we need to create a new PlaybackState, pause the video player, set the Media Session to not active, and pass the newly created PlaybackState to the Media Session itself.</p><p><strong>IMPORTANT:</strong> Notice here that <strong>we need to set the actions again</strong>. If we won’t set the actions again, the newly created PlaybackState would be actions-less, so it wouldn’t be able to map the voice controls back to the Media Session itself.</p><p><strong>onResume()</strong></p><pre>@Override<br>protected void onResume() {    <br>super.onResume();                <br><br>mMediaSession.setActive(true);        <br>mPlaybackState = PlaybackState.STATE_PLAYING;        <br><br>mMediaSession.setPlaybackState(new PlaybackState.Builder()<br>                .setState(mPlaybackState, mVideo.getCurrentPosition(),1.0f)<br>                .setActions(getActions())<br>                .build());        <br><br>mVideo.requestFocus();<br>mVideo.start();<br> }</pre><p>We need to do the same thing in the onResume() methods. We set the Media Session to active, set the PlaybackState to playing, and then create a new PlaybackState, still setting the actions.</p><p><strong>onStop()</strong></p><pre>@Overrideprotected void onStop() {       <br><br> // Stop the activity    <br>super.onStop();    <br><br>// Stop the video player    <br>mPlaybackState = PlaybackState.STATE_STOPPED;    <br>mVideo.stopPlayback();    <br><br>//deactivate the media session    <br>mMediaSession.setActive(false);    <br>mMediaSession.setPlaybackState(new PlaybackState.Builder()<br>            .setState(mPlaybackState, mVideo.getCurrentPosition(),1.0f)<br>            .setActions(getActions())<br>            .build());<br> }</pre><p>We do the same thing when our application is stopped, using PlaybackState stopped.</p><p><strong>onDestroy()</strong></p><pre>@Override<br>protected void onDestroy() {     <br><br>//We release the media session as we’re destroying the Activity     <br>if (mMediaSession != null) {              <br>        mMediaSession.release();         <br>        mMediaSession = null;<br>     }<br>super.onDestroy();<br>}</pre><p>The final activity lifecycle event that we need to manage is <strong>onDestroy() </strong>which is called when the application is terminated. In this case, the only thing we need to do is to release the Media Session, which might be used by other applications.</p><p><strong>Step 5 — Set the Media Session callbacks</strong></p><p>This is where the magic happens and where we actually connect the voice commands and the actual behavior in our Media Player.</p><pre>private MediaSession.Callback getMediaSessionCallback() {<br><br>return new MediaSession.Callback() {<br>     @Override<br>    public void onPlay() { } <br>    <br>    @Override<br>    public void onPause() { }<br>    <br>    @Override<br>    public void onSeekTo(long pos) { }<br><br>    @Override<br>    public void onFastForward() { }<br><br>    @Override<br>    public void onSkipToNext() { }<br><br><br>    ...</pre><p>The Media Session callbacks are multiple and <strong>mimic the commands available in the Media Player</strong>. These are the commands that we will see: play, pause, seekTo, fast-forward, and skip to next.</p><p><strong>onPlay()</strong></p><pre>private MediaSession.Callback getMediaSessionCallback() {<br><br>return new MediaSession.Callback() {<br>         @Override<br>         public void onPlay() {                  <br>         <br>         mPlaybackState = PlaybackState.STATE_PLAYING;<br>         mVideo.start(); <br>         <br>         updatePlaybackState(mPlaybackState, mVideo.getCurrentPosition(),1.0f );<br>         mMediaController.hide();<br><br>         }<br>    ...</pre><p>The callback <strong>onPlay()</strong> is pretty easy: we set the PlaybackState to STATE_PLAYING, we start the video player, we just update the PlaybackState, and possibly hide the Media Controller.</p><p><strong>onPause()</strong></p><pre>private MediaSession.Callback getMediaSessionCallback() {<br><br>return new MediaSession.Callback() {<br>   ...<br>   @Override<br>   public void onPause() {                  <br>   <br>    mPlaybackState = PlaybackState.STATE_PAUSED;<br>    mVideo.pause();<br>    <br>    updatePlaybackState(mPlaybackState, mVideo.getCurrentPosition(),1.0f );          <br>    mMediaController.show();  <br>    <br>    }<br>    ...</pre><p><strong><em>onPause() </em></strong>is similar to onPlay(): set the play back to STATE_PAUSED, pause the media player, and update the PlaybackState accordingly. In this case, we will show the MediaController as we want to display the UI to the customer, showcasing at what stage the playback is.</p><p><strong>onSeekTo(long pos)</strong></p><pre>private MediaSession.Callback getMediaSessionCallback() {<br>return new MediaSession.Callback() {<br>    ...<br> @Override<br> public void onSeekTo(long pos) { <br> <br>    mPlaybackState = PlaybackState.STATE_BUFFERING;<br>    <br>    mVideo.seekTo(mVideo.getCurrentPosition() + (int) pos);<br>    <br>    }<br>    ...</pre><p>The callback <strong>onSeekTo() </strong>is interesting and this is where you can see how powerful the implementation of the voice controls is. This method allows us to fast-forward the content by a certain amount of seconds, minutes, or even hours.</p><p>This method is invoked when the customer says “Alexa, forward…” and adds a certain amount of minutes (for example, <em>“Alexa, fast-forward one minute and thirty seconds”</em>). In this case, the Alexa voice controls automatically convert what the customer is saying into milliseconds, which is passed to the seekTo() position as the variable l<strong>ong “pos.”</strong></p><p>The great advantage here is we don’t need to do anything to correctly parse what the customer is saying because that is managed automatically by Alexa.</p><p><strong>Fast-forward()</strong></p><pre>private MediaSession.Callback getMediaSessionCallback() {<br><br>return new MediaSession.Callback() {<br><br>    ...<br> @Override<br> public void onFastForward() {                  <br> <br>    mPlaybackState = PlaybackState.STATE_BUFFERING;<br>    <br>    mVideo.seekTo(mVideo.getCurrentPosition() + 10 * 1000);   <br>    <br>    }<br>    ...</pre><p>However, the customer might also just say “Alexa, fast-forward” without specifying an amount of seconds, minutes, or hours. In this case, we need to implement the fast-forward method just passing a predefined amount of milliseconds. In this example, we are passing 10,000 milliseconds, aka 10 seconds.</p><p><strong>onSkipToNext()</strong></p><pre>private MediaSession.Callback getMediaSessionCallback() {<br><br>return new MediaSession.Callback() {<br>    ...<br> @Override<br> public void onSkipToNext() {                  <br>    mPlaybackState = PlaybackState.STATE_SKIPPING_TO_NEXT;<br>    <br>    //Skip to the next video in the playlist<br>    skipToNextVideo();       <br>    <br>    }<br>    ...</pre><p>The last callback we see is <strong>onSkipToNext()</strong>. This method really depends on how you have implemented your application and how you fetch the next videos. You would likely create a<strong> skiptoNextVideo(),</strong> method which would fetch all the information to skip to the next video. Just remember to set the state to<strong> STATE_SKIPPING_TO_NEXT</strong> as loading a new video might take a while and you want to show some buffering in your video player.</p><h3>Building apps in minutes</h3><p>We want to enable you to build high-quality applications for Amazon Fire TV in just a few minutes. In order to do this, you can leverage the <a href="https://developer.amazon.com/docs/fire-app-builder/overview.html"><strong>Amazon Fire App Builder</strong></a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*GQKqT5Ae0SPWOBYj" /></figure><p>Amazon Fire App Builder is a plug-and-play template for audio and video applications, allowing you to create an app in less than an hour. It contains modules to enable advanced functionalities. It handles JSON feeds, custom branding and customization, fully supports the Amazon Fire TV family, and it fully integrates the Media Session API and voice controls that we seen in this tutorial. This work out of the box, so you don’t have to do anything to enable it when using Fire App Builder. Fire App Builder provides modules for features like in-app purchasing, subscriptions, social logins, advertisement, analytics, and custom media player. You can swap these modules in and out of your application depending on what you need.</p><p>To find out more about Fire App Builder visit <a href="https://github.com/amzn/fire-app-builder">https://github.com/amzn/fire-app-builder</a>.</p><p>If you’re interested in finding out more about Fire TV development, download our free eBook, “<a href="https://bit.ly/firetvebook"><strong>How to Develop Media Streaming Apps for Amazon Fire TV</strong></a>”.</p><p>Thanks for reading!</p><p>Mario Viviani — <a href="https://twitter.com/mariuxtheone">@mariuxtheone</a></p><p><em>Originally published at </em><a href="https://developer.amazon.com/blogs/appstore/post/d5c274e3-cbbf-4c6a-816d-816438739489/step-by-step-tutorial-adding-voice-controls-with-the-android-media-session-api-on-amazon-fire-tv"><em>developer.amazon.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6dea9c5ee77" width="1" height="1" alt=""><hr><p><a href="https://medium.com/amazon-appstore/step-by-step-tutorial-adding-voice-controls-with-the-android-media-session-api-on-amazon-fire-tv-f6dea9c5ee77">Step-By-Step Tutorial: Adding Voice Controls with the Android Media Session API on Amazon Fire TV</a> was originally published in <a href="https://medium.com/amazon-appstore">Amazon Developers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Developing for the Living Room: How to Build an Android App for Fire TV (Part 6)]]></title>
            <link>https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-fire-tv-part-6-6560e45f3dd9?source=rss-760db44e8e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/6560e45f3dd9</guid>
            <category><![CDATA[tv]]></category>
            <category><![CDATA[amazon-fire-tv]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Mario Viviani]]></dc:creator>
            <pubDate>Mon, 09 Jan 2017 10:45:45 GMT</pubDate>
            <atom:updated>2017-01-09T10:45:45.650Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*v4IqVGRJzjghSwbj." /></figure><h4>Playing the content: The PlaybackOverlayActivity</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*fGXsIZKLCb_4BLDz." /></figure><p>In parts 1–5 of this series we followed the user journey on Fire TV from browsing and content discovery to reading the details of specific content and performing an action. Now we end our journey on the best part: how to play the video!</p><h3>The PlaybackOverlayActivity</h3><p>In a Leanback-enabled project, playing video content is performed within the <strong>PlaybackOverlayActivity</strong>. The UI of the <strong>PlaybackOverlayActivity</strong> is simple. We have a full-screen video player that is responsible for playing the content. On top of the video player is the <strong>PlaybackOverlayFragment</strong>, which is responsible for displaying all the media controls and managing the underlying content play back.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*v4IqVGRJzjghSwbj." /></figure><p>There are many different video players that can be used. When we first deploy the Leanback-enabled project, the default video player is <strong>VideoView</strong>.</p><p><strong>VideoView</strong>is a very basic video player that is perfect if we just want to display non-encrypted video files in an easy way. That being said, most developers prefer to opt for a more powerful and feature-rich player, and for Fire TV the usual choice is the <a href="https://developer.amazon.com/public/solutions/devices/fire-tv/docs/fire-tv-media-players#exoplayer?sc_channel=blog&amp;sc_publisher=blog&amp;sc_campaign=BLOG_DG_Evergreen&amp;sc_detail=blog&amp;sc_segment=blog&amp;sc_country=ww&amp;sc_itrackingcode=blog1001&amp;sc_medium=BLOG_DG_Evergreen_blog_ww_blog1001">Amazon-customized version of ExoPlayer</a>. For the sake of simplicity, we’ll stick with what we find in the default Leanback template, <strong>VideoView</strong>.</p><p>When we analyze the xml file of the UI in the <strong>PlaybackOverlayActivity</strong>, we’ll see that there’s nothing more than these two components:</p><pre>&lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;&gt;<br>    &lt;VideoView<br>        android:id=&quot;@+id/videoView&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;match_parent&quot;<br>        android:layout_gravity=&quot;center&quot;/&gt;<br>    &lt;fragment<br>        android:id=&quot;@+id/playback_controls_fragment&quot;<br>        android:name=&quot;PlaybackOverlayFragment&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;match_parent&quot;<br>/&gt;<br>&lt;/FrameLayout&gt;</pre><h3>Main components of the PlaybackOverlayActivity</h3><p>The basic setup of the <strong>PlaybackOverlayActivity</strong> is quite simple, and focuses on four main components:</p><pre>public class PlaybackOverlayActivity extends Activity implements<br>        PlaybackOverlayFragment.OnPlayPauseClickedListener {<br><br>private VideoView mVideoView;<br>private LeanbackPlaybackState mPlaybackState;<br>private MediaSession mSession;<br><br>…<br><br>}</pre><ol><li><strong>Listeners</strong>: we can add plenty of Listeners to the <strong>PlaybackOverlayActivity</strong>, which is useful for adding callbacks for all the actions performed by the user on their remote control (Play, Pause, Rewind, etc). For simplicity, we’ll review the only Listener available on the Leanback template, the <strong>OnPlayPauseClickedListener</strong>.</li><li><strong>VideoView</strong>: is the player we’ll use to play the video content.</li><li><strong>LeanbackPlaybackState</strong>: is just a flag that is used to keep track of what the status of the app is (e.g. <strong>LeanbackPlaybackState.PLAYING, LeanbackPlaybackState.PAUSED</strong>).</li><li><strong>MediaSession</strong>: the main task of MediaSession is to talk to the underlying Android framework and manage the ownership of the actions performed with the remote.</li></ol><h3>The remote</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/0*iuySOzKEIU955hGM." /></figure><p>One of the main differences between Fire TV and other Android devices is that the only way to interact with Fire TV is through its remote.</p><p>Because Fire OS 5 is based on Android Lollipop, the <strong>KeyEvents</strong> generated on the Fire TV remote are the same <strong>KeyEvents</strong> that are generated on a classic Android device, but with physical buttons.</p><p>First of all, we need to make sure that our application is in control of the remote. We do it easily by setting a couple flags in the <strong>MediaSession</strong>.</p><pre><a href="http://twitter.com/Override">@Override</a><br>public void onCreate(Bundle savedInstanceState) {<br>    super.onCreate(savedInstanceState);<br>    setContentView(R.layout.playback_controls);<br>    ...<br>    <br>    mSession = new MediaSession(this, &quot;LeanbackSampleApp&quot;);<br>    mSession.setCallback(new MediaSessionCallback());<br>    mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS<br>            | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS<br>    );</pre><pre>mSession.setActive(true);</pre><pre>}</pre><p>When <strong>MediaSession.setFlags()</strong> is invoked, our application takes control of the actions performed on the buttons of the remote. In particular, the <strong>FLAG_HANDLES_MEDIA_BUTTONS</strong>allows users to take control of the buttons on the bottom of the remote (Rewind, Play/Pause, and Forward), and <strong>FLAG_HANDLES_TRANSPORT_CONTROLS</strong> allows our app to listen for the Navigation buttons that control the movement inside the views of the app (Up, Right, Down, Left).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*Lbm5RdDxsFYvk78Z." /></figure><h3>Listen to KeyEvents on the remote</h3><p>Now that we have set the <strong>MediaSession</strong> of our app, we can start listening for actions performed on the remote.</p><p>To do this, we need to react to <strong>KeyEvents</strong>:</p><pre><a href="http://twitter.com/Override">@Override</a><br>public boolean onKeyUp(int keyCode, KeyEvent event) {<br>    PlaybackOverlayFragment playbackOverlayFragment = <br>  findFragmentById(R.id.playback_controls_fragment);<br>    switch (keyCode) {<br>        ...<br>        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:<br>            if (mPlaybackState == LeanbackPlaybackState.PLAYING) {<br>                playbackOverlayFragment.togglePlayback(false);<br>            } else {<br>                playbackOverlayFragment.togglePlayback(true);<br>            }<br>            return true;<br>        ...<br>    }<br>}</pre><p>We need to override <strong>onKeyUp()</strong>, as this is the event that is triggered when the user clicks on a button and lifts their finger (single click/action concluded).</p><p>In this case we demonstrate how to react when the user presses the button Play/Pause. We react to the <strong>KeyEvent</strong>: <strong>KEYCODE_MEDIA_PLAY_PAUSE<em> </em></strong>then check the current <strong>LeanbackPlaybackState</strong>. If we are <strong>PLAYING</strong>, we toggle playback to false on the <strong>PlaybackOverlayFragment</strong> (to stop playing). Otherwise we set it to true, triggering the callback on the underlying fragment.</p><p>Finally, we need to trigger playback<strong> </strong>on the <strong>VideoView</strong>. To do this, we need to implement the callback provided by the <strong>ClickListener</strong> in the Activity (in our case, <strong>PlaybackOverlayFragment.OnPlayPauseClickedListener</strong>).</p><pre>public void onFragmentPlayPause(Movie movie, int position, Boolean playPause) {<br>   mVideoView.setVideoPath(movie.getVideoUrl());<br>    ...<br>    if (mPlaybackState != LeanbackPlaybackState.PLAYING) {<br>        	mPlaybackState = LeanbackPlaybackState.PLAYING;<br>        	if (position &gt; 0) {<br>            mVideoView.seekTo(position);<br>            mVideoView.start();<br>        }<br>    ...<br>}</pre><p>Let’s see step by step what happens here:</p><ol><li>Set the URL of the video to play. To do this, we use <strong>VideoView.setVideoPath</strong>(), bypassing the URL of the video. It could be a simple video file on our cloud repository or an embedded video in the app. The important bit is that the URL needs to point to a video file.</li><li>We check the <strong>LeanbackPlaybackState</strong>.</li><li>If we need to start playing the content, we use <strong>seekTo() </strong>to set<strong> </strong>the position in the video where we want it to start playing (in milliseconds, typically 0).</li><li>Call <strong>VideoView.start()</strong> to finally make the video play!</li></ol><h3>Other features of Leanback-enabled projects</h3><p>The Leanback template is quite complex but allows the creation of rich TV experiences for our users. For example, by default you can define recommended videos when a piece of content is displayed. Also, developers can implement the <strong>SearchFragment</strong> to create custom search experiences.</p><h3>Beyond Leanback: The Fire App Builder</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*adA7I609sXv1LC2Y." /></figure><p>Leanback is just the first step when it comes to creating rich media streaming experiences. At Amazon, we’ve been listening closely to the feedback coming from TV app developers and we recently released a new template for creating native TV apps the easiest way possible: the <strong>Fire App Builder</strong>.</p><p>With the Fire App Builder you can create a<strong> rich TV Android App in a few minutes</strong>, easily connecting it to a streaming service and with <strong>plug-and-play modules for IAP, Video Ads, Social Login, Closed Captioning and much more.</strong></p><p>To learn more, visit the <strong>Github repository</strong> at <a href="https://github.com/amzn/fire-app-builder">github.com/amzn/fire-app-builder</a> and the <strong>complete documentation</strong> at <a href="http://bit.ly/FireAppBuilderDoc">bit.ly/FireAppBuilderDoc</a></p><p>We are very excited to see your TV app on Fire TV!</p><p>-Mario Viviani (<a href="http://www.twitter.com/mariuxtheone">@mariuxtheone</a>)</p><p><em>Originally published at </em><a href="https://developer.amazon.com/blogs/post/804cf680-9643-40da-8372-36cc45d73707/developing-for-the-living-room-how-to-build-an-android-app-for-fire-tv-part-6"><em>developer.amazon.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6560e45f3dd9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-fire-tv-part-6-6560e45f3dd9">Developing for the Living Room: How to Build an Android App for Fire TV (Part 6)</a> was originally published in <a href="https://medium.com/amazon-appstore">Amazon Developers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Developing for the Living Room: How to Build an Android App for Fire TV (Part 5)]]></title>
            <link>https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-fire-tv-part-5-e915bee68d72?source=rss-760db44e8e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/e915bee68d72</guid>
            <category><![CDATA[amazon]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[design]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[tv]]></category>
            <dc:creator><![CDATA[Mario Viviani]]></dc:creator>
            <pubDate>Wed, 14 Dec 2016 12:42:05 GMT</pubDate>
            <atom:updated>2016-12-14T12:42:28.311Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*afozAU31f8fUuGYC.png" /></figure><h4>Providing the Details of the App Content through the DetailsFragment</h4><p>In <a href="https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-fire-tv-part-1-6ae108106fd2#.f6828fevi">Part 1 of this series</a> we analyzed the <strong>TV Interaction Model</strong>, based on three steps: <em>Browsing for Content, Reading Description and Details, </em>and <em>Playing the Content</em>. The first action, Browsing for Content, as we have seen in <a href="https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-amazon-fire-tv-part-3-a95b954c3f15#.g155ovmca">Part 3 of this series</a>, is achieved through the BrowseFragment.</p><p>Now let’s see how we can provide information about a specific piece of content, following the second step of the user journey, <em>Reading Description and Details</em>. To do this we’ll use one of the main components of a Leanback-enabled project: the <strong>DetailsFragment</strong>.</p><h3>The DetailsFragment</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*afozAU31f8fUuGYC.png" /><figcaption>This is the DetailsFragment</figcaption></figure><p>The DetailsFragment is displayed when the user selects a specific piece of content on the BrowseFragment. It contains information like Title, Subtitle, Description, and is accompanied by a preview of the content. It also contains Actions that we can prompt our user to perform.</p><p>One of the most important classes used in the DetailsFragment is DetailsOverViewRow. This class defines which content is displayed in the fragment (as seen in the previous episode, DetailsOverViewRow takes advantage of a Presenter, called DescriptionPresenter) and, most importantly, is responsible for defining the Actions that we can prompt our user to perform.</p><pre>private void setupDetailsOverviewRow() { <br>       final DetailsOverviewRow row = <br>                   new DetailsOverviewRow(mSelectedMovie); </pre><pre>... </pre><pre>row.setImageDrawable(R.drawable.default_background)); </pre><pre>row.addAction(new Action(ACTION_WATCH_TRAILER, “Watch Trailer”, “FREE”))); </pre><pre>mAdapter.add(row); </pre><pre>}</pre><p>In the highlighted row we demonstrate how easy it is to add a specific Action to the DetailsFragment. Just by coding <strong><em>addAction()</em></strong> we can add a new Action for the user to perform. In this case we added the unique ID <strong><em>ACTION_WATCH_TRAILER, </em></strong>for the Action and two Strings <strong>“</strong>Watch Trailer<strong>”, “</strong>FREE<strong>” </strong>to define the text field of the button.</p><p>Once we have added this line, the Action will be displayed on the DetailsFragment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/0*2I6uZLyliU42eGru.png" /></figure><p>By using Actions we can easily add IAP items like “Rent the Content”, “Buy”, or “Subscribe”. It is just a matter of attaching a Listener to the Actions to perform consequent tasks.</p><p>When we deploy a Leanback-enabled project, the only Action that is defined by default is the “Watch Trailer” that prompts the trailer of the content to play.</p><h3>Stay tuned for Part 6: How to Play Video Content using the PlaybackOverlayFragment</h3><p>In the next and final episode of this series we’ll show how to play the content, leverage the Remote Control, and how to show the on-screen controls using the PlaybackOverlayFragment<strong>.</strong></p><p>Stay tuned!</p><p>Mario Viviani (<a href="http://www.twitter.com/mariuxtheone">@mariuxtheone</a>)</p><p><em>Originally published at </em><a href="https://developer.amazon.com/blogs/post/Tx3S4DEKQMZA7VK/developing-for-the-living-room-how-to-build-an-android-app-for-fire-tv-part-5"><em>developer.amazon.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e915bee68d72" width="1" height="1" alt=""><hr><p><a href="https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-fire-tv-part-5-e915bee68d72">Developing for the Living Room: How to Build an Android App for Fire TV (Part 5)</a> was originally published in <a href="https://medium.com/amazon-appstore">Amazon Developers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Developing for the Living Room: How to Build an Android App for Fire TV (Part 4)]]></title>
            <link>https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-fire-tv-part-4-cbe572a6f1e6?source=rss-760db44e8e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/cbe572a6f1e6</guid>
            <category><![CDATA[design]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[tvs]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Mario Viviani]]></dc:creator>
            <pubDate>Fri, 02 Dec 2016 22:07:19 GMT</pubDate>
            <atom:updated>2016-12-02T22:07:45.363Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*F1SSF00C3wV5XVoV.png" /></figure><h4>WORKING WITH THE PRESENTER CLASS</h4><p>In the <a href="https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-amazon-fire-tv-part-3-a95b954c3f15#.nentesn71">previous episode of this series</a> we discussed how to create the main interface of our Leanback-enabled project through the <strong>BrowseFragment</strong>. Now let’s take a closer look into the <strong>Presenter</strong> class. The <strong>Presenter</strong> class allows us to define the look and feel of our Leanback-enabled app without editing the underlying data structure.</p><h3>The Presenter Class</h3><p>The Leanback template we created was built following a custom version of the common development pattern, Model-view-controller (MVC), in which the <strong>Presenter </strong>class acts as the <strong>View</strong>. The <strong>Presenters</strong> are passed to the <strong>ArrayObjectAdapter</strong> as arguments and define how the content of the Adapter should be displayed</p><p>The Leanback approach provides a variety of predefined <strong>Presenters</strong>:</p><ul><li><strong>CardPresenter</strong> defines the UI of a single item displayed within the row Adapter</li><li><strong>ListRowPresenter </strong>defines how various content in a row should be displayed and arranged</li><li><strong>DetailsDescriptionPresenter</strong> defines the UI of the <strong>DetailsFragment</strong></li></ul><p>Implementing the Presenters are quite similar: they all follow the <strong>ViewHolder</strong> pattern and are mostly composed by Custom Views with methods to set the fields of the views. Let’s take a close look at the customizing the <strong>CardPresenter</strong> as an example:</p><h3>Customizing the CardPresenter</h3><p>In the <strong>BrowseFragment</strong>, we see that the <strong>CardPresenter</strong> defines the UI of a single item of the adapter.</p><pre>public class MainFragment extends BrowseFragment { </pre><pre>CardPresenter cardPresenter = new CardPresenter(); </pre><pre>//Create the adapter for the row and add all the movies ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*F1SSF00C3wV5XVoV.png" /></figure><p>The highlighted view in the previous image is a custom view called <strong>ImageCardView</strong> and is used in the <strong>CardPresenter</strong> to define the UI of a single component.</p><p>Let’s analyze how views are initialized in the <strong>CardPresenter</strong>.</p><pre>public class CardPresenter extends Presenter { <br>public ViewHolder onCreateViewHolder(ViewGroup parent) { <br>... <br>ImageCardView cardView = new ImageCardView(parent.getContext); cardView.setFocusable(true); <br>cardView.setFocusableInTouchMode(true); <br>return new ViewHolder(cardView); <br>... }</pre><p>There are two things to notice here:</p><ol><li>As we mentioned before, the <strong>CardPresenter</strong> is built on the <strong>ViewHolder </strong>pattern. This ensures views are correctly recycled and memory is not wasted.</li><li>The <strong>CardView</strong> is set to <strong>SetFocusable(true)</strong> and <strong>setFocusableInTouchMode(true)</strong>. Although there is no existing touch interaction on a TV screen, this step is necessary because this is how Android currently manages the focus-ability of views.</li></ol><p>Now let’s take a look at how the components of the<strong> ImageCardView</strong> (and of the <strong>Presenter</strong>) are set:</p><pre>public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) { </pre><pre>Movie movie = (Movie) item; </pre><pre>ImageCardView cardView = (ImageCardView) viewHolder.view; </pre><pre>if (movie.getCardImageUrl() != null) {</pre><pre>  cardView.setTitleText(movie.getTitle());<br>  cardView.setContentText(movie.getDescription());<br>  Glide.with(viewHolder.view.getContext())<br>      .load(movie.getCardImageUrl())<br>      .centerCrop()<br>       .into(cardView.getMainImageView()); <br>   } </pre><pre>}</pre><p>In this example we use our own custom class “Movie” to retrieve the information we want to display.</p><p>The <strong>cardView </strong>is retrieved from the <strong>ViewHolder</strong>, then we have the simple setters <strong>setTitleText()</strong> and <strong>setContentText(),</strong> to edit the main fields of the view.</p><p>To retrieve the image for the thumbnail, Leanback automatically uses the <a href="https://github.com/bumptech/glide"><strong>Glide</strong></a> library (but you can easily replace it with the image loading library of your choice).</p><h3>Up next:</h3><p>In the next episode of this series we’ll make a further step towards fully understanding how a Leanback-enabled project works, moving to the second step of the user journey: building and displaying the details of specific content using the <strong>DetailsFragment</strong>.</p><p>Stay tuned!</p><p>Mario Viviani (<a href="http://www.twitter.com/mariuxtheone">@mariuxtheone</a>)</p><p><em>Originally published at </em><a href="https://developer.amazon.com/public/community/post/Tx3MOMYHYU33PSH/Developing-for-the-Living-Room-How-to-Build-an-Android-App-for-Fire-TV-Part-4"><em>developer.amazon.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cbe572a6f1e6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-fire-tv-part-4-cbe572a6f1e6">Developing for the Living Room: How to Build an Android App for Fire TV (Part 4)</a> was originally published in <a href="https://medium.com/amazon-appstore">Amazon Developers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Developing for the Living Room: How to Build an Android App for Amazon Fire TV — Part 3]]></title>
            <link>https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-amazon-fire-tv-part-3-a95b954c3f15?source=rss-760db44e8e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/a95b954c3f15</guid>
            <category><![CDATA[tvs]]></category>
            <category><![CDATA[mobile-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Mario Viviani]]></dc:creator>
            <pubDate>Tue, 13 Sep 2016 09:20:38 GMT</pubDate>
            <atom:updated>2016-09-13T09:20:38.892Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hPQIVdVeGlujlVte.png" /></figure><h4>BROWSING THE CONTENT OF A LEANBACK-ENABLED ANDROID APP: THE BROWSEFRAGMENT</h4><p>In the <a href="https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-fire-tv-part-2-8d59299d0b32#.btqeza57y">previous episode of this series</a> we discussed the <strong>anatomy of a Leanback-enabled Android App for </strong><a href="https://developer.amazon.com/public/solutions/devices/fire-tv?sc_channel=blog&amp;sc_publisher=blog&amp;sc_campaign=BLOG_DG_Evergreen&amp;sc_detail=blog&amp;sc_segment=blog&amp;sc_country=ww&amp;sc_itrackingcode=blog1001&amp;sc_medium=BLOG_DG_Evergreen_blog_ww_blog1001"><strong>Amazon Fire TV</strong></a>, discovering what its main components are and how they are tightly tied to the Media Streaming Interaction Model.</p><p>We’ll now take a close look into the first and most basic component of an Android App for Amazon Fire TV: the BrowseFragment.</p><h3>The BrowseFragment</h3><p>After we launch our freshly created <strong>TV Android App, </strong>built using the Android Studio App Wizard, we will encounter an interface that will look very similar to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0aUJWCS730zSPfRP.png" /></figure><p>Everything we see in this Activity is generated inside a class called <a href="https://developer.android.com/reference/android/support/v17/leanback/app/BrowseFragment.html?sc_channel=blog&amp;sc_publisher=blog&amp;sc_campaign=BLOG_DG_Evergreen&amp;sc_detail=blog&amp;sc_segment=blog&amp;sc_country=ww&amp;sc_itrackingcode=blog1001&amp;sc_medium=BLOG_DG_Evergreen_blog_ww_blog1001"><strong>BrowseFragment</strong></a>. The BrowseFragment creates an interface to allow the user to easily browse content within the app, most typically videos or movies.</p><p>The BrowseFragment itself is actually composed by two sub-fragments: the <strong>HeadersFragment </strong>and the <strong>RowsFragment</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-y96JDfJZ9bd7nKZ.png" /></figure><p>The <strong>HeadersFragment</strong> shows the header of each row of content, and it can be shown or hidden. Typically it contains the “category” of the content contained in that specific row.</p><p>The <strong>RowsFragment</strong> contains the rows of contents, usually displayed as video thumbnails. We will see how to populate these content rows using <strong>ObjectAdapters</strong>.</p><h3>Populating the BrowseFragment</h3><p>Content rows are horizontal lists populated through <strong>Adapters</strong>, which arrange the content using classes dedicated to define the look and feel of the app called <strong>Presenters.</strong></p><p>In order to populate a single row of content we will use an <em>ArrayObjectAdapter,</em> which we’ll call <strong>listRowAdapter</strong>. This adapter will be responsible for <strong>displaying the content of a single row</strong>.</p><p>For this example, let’s also assume we defined a class called “Movie” that contains all the information of a movie, like title and description.</p><p>Finally, we have a <em>HeaderItem</em> that we will use to define the <strong>header of the row</strong>. We could, for example, define the “Comedy” category.</p><p>Here is a visual representation of what a <strong>listRowAdapter </strong>+ <strong>HeaderItem</strong> would look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hPQIVdVeGlujlVte.png" /></figure><p>Let’s assume to have a method “SetupMovies” which will populate a list of movies that we can then pass to the Adapter. The code below is what we will use to populate a single row and define the header item.</p><pre>public class MainFragment extends BrowseFragment {<br><br>    private void loadRows() {<br><br>     //let’s generate the list of movies<br>        List list = MovieList.setupMovies();<br><br>     //The CardPresenter defines the UI of the items in the row<br>        CardPresenter cardPresenter = new CardPresenter();<br><br>     //Create the adapter for the row<br>        ArrayObjectAdapter listRowAdapter <br>                      = new ArrayObjectAdapter(cardPresenter);<br>        <br>	//let’s populate the Adapter<br>	for (Movie movie:list) {<br>            listRowAdapter.add(movie);<br>        }<br><br>        //Create the header of this row    <br>        HeaderItem header = new HeaderItem(0, &quot;Comedy&quot;);</pre><p>At the end of this, we have created and populated the Adapter and therefore the <strong>first row of the BrowseFragment</strong>.</p><p>Now, usually a Media Streaming App would have more than a single row of content, right? So we need to add <strong>multiple Adapters</strong> to our BrowseFragment. In order to do this, the easiest way is to have <strong>a higher-level Adapter</strong> that can manage multiple <em>ListRowAdapters. </em>We’ll call this class <em>RowsAdapter</em>.</p><p>The <em>RowsAdapter </em>will contain multiple <em>ListRowAdapters</em> and will have its own Presenter to define how the rows of content should be displayed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4PRLoyvyvuoHt5nc.png" /></figure><p>We will populate the RowsAdapter adding to it multiple listRows, combining listRowAdapters and relative HeaderItems. Note that to do that we use the class <em>ListRow</em> that receives <em>HeaderItem</em> and <em>ArrayObjectAdapter</em> as parameters.</p><pre>public class MainFragment extends BrowseFragment {<br> ...<br><br>    //create a RowsAdapter<br>     ArrayObjectAdapter rowsAdapter <br>		= new ArrayObjectAdapter(new ListRowPresenter());<br>     <br>    //add multiple rows  <br>     rowsAdapter.add(new ListRow(headerItem1, listRowAdapter1));<br>     rowsAdapter.add(new ListRow(headerItem2, listRowAdapter2));<br>     <br>     setAdapter(mRowsAdapter);</pre><p>When we call <em>setAdapter()</em> we populate all the rows in the BrowseFragment and it show us the main interface.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kK3vq8MiQnzwSe5D.png" /></figure><h3>In Part 4 of this Series: Presenters — How to Define the Look &amp; Feel of the Widgets in a Leanback-Enabled Android App.</h3><p>In the next episode of this series we’ll make a further step towards fully understanding how a Leanback-enabled project works. We’ll deep dive into the <em>Presenter </em>classes, understanding how we can define the look and feel of the widgets shown in the main interface of a Leanback-enabled Android App.</p><p>Stay tuned!<br> Mario Viviani (<a href="http://www.twitter.com/mariuxtheone">@mariuxtheone</a>)</p><p><em>Originally published at </em><a href="https://developer.amazon.com/public/community/post/TxJT0CW3JVUFDS/Developing-for-the-Living-Room-How-to-Build-an-Android-App-for-Amazon-Fire-TV-Pa"><em>developer.amazon.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a95b954c3f15" width="1" height="1" alt=""><hr><p><a href="https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-amazon-fire-tv-part-3-a95b954c3f15">Developing for the Living Room: How to Build an Android App for Amazon Fire TV — Part 3</a> was originally published in <a href="https://medium.com/amazon-appstore">Amazon Developers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Developing for the Living Room: How to Build an Android App for Fire TV — Part 2]]></title>
            <link>https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-fire-tv-part-2-8d59299d0b32?source=rss-760db44e8e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/8d59299d0b32</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[apps]]></category>
            <category><![CDATA[tvs]]></category>
            <dc:creator><![CDATA[Mario Viviani]]></dc:creator>
            <pubDate>Thu, 11 Aug 2016 16:18:05 GMT</pubDate>
            <atom:updated>2016-08-11T16:22:47.666Z</atom:updated>
            <content:encoded><![CDATA[<h4>ANATOMY OF A LEANBACK-ENABLED ANDROID APP FOR AMAZON FIRE TV</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yrPU8kW4Qo4YP-kE.png" /></figure><p>In the <a href="https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-fire-tv-part-1-6ae108106fd2#.xhx67ww8e">first part of this series</a> we learned how easy is to <strong>create a new Android App for </strong><a href="https://developer.amazon.com/public/solutions/devices/fire-tv?sc_channel=blog&amp;sc_publisher=blog&amp;sc_campaign=BLOG_DG_Evergreen&amp;sc_detail=blog&amp;sc_segment=blog&amp;sc_country=ww&amp;sc_itrackingcode=blog1001&amp;sc_medium=BLOG_DG_Evergreen_blog_ww_blog1001"><strong>Amazon Fire TV</strong></a> and how to run it for the first time using the Android Studio Wizard for Leanback-Enabled apps in a matter of a few minutes.</p><p>We’ll now deep dive into what <strong>the main components</strong> of this app are and how they interact with each other.</p><h3>Main Dependencies in a Leanback-Enabled Project</h3><p>After we have created a <strong>new TV project</strong> using the Android Studio Wizard, we can notice that the wizard has automatically included some libraries in our project dependencies.</p><p>Your <strong>build.gradle </strong>file will look like this:</p><pre>dependencies <br>{ compile fileTree(<strong>dir</strong>: <strong>&#39;libs&#39;</strong>, <strong>include</strong>: [<strong>&#39;*.jar&#39;</strong>]) <br>  compile <strong>&#39;com.android.support:leanback-v17:23.4.0&#39;</strong> <br>  compile <strong>&#39;com.android.support:recyclerview-v7:23.4.0&#39; <br>  </strong>compile <strong>&#39;com.android.support:appcompat-v7:23.4.0&#39; <br>  </strong>compile <strong>&#39;com.github.bumptech.glide:glide:3.4.+&#39;</strong> }</pre><p>Let’s understand why each one of these libraries is important in a Leanback-enabled project:</p><ul><li><strong>Leanback: </strong>The v17 Leanback Support Library is a standard Android support library focused on providing <strong>APIs and widgets to simplify the development of TV Apps</strong>. Most of the components used in a Leanback-enabled project are contained in this library.</li><li><strong>RecyclerView: </strong>This library provides the RecyclerView class. This class is used to <strong>display large datasets efficiently</strong> recycling views, improving performance and saving memory. A lot of the components of the Leanback Library rely on RecyclerView. The RecyclerView implements a very common programming pattern in Android which is the Viewholder pattern. This is important to master when developing for TV (<a href="https://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder?sc_channel=blog&amp;sc_publisher=blog&amp;sc_campaign=BLOG_DG_Evergreen&amp;sc_detail=blog&amp;sc_segment=blog&amp;sc_country=ww&amp;sc_itrackingcode=blog1001&amp;sc_medium=BLOG_DG_Evergreen_blog_ww_blog1001">more info on ViewHolder here</a>).</li><li><strong>AppCompat: </strong>The main purpose of the AppCompat library <strong>provides APIs, widgets and tools across multiple versions of Android</strong>. It’s useful to provide your Android application on multiple devices, in particular if you ship your app in a single .apk binary file both for handheld devices and for TV.</li><li><strong>Glide: </strong>An efficient open source media management and image loading framework for Android. It allows to <strong>efficiently decode, download and apply images</strong> from the cloud. It’s used by some components of the Leanback-enabled app to efficiently fetch thumbnails and images to display previews of the content in the TV app.</li></ul><p>All these components are the main building blocks to create a solid and consistent TV experience for users, and in the process of developing an App for Fire TV we will fully explore their capabilities and functions.</p><h3>The Media Streaming App Interaction Model</h3><p>Let’s see what the classic interaction model is when users want to play content using a Media Streaming App.</p><p>The user journey is composed by three main steps:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yrPU8kW4Qo4YP-kE.png" /></figure><ol><li><strong>Browse for Content</strong>: The user goes through the main catalogue of media, searching for interesting content to play.</li><li><strong>Read Description &amp; Details</strong>: When an item has caught the user’s interest, they will focus on reading the descriptions and details of the content in order to make a decision.</li><li><strong>Play</strong>: When the user has decided that’s the right content they will start playing the content.</li></ol><h3>The Main Components of a Leanback-Enabled Android App</h3><p>A Leanback-enabled Android App <strong>follows this interaction model</strong>, and in fact the three <strong>main components</strong> of a Leanback-enabled app are <strong>exactly covering the three main steps</strong> of the Media Streaming App interaction model:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*x69jyEydlAZw8YSo.png" /></figure><ol><li><strong>BrowseFragment:</strong> Allows to browse for content in the main app catalogue.</li><li><strong>DetailsFragment:</strong> Grants access to extended details for specific content selected in the BrowseFragment and to perform actions like “Play Content”.</li><li><strong>PlaybackOverlayFragment: </strong>This Fragment allows to overlay media controls on a full-screen media player.</li></ol><p>This <strong>1:1 mapping</strong> <strong>with the Media Streaming App interaction model</strong> provides developers a consistent project structure, which highly simplifies the app design and development process.</p><p>Using a <strong>well-defined app structure</strong> like the one provided by the Leanback approach also allows an <strong>easier onboarding process</strong> since there is a high chance that users will have already been interacting with Media Streaming Apps built following the same pattern.</p><h3>Part 3 of this Series: BrowseFragment A.K.A. Building the Main Activity of Your TV App</h3><p>In the next episode of this series we’ll deep-dive into the first of the main components of a Leanback-enabled app, the <strong>BrowseFragment</strong>. We will learn how to build the main interface of our TV app and how to make the streaming content available for browsing.</p><p>For more information about developing for the 10-foot experience, check out our <a href="https://developer.amazon.com/public/solutions/devices/fire-tv/docs/design-and-user-experience-guidelines?sc_channel=blog&amp;sc_publisher=blog&amp;sc_campaign=BLOG_DG_Evergreen&amp;sc_detail=blog&amp;sc_segment=blog&amp;sc_country=ww&amp;sc_itrackingcode=blog1001&amp;sc_medium=BLOG_DG_Evergreen_blog_ww_blog1001">Design and User Experience Guidelines</a>.</p><p>Mario Viviani (<a href="http://www.twitter.com/mariuxtheone">@mariuxtheone</a>)</p><p><em>Originally published at </em><a href="https://developer.amazon.com/public/community/post/Tx19U7LTUQJHI78/Developing-for-the-Living-Room-How-to-Build-an-Android-App-for-Fire-TV-Part-2"><em>developer.amazon.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8d59299d0b32" width="1" height="1" alt=""><hr><p><a href="https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-fire-tv-part-2-8d59299d0b32">Developing for the Living Room: How to Build an Android App for Fire TV — Part 2</a> was originally published in <a href="https://medium.com/amazon-appstore">Amazon Developers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Developing for the Living Room: How to Build an Android App for Fire TV - Part 1]]></title>
            <link>https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-fire-tv-part-1-6ae108106fd2?source=rss-760db44e8e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/6ae108106fd2</guid>
            <category><![CDATA[apps]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[tvs]]></category>
            <category><![CDATA[tv]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[mobile]]></category>
            <dc:creator><![CDATA[Mario Viviani]]></dc:creator>
            <pubDate>Thu, 11 Aug 2016 15:19:31 GMT</pubDate>
            <atom:updated>2016-08-11T16:32:51.606Z</atom:updated>
            <content:encoded><![CDATA[<h4>DEPLOY A “HELLO WORLD” MEDIA STREAMING APP ON FIRE TV IN A FEW MINUTES</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RIaGkxFQ1GyNqVLL.png" /></figure><p>Android is no longer just for Mobile devices. Even if it started as an OS designed for smartphones, it evolved during time. In 2011 with Android 3.0 Honeycomb, specifically designed for tablets, Android scaled up to bigger screens, and this changed the way users engaged with mobile devices. Recently, Android scaled up to an ever bigger screen: the TV.</p><p>All of a sudden, Android developers can start thinking about building native Android apps for a new and exciting use case, the 10-foot experience, where users sit down, relax and engage with content specifically designed for their TV.</p><h3>Amazon Fire TV and Fire TV Stick are Android devices</h3><p><a href="https://developer.amazon.com/appsandservices/solutions/platforms/android-fireos">Amazon Fire OS</a>, which animates all the most recent Amazon devices like Fire tablets, Amazon Fire TV and Amazon Fire TV Stick, is based on Android 5.0 Lollipop. This means that Fire OS is capable of running full-fledged Android apps: 85% of Android apps submitted to the Amazon Appstore just work! If your app relies on the Android framework APIs and related support libraries, there’s a high chance it can run on Fire OS. You can <a href="https://developer.amazon.com/public/resources/development-tools/app-testing-service">test your APK on our online App Testing Service</a> and discover in 90 seconds if your app is compatible with Fire OS! When it comes to Fire TV, we wanted to make life of developers easy. That’s the reason we decided to fully support the <a href="http://developer.android.com/tools/support-library/features.html#v17-leanback">Android Leanback Support library</a>. This make extremely easy for Android developers to deploy native media streaming apps for TV.</p><p>Let’s discover how.</p><h3>Creating a Media Streaming App Project</h3><p>When it comes to Media Streaming devices like Fire TV, one of the most common use case is to stream video content, like movies, TV Series or video sharing channels like YouTube. The Android Leanback Support Library contains all the main components and widgets to create an efficient and good-looking Media Streaming Android App. Android Studio provides an easy way to deploy a Leanback-enabled Android TV sample project. The great news is that this sample project works out of the box on Fire TV!</p><h3>Step 0 — Enable Developer Options and ADB over WiFi on Fire TV</h3><p>Before creating our project and deploying to the Amazon Fire TV, we need to enable Developer Options and Debug ADB on our Fire TV. To do this, navigate to the Settings of the Fire TV and select System-&gt;Developer Options.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aZIoqNIw1kgoe86P.png" /></figure><p>Now, we need to enable Developer Options and ADB. The best way of developing on Amazon Fire TV is to connect via WiFi to the device. This way we might even sit down on a couch and develop in front of our TV and start experiencing our TV app like our users will do! To enable ADB, just set ADB Debugging to ON.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*NOk9Q4IBygdbKGWY.png" /></figure><p>At this point, our Amazon Fire TV is ready to receive Android debug apps. The last thing we need is the IP ADDRESS of our Fire TV. We will use it to connect our development environment to the Fire TV over the network. To retrieve it, just go to Settings-&gt;System-&gt;About-&gt;Network and copy the IP Address.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5vpbO__ry0cMv_Lt.png" /></figure><p>On our computer connected to the same WiFi Network as the Amazon Fire TV, we can now open up our terminal and run the command</p><p>adb connect &lt;ip-address&gt;</p><p>where &lt;ip-address&gt; needs to be substituted with the Fire TV IP Address we have previously copied. Now we are connected to the Amazon Fire TV and we are good to go to deploy our first media streaming app!</p><h3>Step 1 — Create the Project through Android Studio Project Wizard</h3><p>The first step is to create a new Android project through the Android Studio Project Wizard.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/0*-YH_H766ZGm2rXkZ.jpg" /></figure><p>We select TV as form factor for our app, and we set Minimum SDK to API 21: Android 5.0 Lollipop. This is because, as we said earlier, Fire OS is based on Android 5.0.</p><p>In the Next Step, we select to add an Android TV Activity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*jmxacFmy6v8aAu3-.jpg" /></figure><p>We then can follow the normal final steps to finalize our TV project. When you click Finish at the end of the final step, Android Studio will generate a project which contains a full-fledged Leanback-enabled TV App!</p><h3>Step 2 — Run your first Amazon Fire TV App</h3><p>At this point you can already run your first Amazon Fire TV app! This is because the app that has been created by the Android Studio Wizard contains all the main components of a complete media streaming app. Just click the run button on Android Studio and if you are connected to your Fire TV, the app will appear on screen. The interface will look very similar to this one:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Z3DIwAb-kDdGFh70.png" /></figure><p>You will be able to navigate the content and the categories, see details of the content and launch media streaming. The application already contains a lot of additional features like animations, fragment support and memory optimisation for loading media streaming content. This could be an easy boilerplate for creating a much more complex and rich Fire TV App.</p><h3>In Part 2 of This Series: Understanding Components of a Leanback-Enabled Amazon Fire TV App</h3><p>This is the just first step in creating a complete Amazon Fire TV App. Before jumping in on modifying and personalizing this “hello world” app project, we need to understand what the main components of this app are so we can edit it appropriately.<br> <br>In the next parts of this Series we’ll analyse the main components like the BrowseFragment and DetailsFragment and will explain how to play and stream media content.</p><p><a href="https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-fire-tv-part-2-8d59299d0b32#.mjlwvhop6">Continue to Part 2</a></p><p>Mario Viviani (<a href="http://www.twitter.com/mariuxtheone">@mariuxtheone</a>)</p><p><em>Originally published at </em><a href="https://developer.amazon.com/public/community/post/Tx25A9SHBD7IFNB/Developing-for-the-Living-Room:-How-to-Build-an-Android-App-for-Fire-TV?sc_channel=blog&amp;sc_publisher=blog&amp;sc_campaign=BLOG_DG_Evergreen&amp;sc_detail=blog&amp;sc_segment=blog&amp;sc_country=ww&amp;sc_itrackingcode=blog1001&amp;sc_medium=BLOG_DG_Evergreen_blog_ww_blog1001"><em>developer.amazon.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6ae108106fd2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/amazon-appstore/developing-for-the-living-room-how-to-build-an-android-app-for-fire-tv-part-1-6ae108106fd2">Developing for the Living Room: How to Build an Android App for Fire TV - Part 1</a> was originally published in <a href="https://medium.com/amazon-appstore">Amazon Developers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Designing Native Android Apps for TV: How TVPlayer Designed for Amazon Fire TV]]></title>
            <link>https://medium.com/amazon-appstore/designing-native-android-apps-for-tv-how-tvplayer-designed-for-amazon-fire-tv-79c3801b60e7?source=rss-760db44e8e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/79c3801b60e7</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[tvs]]></category>
            <category><![CDATA[design]]></category>
            <dc:creator><![CDATA[Mario Viviani]]></dc:creator>
            <pubDate>Wed, 13 Apr 2016 09:10:03 GMT</pubDate>
            <atom:updated>2016-04-13T09:10:16.035Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*F1yr5kRsBHZ2vNWK.jpg" /></figure><p><strong>Design </strong>is one of the pillars of the success of a mobile app, along with the development process and the distribution channels. Defining how the users will interact with your app is as important as implementing the features of the app and integrating an efficient monetization model. Also, when it comes to <strong>designing an interface for a new interaction model like TV</strong>, a lot of the patterns that work well with smartphone and tablet interfaces needs to be rethought to take advantage of the big screen and the input model provided through the remote.</p><h3>TVPlayer on Fire TV</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*C6I2Hu2HtWFTIPO7.png" /></figure><p>This was a task that didn’t scare the developer of <strong>TVPlayer</strong>, one of most successful TV apps on Fire TV and Fire TV Stick in UK.</p><p><strong>TVPlayer is a media streaming native Android app that let you watch Live TV on your Amazon and Android devices for free</strong>, granting access to many of the UK’s most watched channels. TVPlayer has been part of the launch of Fire TV in 2014, and its success continued during 2015 and 2016.</p><p><strong>Simplestream</strong>, the team of developers and designers behind TVPlayer, took the task of creating a TV-first experience really seriously.</p><p>We asked <strong>Lewis Arthur</strong>, Operations Director, and <strong>Michael Jordan</strong>, Android Developer from Simplestream to share their insights about the design process of their Fire TV app, here’s what they told us.</p><h3>The Design Process: From Mobile to TV</h3><p>Before landing on Fire TV, TVPlayer was already available on Amazon Appstore for Fire Tablets and Android devices. The designers of mobile version of the app focused their attention <strong>on usability and easiness of access to content.</strong> They adopted <strong>big bold images</strong> on the layout of the landing Activity and a scrollable lists of Live TV channels in the main tabs. The designers decided to maintain a “light” theme in order to make content stand out and also to keep the theming consistent across different platforms.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/487/0*AncB4Dlaqvw7nWvV.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/487/0*WBoJB2UYacca0TzG.png" /></figure><p>When it came to providing the <strong>support for tablets</strong>, the design needed to be rethought to take better advantage of the big screen. The <strong>main layout</strong> was edited to <strong>showcase more content</strong> to users straight from the main Activity. This is a good strategy to both catch the attention of the user and provide quick access to multiple relevant content.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*s8YaswLN8cFMLkEi.png" /></figure><p>For <strong>the first implementation of the Fire TV App</strong>, the main interface needed to go through a new lifting. The two major differences with phone and tablet UI and UX design, when it comes to design for TV, are embedded in the nature itself of the TV: It has a <strong>big display</strong> and it doesn’t provide a <strong>touch interface</strong>, since all the interactions happen through the remote.</p><p>Simplestream went <strong>all-in with big bold images</strong> on the main layout of their first Fire TV App. The “Home”, “On Now” and “Channels” Tab <strong>were moved on the side</strong>, and <strong>the font of the text has been greatly</strong> increased in size to make it clearly readable even from a distance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uER5i3hZGQnhcnw4.png" /></figure><p>This first iteration of the Fire TV main interface of app had a clear feeling of the tablet version, with a grid layout showing the main categories of channels.</p><h3>Developing for Fire TV is Fast and Easy: 4 weeks from Mobile to TV</h3><p>When we asked Simplestream <strong>how long did it take</strong> for them to go from the mobile version of the app to the first version of the Fire TV app, they told us that the transition took <strong>approximately 4 weeks</strong>. Since Fire TV is a full-fledged Android device, running <a href="https://developer.amazon.com/public/solutions/devices/fire-tv/overview/developing-apps-and-games-for-amazon-fire-tv">Fire OS 5</a> which is based on Android Lollipop, the transition was fast and smooth: the <strong>main components of the mobile and TV version are the same</strong>, and keeping the interface separated from the main under-the-hood app architecture provided the developers <strong>enough flexibility to iterate fast</strong> and get to a complete TV app in a matter of a few weeks.</p><h3>The Renovated Interface on Fire TV</h3><p>After more than a year of availability of the app on Fire TV and after collecting enough feedback about user interaction from the app analytics, the developers and designers of TVPlayer decided it was time to provide a <strong>new interface</strong> for their Fire TV app. The idea was to make the app more efficient, to provide content in the best way possible to TV users and also to introduce new features in the app.</p><p>The key new development was the inclusion of a monthly subscription in app purchase — diversifying the app to include both free and paid content. The Simplestream designers went through a <strong>deep competitor analysis to understand what are the current trends in Media Streaming app design</strong> and how to design an interface that would maintain consistency even <strong>cross-platform and cross-devices.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-AkSc_NnXYNLbKrf.jpg" /></figure><h3>Choosing the Right Colour Theme to Make the App Easy to the Eyes</h3><p>First of all, the designers decided to go for a <strong>dark colour theme</strong> on the Fire TV. This was a decision taken when designing the previous version of the Fire TV app, but in the updated interface they went the extra mile, darkening out most UI components.<strong> The main purpose of the dark theme is to avoid tiring the eyes of the user</strong> when browsing for content, and therefore provide a more relaxing user experience. This was particularly important for the loading screen of the app, which was changed to be black to avoid ‘burned retinas’.</p><blockquote><strong>Avoid tiring the eyes of the user.</strong></blockquote><p>When developing apps for TV <strong>the usage of light and bright themes needs to be carefully considered</strong> since potentially a lot of the usage of this apps could happen in the evenings, when there’s usually less natural light and therefore <strong>a bright UI might hurt user experience</strong> and lead to abandonment of the app over time.<strong> Branding</strong> through colours is also very important. For TVPlayer, the use of <strong>blue as highlighting colour</strong> helped maintain <strong>branding consistency</strong> from previous version and across different platform. Blue indicates free content and Pink paid ‘Plus’ content.</p><h3>Content Pagination through ViewPager</h3><p>The developers of TVPlayer wanted a <strong>high-performing</strong> UI, but also wanted to maintain <strong>brand identity</strong> and provide a unique user experience. That’s the reason they decided not to follow the classical <a href="http://developer.android.com/tools/support-library/features.html#v17-leanback">Android Leanback interface</a> and they build their own <strong>layouts and navigation system</strong>.</p><p>The TVPlayer Fire TV App <strong>main layout</strong> relies on a custom implementation of the <a href="http://developer.android.com/training/animation/screen-slide.html">ViewPager</a> component. The ViewPager is a layout manager that allows to easily flip left and right through pages of content. In TVPlayer, the horizontal scrolling through pages of programs and channels has been implemented through a <strong>custom implementation of ViewPager</strong>, with every page containing a set of custom views.</p><h3>Improve Quick Access to Content Drives More Engagement</h3><p>The grid view created in the previous version has been improved, with <strong>more spacing between items</strong> and introducing <strong>horizontal scrolling instead of vertical</strong>. Fast content navigation has been introduced as well: <strong>page navigation indicators</strong> can be selected by the user and allow fast scrolling through pages. The page navigation indicators are connected to the ViewPager and determine what page is currently shown or needs to be shown next.Faster scrolling means giving faster access to more content to the user, and therefore it helps user engagement and retention.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/398/0*yEy2wOan57S_ShDy.PNG" /></figure><h3>The Mosaic Interface: Faster Access to Content, Better Monetisation</h3><p>The final result is a <strong>mosaic interface</strong> that gives quickly access to multiple channels. The usage of Custom Android Views and Adapters enabled the developers to <strong>have control on and</strong> <strong>mix Free and “Plus” content in the same interface</strong>, improving the access to premium content through <strong>In-App Purchase</strong> and therefore improving revenue generation. TVPlayer moved from a 100% Free approach in the first Version of the app to the <strong>introduction of In-App Purchase in the new version</strong>. Maintaining a clean user interface and separating the logics behind UI and main app components made the transition to the new model easier and faster, and allowed a faster iteration process in order to deliver a <strong>great customer experience</strong> and an <strong>improved monetisation strategy.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*iHns9GwlNwn2BGyU.jpg" /></figure><h3>Start Creating Apps for Amazon Fire TV Today!</h3><p><strong>Amazon Fire TV</strong> and <strong>Fire TV Stick</strong> offer you a great opportunity to your Android or HTML5 app to reach more users. It’s easy to port Mobile Android apps to Fire TV following the documentation on the Amazon Appstore Developer Portal! Here’s some useful links for you:</p><p><a href="https://developer.amazon.com/public/solutions/devices/fire-tv/overview/developing-apps-and-games-for-amazon-fire-tv">Amazon Fire TV: Reach new audiences by bringing your apps and games to the Iiving room</a></p><p><a href="https://developer.amazon.com/public/solutions/devices/fire-tv/overview/getting-started-developing-apps-and-games-for-amazon-fire-tv">Getting Started Developing Apps and Games for Amazon Fire TV</a></p><p><a href="https://developer.amazon.com/public/solutions/devices/fire-tv/overview/developing-apps-and-games-for-amazon-fire-tv">Developing Apps and Games for Amazon Fire TV</a></p><p><strong><em>Stay Tuned for Part 2 of the “Get Your Android App Ready for the 10-Foot Experience” series: Tools, Libraries and Best Practices for Developing a Successful Fire TV App. Simplestream shared with us more insights on their Fire TV App development process! In the next episode of this Series, we’ll unveil what are Tools, Libraries and Best Practices that Simplestream put in place when developing and testing the new version of TVPlayer. Stay tuned!</em></strong></p><h4>Mario Viviani (<a href="https://twitter.com/mariuxtheone">@mariuxtheone</a>)</h4><p><em>Originally published at </em><a href="https://developer.amazon.com/public/community/post/TxKPNSL5CWRR17/Get-Your-Android-App-Ready-for-the-10-Foot-Experience-Part-1-TVPlayer-and-Design?utm_content=buffer225be&amp;utm_medium=social&amp;utm_source=twitter.com&amp;utm_campaign=buffer"><em>developer.amazon.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=79c3801b60e7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/amazon-appstore/designing-native-android-apps-for-tv-how-tvplayer-designed-for-amazon-fire-tv-79c3801b60e7">Designing Native Android Apps for TV: How TVPlayer Designed for Amazon Fire TV</a> was originally published in <a href="https://medium.com/amazon-appstore">Amazon Developers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optimize Your Android App User Interface for Amazon Fire, Fire HD 8 and Fire HD 10 Tablets]]></title>
            <link>https://medium.com/amazon-appstore/optimize-your-android-app-user-interface-for-fire-fire-hd-8-and-fire-hd-10-tablets-a3821fd0bcaa?source=rss-760db44e8e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/a3821fd0bcaa</guid>
            <category><![CDATA[ux]]></category>
            <category><![CDATA[design]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Mario Viviani]]></dc:creator>
            <pubDate>Wed, 24 Feb 2016 15:43:40 GMT</pubDate>
            <atom:updated>2016-02-24T17:43:56.809Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F7d4I07J4OcEJkaVJ_iwOw.png" /></figure><p>The new line of <a href="https://developer.amazon.com/public/solutions/devices/fire-tablets">Fire tablets</a> set an <strong>entirely new standard</strong> for low-cost tablets, with the <strong>Fire 7’’ Tablet</strong> <strong>being sold at less than $50</strong>. Amazon has already sold millions of tablets, so it’s a great time for you to submit your apps and games to the Amazon Appstore.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*6dmKXYTeSXyDEVCl9_LLOA.png" /></figure><p>In order to provide a great experience to users, the <strong>first thing you have to think about is the user interface (UI) of your app</strong>. Designing the UI and UX (user experience) of an app could become a very challenging and time-consuming task, but there are some <strong>simple steps</strong> that you can follow to make your app look great on the new Fire tablets. Also, keep in mind that Fire OS 5, available on all the new Fire tablets, is based on Android 5.1, so all the standard approaches to develop Android apps UI also apply to Fire OS apps!</p><p>In this post, we’ll deep dive into understanding <strong>screen resolution and density</strong> of Fire tablets, disclose the best strategies to properly <strong>create and size the layouts and graphical resources of your app</strong>, teach you how to provide a <strong>correctly-size app icon for Fire OS 5</strong>, and how to explicitly declare support for Fire tablets in your Android app manifest.</p><h3>Understanding Screen Resolution and Density on Fire Tablets</h3><p>The general best practice for Android apps is to provide a <strong>flexible layout</strong> which is able to adapt to different resolutions. You can do this in different ways.</p><p>For example, you can use <strong>RelativeLayout</strong> when creating the hierarchy of containers in your XML layout, since using RelativeLayout elements can be placed and sized relative to other elements. Also remember to use <em>wrap_content</em> and <em>match_parent</em> XML attributes when defining the <strong>height </strong>and <strong>width</strong> of your elements, where possible, since this will allow <strong>more flexibility</strong> when creating the layouts for different screen sizes.</p><p>It’s entirely up to you to decide how to structure the layout of your app, just keep in mind that the more flexible you create your layout, the better it will support different screen formats. You can find more information about how to properly define a flexible layout here: <a href="https://developer.amazon.com/public/solutions/devices/fire-tablets/app-development/01--screen-layout-and-resolution#Use%20Flexible%20Layout">Use Flexible Layout</a></p><h4>Screen Density is Key</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*h-2IPPJjhWe973Xwn_ov6Q.png" /><figcaption>Fire Tablets have different screen densities</figcaption></figure><p>An important metric to consider when defining your flexible app layout and graphical assets is <strong>screen</strong> <strong>density</strong>. Density represents how many pixels are condensed in a certain amount of space on a device display. It’s important to correctly understand and use density-specific metrics, since directly indicating pixels could result in very differently-sized elements on different screens, caused by the different amount of pixels condensed in the same physical area.</p><p>When you are defining the size of the elements of your layout, remember to use <strong>density-independent pixels (dp)</strong> rather than absolute pixels. If you use density-independent pixels you will secure a layout component that will approximately<strong> have the same physical size </strong>across different devices, no matter the screen density and the size of the display. This is because at runtime, the system automatically scales the dp units based on the actual pixel density of the display. The baseline density is 160 DPI (defined “medium density”) and the scaling for dp is calculated accordingly.</p><p>It’s quite simple to calculate the amount of pixels (px), density-independent pixels (dp) or Dots Per Inch (DPI) available on a display, using the formula:</p><blockquote><em>px = dp * (DPI / 160)</em></blockquote><p>Android provides some generalized<strong> density qualifiers</strong>, which are really helpful to group similar devices together. This qualifiers depend on the density of the screen, and help you create graphical assets which can be re-used across multiple devices.</p><p>There are <strong>two densities categories</strong> you should address <strong>when targeting Fire tablets</strong>:</p><ul><li>medium (<strong>mdpi</strong>) for Fire and Fire HD10.</li><li>tv (<strong>tvdpi</strong>) for Fire HD8. However, tvdpi is to be treated as a <strong>secondary density category</strong>, so developers addressing Fire HD 8 should create graphical assets for the higher <strong>hdpi </strong>category, the graphical assets will be automatically scaled accordingly.</li></ul><p>The explanation for this is Fire HD 8 having the same resolution of the HD 10 (1280x800 px) but the display on HD 8 is actually smaller (8’’) and so it has a higher pixel density (189 DPI on Fire HD 8 against 149 DPI on Fire HD 10).</p><p>As a reference, here’s the actual physical DPI metric for the three new tablets:</p><ul><li>Fire: 171 DPI</li><li>Fire HD 8: 189 DPI</li><li>Fire HD 10: 149 DPI</li></ul><p>Screen density is fundamental when designing layouts for different screen sizes. It’s always good practice to verify the density of the devices you want to specifically target with your layouts in order to understand what qualifier you need to use for your resources.</p><h3>General Screen Size Qualifiers for Fire Tablets</h3><p>Another strategy to define layout or assets is to use the <strong>general size qualifiers </strong>defined by the Android platform. They are based on the <strong>physical screen size</strong> (small, normal, large, xlarge).</p><p>Since this metric is defined with 4’’/5’’ phones as a base point for normal, tablets usually fall under large or xlarge qualifiers. That’s the case for Fire tablets.</p><p>Here’s the general size qualifiers for Fire Tablets:</p><ul><li><strong>large</strong> for Fire and Fire HD 8</li><li><strong>xlarge</strong> for Fire HD 10</li></ul><p>Using general screen size qualifiers as a metric is a good approach when conceiving flexible layouts, because it allows you to target ranges of similar devices. However, this approach does have one downside; its categories are quite broad. For example, “large” goes from 4’’ to 8’’, and you would probably want to provide radically different layouts for a 4’’ phone and an 8’’ tablet. A good way to define layouts is to use screen smallest width qualifier ad described in the next section.</p><h3>Using Screen Smallest Width to Define Layouts</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*rUkNul1gNkdukq4XYT_QTw.png" /></figure><p>One of the most effective strategies when defining layouts is to use the <strong>screen smallest width qualifier</strong>. The smallest width qualifier, added in Android 3.2 (Honeycomb), defines the width of the smallest side of a tablet in dp units. This strategy combines the two previous approaches — density and screen size qualifiers — so it’s a good way to specifically target layouts from a screen-and-density-wise perspective. Using smallest width is generally preferable to just using general screen size qualifiers as it enables you to <strong>more specifically target a given screen type</strong>.</p><p>This qualifier takes the form swXXXdp where XXX is the given width in density-independent pixels.</p><p>Let’s use an example, to demonstrate how it works: if we want to target Fire HD 10 using Smallest Width qualifier, we need to calculate the size of the smallest width in dp. Let’s apply the formula:</p><blockquote><em>px = dp * (DPI / 160)</em></blockquote><p>It becomes, applying Fire HD 10 parameters:</p><p><em>800 (px of smallest side) = dp * (149 (DPI of smallest side) /160)</em></p><p>This results in dp= 800/0.9 = <strong>860 dp (approximated)</strong> on smallest width, so Fire HD 10 falls under sw800dp smallest width qualifier.</p><p>The smallest width qualifiers for Fire Tablets are as follows:</p><ul><li><strong>sw600dp</strong> for Fire and Fire HD 8</li><li><strong>sw800dp </strong>for Fire HD 10</li></ul><p>Using screen smallest width qualifiers reduces the time defining multiple screen layouts and allows the creation of flexible layouts that target really specific screen formats. If you are interested in more information about the different qualifiers, take a look at Provide Multiple Layouts for Different Screens</p><h3>Build Your Layouts for Fire Tablets in Multiple Ways</h3><p>As we’ve seen, there are <strong>multiple ways</strong> of targeting different screen sizes, and different approaches possible even with the three Fire Tablets. You can <strong>combine the usage of density/screen size qualifiers and screen smallest width qualifier</strong>.</p><p>To help you define your strategy for layout and assets, here’s a simple scheme that explains which metric applies to each tablet, and which are shared across more than one:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LYWgRTZxUeujZ07J.png" /></figure><p>Here is also the complete table of the <strong>metrics about the Fire tablets </strong>we discussed in this article:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*Jg3jrOY67QffD69Bw_Z_GA.png" /></figure><p>For example, you could decide to use the density category for the graphical assets and smallest width qualifier for the layouts for your Fire tablets-targeted app, which would convert in having the following in your resources folder:</p><p><em>res/drawable-hdpi</em> (Fire HD 8)</p><p><em>res/drawable-mdpi</em> (Fire and Fire HD 10)</p><p><em>res/layout-sw600dp</em> (Fire and Fire HD 8)</p><p><em>res/layout-sw800dp</em> (Fire HD 10)</p><p>If you want more information about how to create a layout for the new tablets and for the previous generation of devices, take a look here: <a href="https://developer.amazon.com/public/solutions/devices/fire-tablets/app-development/01--screen-layout-and-resolution">Screen Layout and Resolution on Fire Tablets</a></p><h3>App Icons for Fire Tablets</h3><p>Even though the new tablets have mdpi (Fire and Fire HD 10) and tvdpi (Fire HD 8) densities, the icon assets that are fetched for apps on these devices respond to different resolutions. This is because in Fire OS 5 the icons in the “Recent” tab of Fire OS Launcher are bigger than the launcher icons displayed in the “Home” tab, so it needs to be <strong>higher quality </strong>than the launcher icons.</p><p>Here’s a table of the density of fetched launcher icons:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/656/1*CD2y5V-YEH3ZE-CTjiu4Bg.png" /></figure><p>For more information about icons, take a look at <a href="https://developer.amazon.com/public/solutions/devices/fire-tablets/app-development/launcher-icon-guidelines">Launcher Icon Guidelines</a></p><p>If you’re interested in how to create great app icons for your Fire OS apps, take a look at the advices given by a great UX Designer here <a href="https://developer.amazon.com/public/community/post/Tx1JKZU0SL4Z685/Tools-to-Help-You-Make-Great-App-Icons">Tools to Help You Make Great App Icons</a></p><h3>Declare Support for Fire Tablets in Your Manifest</h3><p>After you have defined a flexible layout and provided alternative layouts and graphics resources to accommodate different screen sizes, you should also declare the supported screen sizes in your Android manifest with the &lt;supports-screens&gt; element for your app to be recognized as officially supporting Fire Tablets.</p><p>For example, adding the following snippet to your Manifest, you will target only “large” and “xlarge” screens, and those with a minimum width of 600dp, in other words, you will specifically targeting only the Fire tablets.</p><pre>&lt;supports-screens</pre><pre>android:smallScreens=”false”</pre><pre>android:normalScreens=”false”</pre><pre>android:largeScreens=”true”</pre><pre>android:xlargeScreens=”true”</pre><pre>android:requiresSmallestWidthDp=”600&quot;/&gt;</pre><p>If you want to target a broader scope of devices, including non-Amazon devices, you can add something like this:</p><pre>&lt;supports-screens</pre><pre>android:smallScreens=”true”</pre><pre>android:normalScreens=”true”</pre><pre>android:largeScreens=”true”</pre><pre>android:xlargeScreens=”true”<em>/&gt;</em></pre><p>This will still ensure your app will target Fire Tablets, but at the same time will support other device formats.</p><h3>Submit Your App to Amazon Appstore and Start Targeting Millions of Fire Tablets and Android Devices</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c3M64I6sIdr9Tg6ZLFIUJw.png" /></figure><p>Once your app is fully tuned for Fire tablets, it’s time for publishing it and start engaging with millions of users!</p><blockquote>If you haven’t already, <a href="https://developer.amazon.com/login.html">register for a free Amazon Developer Account</a> and submit your app!</blockquote><p>Here you can find more information about designing apps for Fire Tablets and publishing apps on Amazon Appstore:</p><h4><a href="https://developer.amazon.com/public/solutions/devices/fire-tablets/app-development/launcher-icon-guidelines">Developing for Fire Tablets</a></h4><h4><a href="https://developer.amazon.com/public/solutions/devices/kindle-fire/app-development/setting-up-your-development-environment-for-fire-tablets">Setting Up your Development Environment for Fire Tablets</a></h4><h4><a href="https://developer.amazon.com/public/resources/development-tools/ide-tools/tech-docs/04-creating-your-first-kindle-fire-app">Creating your First Fire Tablet App</a></h4><h4><a href="https://developer.amazon.com/public/support/submitting-your-app">Submitting your Apps on Amazon Appstore</a></h4><p>Mario Viviani - Technology Evangelist at Amazon Appstore</p><p>(Follow me on Twitter! <a href="https://twitter.com/mariuxtheone">@mariuxtheone</a>)</p><p><em>Originally published at </em><a href="https://developer.amazon.com/public/community/post/TxYBJQW9EWTNES/Optimize-Your-Android-App-UI-for-Fire-Tablets"><em>developer.amazon.com</em></a></p><h3>Liked this post? Check out the <a href="https://developer.amazon.com/appsandservices/community/blog">Amazon Appstore Developer Blog</a> and <a href="http://dev.amazonappservices.com/BlogSubscription.html">Subscribe to our Weekly Blog Roundup</a></h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a3821fd0bcaa" width="1" height="1" alt=""><hr><p><a href="https://medium.com/amazon-appstore/optimize-your-android-app-user-interface-for-fire-fire-hd-8-and-fire-hd-10-tablets-a3821fd0bcaa">Optimize Your Android App User Interface for Amazon Fire, Fire HD 8 and Fire HD 10 Tablets</a> was originally published in <a href="https://medium.com/amazon-appstore">Amazon Developers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to add Toolbar to an Activity which doesn’t extend AppCompatActivity]]></title>
            <link>https://medium.com/google-developer-experts/how-to-add-toolbar-to-an-activity-which-doesn-t-extend-appcompatactivity-a07c026717b3?source=rss-760db44e8e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/a07c026717b3</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[libraries]]></category>
            <category><![CDATA[support]]></category>
            <dc:creator><![CDATA[Mario Viviani]]></dc:creator>
            <pubDate>Thu, 23 Apr 2015 14:14:09 GMT</pubDate>
            <atom:updated>2015-04-23T14:14:09.052Z</atom:updated>
            <content:encoded><![CDATA[<h2>How to add Toolbar</h2><h2>to an Activity</h2><h2>which doesn’t extend AppCompatActivity</h2><h4>Android Support Library 22.1 introduces AppCompatDelegate</h4><p>The new <strong>AppCompat support library</strong> version <strong>22.1</strong> introduces a lot of cool new features which allows to easily add Material Design/API 21+ features to our old, non-AppCompat Activities.</p><p>We know that extending <a href="https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html"><strong>AppCompatActivity</strong> </a>is the best way to go, but sometimes you need to go old-school, right? ;-)</p><p>You can use the new <a href="http://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html"><strong>AppCompatDelegate</strong></a> component provided by the Support Library to easily <strong>add a Toolbar to your Activity</strong>.</p><h3>1) Add the Toolbar widget to the activity layout</h3><p>ActionBar is now deprecated and it should be replaced by the <a href="https://developer.android.com/reference/android/support/v7/widget/Toolbar.html"><strong>Toolbar</strong></a><strong> </strong>widget, which allows much more UI flexibility and also allows your Activity to be easily compliant with Material Design UI Patterns.</p><p>Since we’re using the Support Library here, <strong>we’ll use the support version of Toolbar</strong> (which, otherwise, is only available from API 21+).</p><p>Add it to your xml layout like this:</p><pre>&lt;<strong>android.support.v7.widget.Toolbar</strong><br><br>    <strong>android:id=</strong>&quot;@+id/my_awesome_toolbar&quot;<br>    <strong>android:layout_height=</strong>&quot;@dimen/abc_action_bar_<br>default_height_material&quot;<br>    <strong>android:layout_width=</strong>&quot;match_parent&quot;<br>    <strong>android:minHeight=</strong>&quot;@dimen/abc_action_bar_<br>default_height_material&quot;<br>    <strong>android:background=</strong>&quot;?attr/colorPrimary&quot;<br><br>    /&gt;</pre><h3><strong>2) Modify your Theme</strong></h3><p>Since we’re embedding the Toolbar in the layout, we need to use a ActionBar-free theme.</p><p>So, be sure to use a <strong>Theme.AppCompat.NoActionBar </strong>theme in your <em>styles.xml</em>.</p><p><strong>ProTip:</strong> Add the <strong>Material Design color items</strong> to your theme, to easily allow Toolbar and Status Bar Tinting (only on 21+ for Status Bar).</p><pre><em>&lt;!-- Base application theme. --&gt;<br></em>&lt;<strong>style name=&quot;AppTheme&quot; parent=&quot;Theme.AppCompat.NoActionBar&quot;</strong>&gt;<br><br>    <em>&lt;!-- colorPrimary is used for coloring the Toolbar --&gt;<br>    </em>&lt;<strong>item name=&quot;colorPrimary&quot;</strong>&gt;#3F51B5&lt;/<strong>item</strong>&gt;<br><br>    <em>&lt;!-- colorPrimaryDark is used for coloring the status bar --&gt;<br>    </em>&lt;<strong>item name=&quot;colorPrimaryDark&quot;</strong>&gt;#303F9F&lt;/<strong>item</strong>&gt;<br><br>    <em>&lt;!-- colorAccent is used as the default value for colorControlActivated<br>         which is used to tint widgets --&gt;<br>    </em>&lt;<strong>item name=&quot;colorAccent&quot;</strong>&gt;#FFAB40&lt;/<strong>item</strong>&gt;<br><br>&lt;/<strong>style</strong>&gt;</pre><h3>3) Add AppCompatDelegate to your Activity</h3><p>The <a href="http://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html"><strong>AppCompatDelegate</strong></a> is a delegate that provides the AppCompatActivity features to your Activity, and can only be created using the <a href="http://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html#create(android.app.Activity,%20android.support.v7.app.AppCompatCallback)"><em>create()</em> </a>method, which takes 2 arguments: the <em>Activity </em>itself, and an <a href="http://developer.android.com/reference/android/support/v7/app/AppCompatCallback.html"><em>AppCompatCallback</em></a>.</p><p>So, first of all, let’s have our activity implement <em>AppCompatCallback</em> (we can leave these methods empty, in this demo).</p><pre><strong>public class </strong>MainActivity <strong>extends </strong>Activity <strong>implements </strong>AppCompatCallback {<br><br>    @Override<br>    <strong>public void </strong>onSupportActionModeStarted(ActionMode mode) {<br>      //let&#39;s leave this empty, for now<br>    }<br><br>    @Override<br>    <strong>public void </strong>onSupportActionModeFinished(ActionMode mode) {   <br>      // let&#39;s leave this empty, for now<br>    }</pre><p>Then, in the onCreate() of our Activity:</p><ol><li>Create the <strong>AppCompatDelegate </strong>with <a href="http://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html#create(android.app.Activity,%20android.support.v7.app.AppCompatCallback)"><strong>AppCompatDelegate.create()</strong></a></li><li>Call <a href="http://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html#onCreate(android.os.Bundle)"><strong>AppCompatDelegate.onCreate()</strong></a><strong> <em>(</em></strong><em>There are some Activity lifecycle methods which should be proxied to the delegate)</em></li><li>Inflate the layout with <a href="http://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html#setContentView(int)"><strong>AppCompatDelegate.setContentView()</strong></a></li><li>Add the Toolbar to the delegate with <a href="http://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html#setSupportActionBar(android.support.v7.widget.Toolbar)"><strong>AppCompatDelegate.setSupportActionbar()</strong></a></li></ol><pre>private AppCompatDelegate <strong>delegate</strong>;<br><br>@Override<br><strong>protected void </strong>onCreate(Bundle savedInstanceState) {<br>    <strong>super</strong>.onCreate(savedInstanceState);<br><br>    <em>//let&#39;s create the delegate, passing the activity at both arguments (Activity, AppCompatCallback)<br>    </em><strong>delegate </strong>= AppCompatDelegate.<em>create</em>(<strong>this</strong>, <strong>this</strong>);<br><br>    <em>//we need to call the onCreate() of the AppCompatDelegate<br>    </em><strong>delegate</strong>.onCreate(savedInstanceState);<br><br>    <em>//we use the delegate to inflate the layout<br>    </em><strong>delegate</strong>.setContentView(R.layout.<strong><em>activity_main</em></strong>);<br><br>    <em>//Finally, let&#39;s add the Toolbar<br>    </em>Toolbar toolbar= (Toolbar) findViewById(R.id.<strong><em>my_awesome_toolbar</em></strong>);<br>    <strong>delegate</strong>.setSupportActionBar(toolbar);<br>}</pre><p><strong>ProTip: </strong>An <a href="http://developer.android.com/reference/android/app/Activity.html">Activity</a> can only be linked with one <a href="http://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html">AppCompatDelegate</a> instance, so the instance returned from <a href="http://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html#create(android.app.Activity,%20android.support.v7.app.AppCompatCallback)">create()</a> should be kept until the Activity is destroyed.</p><p>Now you’re done! Run your app and your Activity will magically have a Toolbar, and totally looks like a real Material Design AppCompatActivity!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/261/1*_biMWZ3U7PnY6Y3kPv7mOg.jpeg" /></figure><p><strong>Quite easy, uh? ;-)</strong></p><p>Again, this is for edge cases, and you should always consider the usage of AppCompatActivity before going for the AppCompatDelegate.</p><p>Be sure to check out the <a href="http://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html">complete reference of <strong>AppCompatDelegate</strong></a>, there are a lot of methods that you should take care of wrapping in your Activity if you want it to fully act as an AppCompatActivity.</p><p><a href="https://github.com/Mariuxtheone/AppCompatDelegateDemo"><strong>Here’s a Github Project that demonstrate what is described in this post.</strong></a></p><p><em>If you liked this post, </em><a href="https://plus.google.com/+MarioViviani/posts"><strong><em>follow me on Google+ </em></strong></a><em>or on</em><strong><em> </em></strong><a href="https://twitter.com/Mariuxtheone"><strong><em>Twitter</em></strong></a><strong><em> </em></strong><em>for more informations about </em><strong><em>Android!</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a07c026717b3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-developer-experts/how-to-add-toolbar-to-an-activity-which-doesn-t-extend-appcompatactivity-a07c026717b3">How to add Toolbar to an Activity which doesn’t extend AppCompatActivity</a> was originally published in <a href="https://medium.com/google-developer-experts">Google Developer Experts</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>