Monday, December 12, 2011

Programmatically add fragment

Last post show how to inflate layout from XML. The Fragment is hard coded in layout XML, "fragment" element in main.xml. Alternatively, it can be specified as ViewGroup, LinearLayout in my example here. Then get an instance of FragmentTransaction and add the fragment in the ViewGroup Programmatically.

Programmatically add fragment

Modify the main layout, main.xml, to specify the fragment as LinearLayout instead of MyFragment.
<?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="horizontal" >

<TextView
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="It's TextView in main Activity"
android:background="#555555"/>
<LinearLayout
android:id="@+id/myfragment"
android:layout_width="0px"
android:layout_weight="4"
android:layout_height="match_parent" />

</LinearLayout>


Modify onCreate() method of the main activity to get an instance of FragmentTransaction and add the fragment in the LinearLayout.
package com.exercise.FragmentTest;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class FragmentTestActivity extends Activity{

Fragment fragment;

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

// get an instance of FragmentTransaction from your Activity
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

//add a fragment
MyFragment myFragment = new MyFragment();
fragmentTransaction.add(R.id.myfragment, myFragment);
fragmentTransaction.commit();

}

}


Both MyFragment.java and /res/layout/fragmentlayout.xml kept no change as last post.

MyFragment.java
package com.exercise.FragmentTest;

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

public class MyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.fragmentlayout, container, false);

return myFragmentView;
}

}


/res/layout/fragmentlayout.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:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="It's Fragment" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher"/>
</LinearLayout>



Download the files.

next:
- Dynamic change fragment using Java code



3 comments:

  1. Thanks man, something in your code help me solve my coding problem

    ReplyDelete
  2. Thanks, something in your code help me with my coding issue

    ReplyDelete
  3. Thanks, something in your code help me with my coding issue

    ReplyDelete