Simple Android Fragments Example 2

Here we use an XML file to set the layout of this simple fragment, unlike the previous example that used java code.

This example is from http://www.i-programmer.info/programming/android/6906-android-adventures-fragments-and-xml.html but the code in this site doesn’t work. I have got it working below. Note I include the import statements and use getSupportFragmentManager() as opposed to getFragmentManager() that is used in the website.

Also the MainActivity extends FragmentActivity, NOT Activity as is given in the example in the website.

MainActivity.java

package com.jamesfroggatt.androidfragments2.app;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;


public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //we are using a "fluent" call sequence for the FragmentManager. This is
        // an common idiom in modern Java and it relies on the fact that
        // getFragmentManager returns a FragmentManager object which you
        // can use immediately by calling its beginTransaction method which
        // returns a Transaction object which you can use immediately and so on.
        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.container, new MyFragment())
                    .commit();
        }
    }
}

MyFragment.java

package com.jamesfroggatt.androidfragments2.app;

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

public class MyFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(
                R.layout.my_fragment, container, false);
        Button b =
                (Button) rootView.findViewById(R.id.button);
        View.OnClickListener onclick =
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Button bt = (Button) view;
                        TextView tv = (TextView) getActivity().
                                findViewById(R.id.textView);
                        tv.setText(bt.getText()
                        );
                    }
                };
        b.setOnClickListener(onclick);
        return rootView;
    }
}

ActivityMain.xml


<LinearLayout
    android:id="@+id/container"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"></LinearLayout>

my_fragment.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jamesfroggatt.androidfragments2.app" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.jamesfroggatt.androidfragments2.app.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Leave a Reply