Android: HelloFragment

Created one of the most basic examples of using Fragments. Seems like most people are using Fragments these days so spent some time trying it out. If you’re trying to see a real basic example here is one.

https://github.com/fangstar/HelloFragment

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"
    android:weightSum="2" >

    <fragment
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        class="com.mfcoding.android.hellofragment.HelloFragment" >

        <!-- Preview: layout=@layout/hello_frag -->
    </fragment>

    <fragment
        android:id="@+id/fragment2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        class="com.mfcoding.android.hellofragment.HelloFragment2" >

        <!-- Preview: layout=@layout/hello_frag -->
    </fragment>

</LinearLayout>

res/layout/hello_frag.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" >

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

src/[PACKAGE]/HelloFragmentActivity.java

package com.mfcoding.android.hellofragment;

import android.app.Activity;
import android.os.Bundle;

public class HelloFragmentActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

src/[PACKAGE]/HelloFragment.java

package com.mfcoding.android.hellofragment;

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_frag, container, false);
		//return super.onCreateView(inflater, container, savedInstanceState);
		return v;
	}

}

src/[PACKAGE]/HelloFragment2.java

package com.mfcoding.android.hellofragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class HelloFragment2 extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.hello_frag, container, false);
		((TextView) view.findViewById(R.id.text)).setText("HelloFragment2");

		return view;
	}

}
Advertisements
This entry was posted in Android and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s