In this article , we will create an Android application which displays a text “HelloFragment” in the screen where the text is displayed by a fragment. Actually fragments are new concepts which are supported from Android 3.0 onwards. But by using the backward compatibility library we can use fragments from Android 1.6 onwards.
1. Create an Android Project called “HelloFragment”
2. Select an Android build target
3. Enter application details
4. Adding fragment support
Since fragments are implemented from Android 3.0 onwards, we need to add fragment support library to this project. We can provide fragment support to our Android 2.3 application by right clicking HelloWorld ( Project Explorer Window ) -> Android Tools -> Add Support Libraries …
5. res/layout/hello_fragment.xml
<?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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:layout_gravity="center_horizontal" /> </LinearLayout>
6. src/in/wptrafficanalyzer/hellofragment/HelloFragment.java
package in.wptrafficanalyzer.hellofragment; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class HelloFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.hello_fragment_layout, null); return v; } }
7. res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <fragment android:name="in.wptrafficanalyzer.hellofragment.HelloFragment" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
8. src/in/wptrafficanalyzer/hellofragment/MainActivity.java
package in.wptrafficanalyzer.hellofragment; import android.os.Bundle; import android.support.v4.app.FragmentActivity; public class MainActivity extends FragmentActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
9. Application in execution
10. Download

11. Reference
http://developer.android.com/guide/index.html

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in
You can hire me on hourly basis or on project basis for Android applications development.
For hiring me, please mail your requirements to info@wptrafficanalyzer.in.
My other blogs
store4js.blogspot.com
Oh thanks a ton… I’m really new at Fragment concept, especially with lower API’s …. this helped me a lot. Will try and share the code that i built using this once it is done.