Pangu
Pangu

Reputation: 3819

Create an overlay over a fragment in Android TV?

I'm starting to learn how to build an Android TV for the first time so I apologize for a likely obvious answer to this question. Basically, I'm trying to replicate how the Android TV Youtube App displays when there is no internet connectivity as shown:

enter image description here

YouTube shows the main fragment (I believe) in the back with no videos, and there seems to be a transparent overlay with an ImageView, TextView, and Button. Once there is internet connectivity and the user taps the Retry button, it loads the video content and the overlay disappears.

I'm trying to achieve this exact same technique, and with internet connectivity, I currently have this:

enter image description here

With no internet connectivity, I'd like to show something similar to how YouTube does.

Here's some of my basic code:

MainActivity.java:

public class MainActivity extends Activity
{
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_browse_fragment"
    android:name="com.ui.MainFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ui.MainActivity"
    tools:deviceIds="tv"
    tools:ignore="MergeRootFrame" />

MainFragment.java:

public class MainFragment extends DetailsFragment
{
    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        setupUIElements();

        setupThings();
    }

    public void setupThings()
    {
        if (isOnline() == true)
        {
            prepareBackgroundManager();
            loadRows();
            setupEventListeners();
        }
        else
        {
            // I don't know what to do here???
        }
    }

    public Boolean isOnline()
    {
        try
        {
            Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
            int returnVal = p1.waitFor();
            boolean reachable = (returnVal == 0);

            return reachable;
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
}

Simple, if there's internet connection, I load the videos from online, otherwise, I inform the user like YouTube does.

Where I'm stuck is in the else statement. I'm not sure exactly what to create there to achieve this overlay.

I've looked at some solutions regarding switching fragments here:

  1. Switching between Fragment view
  2. How to switch between fragments during onclick?

However, I'm not sure if, for my purpose, I would need to do this?

Can someone point me in the right direction? I'm not looking to have someone write the whole code exactly like the YouTube app, but I'd like to have a small sample, with just maybe a Button or TextView? This will allow me to follow the structure and modify it to meet my requirements.

Thanks.

Upvotes: 0

Views: 1690

Answers (1)

Nick Felker
Nick Felker

Reputation: 11968

You can look at the Leanback Sample app, which contains an example of a BrowseErrorFragment.

To display:

BrowseErrorFragment errorFragment = new BrowseErrorFragment();
getFragmentManager().beginTransaction().replace(R.id.main_frame, errorFragment)
                        .addToBackStack(null).commit();

Upvotes: 1

Related Questions