Android - list Fragment

Static library support version of the framework's ListFragment. Used to write apps that run on platforms prior to Android 3.0. When running on Android 3.0 or above, this implementation is still used.
The basic implementation of list fragment is for creating list of items in fragments
This example will explain you how to create your own list fragment based on arrayAdapter. So let's follow the following steps to similar to what we followed while creating Hello World Example −
Before start coding i will initialize of the string constants inside string.xml file under res/values directory
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">ListFragmentDemo</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Hello world!</string>
   <string name="imgdesc">imgdesc</string>
   
   <string-array name="Planets">
      <item>Sun</item>
      <item>Mercury</item>
      <item>Venus</item>
      <item>Earth</item>
      <item>Mars</item>
      <item>Jupiter</item>
      <item>Saturn</item>
      <item>Uranus</item>
      <item>Neptune</item>
   </string-array>

</resources>
Following will be the content of res/layout/activity_main.xml file. it contained linear layout and fragment tag.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >
   
   <fragment
      android:id="@+id/fragment1"
      android:name="com.example.tutorialspoint7.myapplication.MyListFragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

</LinearLayout>
Following will be the content of res/layout/list_fragment.xml file. it contained linear layout,list view and text view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >

   <ListView
      android:id="@android:id/list"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
   </ListView>

   <TextView
      android:id="@android:id/empty"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
   </TextView>
</LinearLayout>
following will be the content of src/main/java/myListFragment.java file.before writing to code, need to follow few steps as shown below
Create a class MyListFragment and extend it to ListFragment.
Create a class MyListFragment and extend it to ListFragment.
Inside the onCreateView() method , inflate the view with above defined list_fragment xml layout.
Inside the onCreateView() method , inflate the view with above defined list_fragment xml layout.
Inside the onActivityCreated() method  , create a arrayadapter from resource ie using String array R.array.planet which you can find inside the string.xml and set this adapter to listview and also set the onItem click Listener.
Inside the onActivityCreated() method  , create a arrayadapter from resource ie using String array R.array.planet which you can find inside the string.xml and set this adapter to listview and also set the onItem click Listener.
Inside the OnItemClickListener() method , display a toast message with Item name which is being clicked.
Inside the OnItemClickListener() method , display a toast message with Item name which is being clicked.
package com.example.tutorialspoint7.myapplication;

import android.annotation.SuppressLint;
import android.app.ListFragment;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class MyListFragment extends ListFragment implements OnItemClickListener {
   @Override
   public View onCreateView(LayoutInflater inflater, 
      ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.list_fragment, container, false);
      return view;
   }

   @Override
   public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), 
         R.array.Planets, android.R.layout.simple_list_item_1);
      setListAdapter(adapter);
      getListView().setOnItemClickListener(this);
   }

   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
      Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
   }
}
 Following code will be the content of MainActivity.java
package com.example.tutorialspoint7.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
}
following code will be the content of manifest.xml, which has placed at res/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.tutorialspoint7.myapplication">

   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>
Let's try to run our SimpleListFragment application we just created. I assume you had created your AVD while doing environment set-up. To run the app from Android Studio, open one of your project's activity files and click Run  icon from the toolbar. Android installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window −

    46 Lectures 
     7.5 hours 

 Aditya Dua

    32 Lectures 
     3.5 hours 

 Sharad Kumar

    9 Lectures 
     1 hours 

 Abhilash Nelson

    14 Lectures 
     1.5 hours 

 Abhilash Nelson

    15 Lectures 
     1.5 hours 

 Abhilash Nelson

    10 Lectures 
     1 hours 

 Abhilash Nelson
 Print
 Add Notes
 Bookmark this page