ErrorFragment
– Android TV app hands on Tutorial 6

ErrorFragment

ErrorActivity & ErrorFragment

ErrorFragment is one of another layouts supported by Leanback library.

ErrorActivity

New -> Java Class -> Name: ErrorActivity
* We don’t use layout file for showing ErrorActivity.

Basically, it just attaches ErrorFragment.

package com.corochann.androidtvapptutorial;

import android.app.Activity;
import android.os.Bundle;

/**
 * Created by corochann on 7/7/2015.
 */
public class ErrorActivity extends Activity {

    private static final String TAG = ErrorActivity.class.getSimpleName();

    private ErrorFragment mErrorFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        testError();
    }

    private void testError() {
        mErrorFragment = new ErrorFragment();
        getFragmentManager().beginTransaction().add(R.id.main_browse_fragment, mErrorFragment).commit();
    }
}

ErrorFragment

New -> Java Class -> Name: ErrorFragment
* We don’t use layout file for showing ErrorActivity.

ErrorFragment class is a subclass of android.support.v17.leanback.app.ErrorFragment.

package com.corochann.androidtvapptutorial;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

/**
 * Modified from AOSP sample code by corochann on 7/7/2015.
 * This class demonstrates how to extend ErrorFragment
 */
public class ErrorFragment extends android.support.v17.leanback.app.ErrorFragment {

    private static final String TAG = ErrorFragment.class.getSimpleName();
    private static final boolean TRANSLUCENT = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        
        setTitle(getResources().getString(R.string.app_name));
        setErrorContent();
    }

    void setErrorContent() {
        setImageDrawable(getActivity().getDrawable(R.drawable.lb_ic_sad_cloud));
        setMessage(getResources().getString(R.string.error_fragment_message));
        setDefaultBackground(TRANSLUCENT);

        setButtonText(getResources().getString(R.string.dismiss_error));
        setButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                getFragmentManager().beginTransaction().remove(ErrorFragment.this).commit();
            }
        });
    }
}

Modify AndroidManifest.xml to declare ErrorActivity,

    <application>

    ...

        <activity android:name=".ErrorActivity" />
    </application>


Implement action to launch ErrorActivity

I will change the name of item of GridItemPresenter “Item 1” -> “ErrorFragment”, and I will launch this ErrorActivity by clicking this “ErrorFragment” item.

Modify MainFragment, to launch ErrorActivity by intent.

    private void loadRows() {
        ...
        //gridRowAdapter.add("ITEM 1");
        gridRowAdapter.add("ErrorFragment");
        ...
    }
        @Override
        public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
                                  RowPresenter.ViewHolder rowViewHolder, Row row) {
            // each time the item is clicked, code inside here will be executed.
            if (item instanceof Movie) {
                Movie movie = (Movie) item;
                Log.d(TAG, "Item: " + item.toString());
                Intent intent = new Intent(getActivity(), DetailsActivity.class);
                intent.putExtra(DetailsActivity.MOVIE, movie);

                getActivity().startActivity(intent);
            } else if (item instanceof String){
                if (item == "ErrorFragment") {
                    Intent intent = new Intent(getActivity(), ErrorActivity.class);
                    startActivity(intent);
                }
            }
        }
<resources>
    <string name="app_name">AndroidTVappTutorial</string>
    <string name="title_activity_main">MainActivity</string>

    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_details">DetailsActivity</string>

    <string name="error">Error</string>
    <!-- Error messages -->
    <string name="video_error_media_load_timeout">Media loading timed out</string>
    <string name="video_error_server_inaccessible">Media server was not reachable</string>
    <string name="video_error_unknown_error">Failed to load video</string>
    <string name="error_fragment_message">An error occurred</string>
    <string name="dismiss_error">Dismiss</string>
    <string name="oops">Oops</string>
</resources>

Build and run

By clicking “ErrorFragment” item, ErrorFragment will show Error message with images.

ErrorFragment

 * AOSP project, SpinnerFragment is also implemented to show loading animation. But I introduced a minimum implementation/usage of ErrorFragment here.

Source code is on github.

Leave a Comment

Your email address will not be published. Required fields are marked *