Saturday, April 16, 2011

Using Fragment of Android Compatibility Package

The Android 3.0 introduce a awesome feature Fragment.
It really helpful when we design for tablet. You can refer The Android 3.0 Fragment API   for more details
Here I'm going to explain how to use Fragment for lower version.
Now android had released a static library that exposes the same Fragments API, so that applications compatible with Android 1.6 or later can use fragments to create tablet-compatible user interfaces.

For this you need to do follow these steps
  • Add android-support-v4.jar to you project
    • You can found this jar at yoursdkpath/extras/android/compatibility/v4/
    • Copy this into your libs folder in the root of your project 
    • add this jar to your project build path
  • In order to use this you need to use FragmentActivity instead of Activity 
    • public class Home extends FragmentActivity
  • To get FragmentManager 
    • Need to use getSupportFragmentManager instead of getFragmentManage
The complete code will look like this

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
public class Home extends FragmentActivity{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
  Fragment productDetails = new MyFragment();
  fragmentTransaction.replace(R.id.mainFragement, productDetails);
  fragmentTransaction.commit();
    }
}
MyFragment.java

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

public class MyFragment extends Fragment{
 public MyFragment(){
  Log.i("MYEXPENSETRACKER","MyFragment ()");
 }
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  TextView t = new TextView(getActivity());
  t.setText("From Fragement");
  return t;
 }
}

The main.xml I used

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    
 <FrameLayout
  android:id="@+id/mainFragement"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
 </FrameLayout>
</LinearLayout>

Hope it helped you..:)

23 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi,
    This is simple and nice example.Keep writing

    ReplyDelete
  3. hey, thanks for the tip! Aren't libs supposed to go to ./libs instead of the root folder? That way, they automatically get included wiht ant builds. Or is this a special case?

    ReplyDelete
  4. Thanks all for your comments
    @Kristianlm here I meant by root folder means the root folder of that project like assets folder.

    ReplyDelete
  5. // Create the list fragment and add it as our sole content.
    if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
    MyFragment myFrag = new MyFragment();
    getSupportFragmentManager().beginTransaction().add(android.R.id.content, myFrag).commit();
    }

    ReplyDelete
  6. Thank you for all!

    ReplyDelete
  7. Thank you, Labeeb, I needed to learn Fragments *fast*, and your writing and examples were clean, easy and very, very helpful! Perfect.

    ReplyDelete
  8. getting error when i run in emulator version androi2.33(sdk value=10)

    ReplyDelete
  9. helped me out thanks a lot. :)

    ReplyDelete
  10. Thanks a lot. I thought fragments and support libraries are a bit complicated. But that was a concise tutorial. great.

    ReplyDelete
  11. Fragment productDetails = new MyFragment();..this throws compiler error for me...
    "change productDetails to MyFragment..."
    if i do so fragmentTransaction.replace/add gives error..

    what might be the reason folks??

    ReplyDelete
    Replies
    1. Make sure you Fragment and the Fragment you extended for MyFragment is same, android.support.v4.app.Fragment

      Delete
  12. thanks....

    but i have a problem
    // Fragment productDetails = new MyFragment();
    in my case i just add Fragment addnew = new Afragment();
    but i got the error " Type mismatch cannot convert from Afragment to Fragment"... i have no idea why this is happening.. plz help me.. thanks in advance

    ReplyDelete
    Replies
    1. Make sure you Fragment and the Fragment you extended for MyFragment is same, android.support.v4.app.Fragment

      Delete
  13. i am just confused about list fragment that when to extend android.support.v4.app.ListFragment and when android.app.ListFragment

    ReplyDelete
    Replies
    1. if you want run your app in lower version os (below api level 11) you need to use support.v4 jar else normal one

      Delete
  14. nice... wish too see more from you

    ReplyDelete
  15. my minSdk and targetSdk version is 17 and i have added andrdoid-support-v4.jar as well . but i get the error android.app.fragment cannot be resolved . it remains even if the minskd version is 8 or 17 . please help

    ReplyDelete