Rajesh Babu

Fragment : It is the breaking up of an individual activity into two or more pieces, each of which grows into a new individual.

Coding guidelines : If you use a class which is extends by Fragment as a inner class, the class should be static and the way to
define xml is :

                     <fragment android:name="com.ni.fragment.HelloWorldActivity$HelloWorldFragment"
                            android:id="@+id/fragment1" 
                            android:layout_weight="1"
                            android:layout_width="200px" 
                            android:layout_height="100px" />

else ( extends by Fragment is separate class)

                     <fragment android:name="com.ni.fragment.HelloWorldFragment"
                            android:id="@+id/fragment1"
                            android:layout_weight="1"
                            android:layout_width="200px"
                            android:layout_height="100px" />

Java code :

public class HelloWorldActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public static class HelloWorldFragment extends Fragment
    {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            View v = inflater.inflate(R.layout.hello_world,null);
            ListView mList =(ListView)v.findViewById(R.id.listview);
            ListAdapters adapter = new ListAdapters(getActivity().getApplicationContext());
            mList.setAdapter(adapter);
            return v;
        }
    }
}

If you want to add a fragment dynamically, you have to create a instance of a class which is extends by Fragment.
Sample code : xml file

                     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                        android:orientation="vertical" 
                                        android:layout_width="fill_parent"
                                        android:layout_height="300px" 
                                        android:id="@+id/layout">
                 </RelativeLayout>

In the above xml no need to give fragment tag, because we have to add fragment dynamically.

Java code:

public class HelloWorldActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        addDynamicFragment();
    }

    private void addDynamicFragment() {
        // TODO Auto-generated method stub
               // creating instance of the HelloWorldFragment.
        Fragment fg = HelloWorldFragment.newInstance();
        // adding fragment to relative layout by using layout id
        getFragmentManager().beginTransaction().add(R.id.layout, fg).commit();
    }

    public static class HelloWorldFragment extends Fragment {

        public static Fragment newInstance() {
            HelloWorldFragment mFrgment = new HelloWorldFragment();
            return mFrgment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            View v = inflater.inflate(R.layout.hello_world, null);
            ListView mList = (ListView) v.findViewById(R.id.listview);
            ListAdapters adapter = new ListAdapters(getActivity().getApplicationContext());
            mList.setAdapter(adapter);
            return v;
        }
    }

}