Learn ListFragment in Android Using Android Studio

Introduction

This article explains ListFragment in Android.

Before beginning the coding, first we learn the basics of ListFragment. To create a ListView in your class you need to extend the ListFragment class. Create an array of String that you want to show in a ListView and ArrayAdapter object. Inside the onCreateView() method you will create the ArrayAdapter object and the array to the constructor of ArrayAdapter. Now after creating the object you will this to the setListAdapter() method to set the list in a ListView.

In activity_main XML file you will use a fragment that you want to show on the MainActivity screen. In the fragment name property you will the second class as an attribute.

Step 1

Create a project like this:

Clipboard05.jpg

Step 2

Create an activity_main XML file and write the following.

In this XML file you will use the fragment inside Relative Layout.

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity">  
  10.    
  11.    
  12.     <fragment  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:name="com.listfragment.Second"  
  16.             android:id="@+id/fragment"  
  17.             android:layout_alignParentTop="true"  
  18.             android:layout_alignParentLeft="true"/>  
  19. </RelativeLayout>  

 

Step 3     

Create another XML file and write the following:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.               android:orientation="vertical"  
  4.               android:layout_width="match_parent"  
  5.               android:layout_height="match_parent"  
  6.               android:background="#123456">  
  7. </LinearLayout>  

 

Step 4

Create a Java file and write the following:

  1. package com.listfragment;  
  2.    
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6.    
  7. public class MainActivity extends Activity {  
  8.    
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.     }  
  14.    
  15.    
  16.     @Override  
  17.     public boolean onCreateOptionsMenu(Menu menu) {  
  18.         // Inflate the menu; this adds items to the action bar if it is present.  
  19.         getMenuInflater().inflate(R.menu.main, menu);  
  20.         return true;  
  21.     }  
  22.    
  23. }  

 


Step 5

Create another Java class and write the following.

For creating a ListView in your class you need to extend the ListFragment class. Create an array of String that you want to show in a ListView and ArrayAdapter object. Inside the onCreateView() method you will create the ArrayAdapter object and the array to the constructor of ArrayAdapter. Now after creating the object you will this to the setListAdapter() method to set the list in a ListView.

After that you will get the ListView by calling the getListView() method. Now set the ListView on its item click listener to get the item when you perform the click event.

  1. package com.listfragment;  
  2.    
  3. import android.*;  
  4. import android.R;  
  5. import android.annotation.TargetApi;  
  6. import android.app.ListFragment;  
  7. import android.graphics.AvoidXfermode;  
  8. import android.os.Build;  
  9. import android.os.Bundle;  
  10. import android.view.LayoutInflater;  
  11. import android.view.View;  
  12. import android.view.ViewGroup;  
  13. import android.widget.AdapterView;  
  14. import android.widget.ArrayAdapter;  
  15. import android.widget.ListView;  
  16. import android.widget.TextView;  
  17. import android.widget.Toast;  
  18.    
  19. /** 
  20.  * Created by vivek on 8/1/13. 
  21.  */  
  22. @TargetApi(Build.VERSION_CODES.HONEYCOMB)  
  23. public class Second extends ListFragment {  
  24.     ListView listview;  
  25.     View view;  
  26.     String[] array=new String[]{"Android","BlackBerry","Windows","Iphone"};  
  27.    
  28.     public View onCreateView(LayoutInflater i,ViewGroup container,Bundle savedInstanceState)  
  29.     {  
  30.    
  31.         ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(), R.layout.simple_list_item_1,R.id.text1,array);  
  32.    
  33.       setListAdapter(adapter);  
  34.         return super.onCreateView(i,container,savedInstanceState);  
  35.     }  
  36.        public void onActivityCreated(Bundle savedInstanceState){  
  37.         super.onActivityCreated(savedInstanceState);  
  38.    
  39.         listview=getListView();  
  40.         listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  41.             @Override  
  42.             public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {  
  43.    
  44.                 Toast.makeText(getActivity(), (CharSequence) listview.getItemAtPosition(i),Toast.LENGTH_LONG).show();  
  45.             }  
  46.         });  
  47.     }  
  48.     }  

 

Step 6

Clipboard02.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all