A Simple Fragment Application with backward compatibility in Android

June 30, 2012
By

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”

New Android Project

Figure 1: New Android Project


2. Select an Android build target

Select Android Build Target

Figure 2: Select Android Build Target


3. Enter application details

Enter Application Details

Figure 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

Hello Fragment

Figure 4: Hello Fragment


10. Download


11. Reference

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


How to hire me?

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


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , ,

One Response to A Simple Fragment Application with backward compatibility in Android

  1. Vivekanand on August 7, 2012 at 4:19 pm

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner