Android Open Source - coursera-android-001 Friends Fragment

From Project

Back to project page coursera-android-001.


The source code is released under:

MIT License

If you think the Android project coursera-android-001 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package course.labs.fragmentslab;
/*  w w  w.j  a va  2  s  .  co  m*/
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FriendsFragment extends ListFragment {

  private static final String[] FRIENDS = { "ladygaga", "msrebeccablack",
      "taylorswift13" };
  private static final String TAG = "Lab-Fragments";

  public interface SelectionListener {
    public void onItemSelected(int position);

  private SelectionListener mCallback;

  public void onCreate(Bundle savedInstanceState) {

    // use different layout definition, depending on whether device is pre-
    // or post-honeycomb

    int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1
        : android.R.layout.simple_list_item_1;

    // Set the list adapter for this ListFragment
    setListAdapter(new ArrayAdapter<String>(getActivity(), layout, FRIENDS));

  public void onAttach(Activity activity) {

    // Make sure that the hosting Activity has implemented
    // the SelectionListener callback interface. We need this
    // because when an item in this ListFragment is selected, 
    // the hosting Activity's onItemSelected() method will be called.
    try {

      mCallback = (SelectionListener) activity;

    } catch (ClassCastException e) {
      throw new ClassCastException(activity.toString()
          + " must implement SelectionListener");

  // Note: ListFragments come with a default onCreateView() method.
  // For other Fragments you'll normally implement this method.
  //   @Override
  //  public View onCreateView(LayoutInflater inflater, ViewGroup container,
  //    Bundle savedInstanceState)

  public void onActivityCreated(Bundle savedInstanceState) {

    Log.i(TAG, "Entered onActivityCreated()");

    // When using two-pane layout, configure the ListView to highlight the
    // selected list item
    if (isInTwoPaneMode()) {




  public void onListItemClick(ListView l, View view, int position, long id) {

    // Notify the hosting Activity that a selection has been made.



  // If there is a FeedFragment, then the layout is two-pane 
  private boolean isInTwoPaneMode() {

    return getFragmentManager().findFragmentById( != null;



Java Source Code List