In this example the fragment xml file is hard coded into the activity_main.xml file. We could however use the fragment manager and add() method in the java code to give us extra control at runtime.
MainActivity.java
package com.jamesfroggatt.listactivitydemo.app; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
MyListFragment.java
package com.jamesfroggatt.listactivitydemo.app; 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.ArrayAdapter; import android.widget.Toast; public class MyListFragment extends ListFragment implements AdapterView.OnItemClickListener { // link the layout xml file to our list fragment @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.my_list_fragment, container, false); } // create the adapter, set the adapter @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // brings data from the XML into the java code // and gives an appearance to each piece of the list ArrayAdapter adapter=ArrayAdapter.createFromResource(getActivity(),R.array.surnames,android.R.layout.simple_list_item_1); setListAdapter(adapter); // set the onItemclick Listener getListView().setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getActivity(),"Item "+position,Toast.LENGTH_SHORT).show(); } }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF4400"> <!-- the list fragment can be added as below (in xml) or we can --> <!-- use the fragment manager in java code.. i.e. make a transaction and use transaction.add() in MainACtivity--> <fragment android:layout_width="wrap_content" android:layout_height="wrap_content" android:name="com.jamesfroggatt.listactivitydemo.app.MyListFragment" android:id="@+id/fragment" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout>
my_list_fragment.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@android:id/list" android:layout_gravity="left|center_vertical" /> <!-- show text when there nothing in the listView --> <!-- "@android:id/empty" --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New text" android:id="@android:id/empty" android:layout_gravity="left|center_vertical"/> </LinearLayout>
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">ListActivityDemo</string> <string name="hello_world">Hello world!</string> <string-array name="surnames"> <item>Froggatt</item> <item>Lawrence</item> <item>Richardson</item> <item>Barrett</item> </string-array> <string name="title_activity_another">MainActivity</string> </resources>
Full tutorial here: