A Simple Fragment Application in Android

June 30, 2012
By

In this article we will create an Android application which will display the “HelloFragment” using a fragment.


1. Create a new Android project namely “HelloFragmentNew”

New Android Project

Figure 1: New Android Project


2. Select build target

Select build target

Figure 2: Select build target


3. Enter application details

Enter Application Details

Figure 3: Enter Application Details


4. res/values/strings.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">HelloFragment</string>

</resources>


5. res/layout/hello_fragment_layout.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:layout_gravity="center_horizontal"
        android:text="@string/app_name"
    />

</LinearLayout>


6. src/in/wptrafficanalyzer/hellofragmentnew/HelloFragment.java


package in.wptrafficanalyzer.hellofragmentnew;

import android.app.Fragment;
import android.os.Bundle;
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:id="@+id/hellofragment"
        android:name="in.wptrafficanalyzer.hellofragmentnew.HelloFragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

</LinearLayout>


8. Application in execution

HelloFragment

Figure 4: HelloFragment


9. Download


10. 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: ,

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