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
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
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..:)
This comment has been removed by the author.
ReplyDeleteHi,
ReplyDeleteThis is simple and nice example.Keep writing
Good work:)
ReplyDeletehey, 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?
ReplyDeleteThanks all for your comments
ReplyDelete@Kristianlm here I meant by root folder means the root folder of that project like assets folder.
Thanks!
ReplyDelete// Create the list fragment and add it as our sole content.
ReplyDeleteif (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
MyFragment myFrag = new MyFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, myFrag).commit();
}
Thank you for all!
ReplyDeleteThank you, Labeeb, I needed to learn Fragments *fast*, and your writing and examples were clean, easy and very, very helpful! Perfect.
ReplyDeletegetting error when i run in emulator version androi2.33(sdk value=10)
ReplyDeleteGood job, it helps me.Tks
ReplyDeletehelped me out thanks a lot. :)
ReplyDeleteThanks a lot. I thought fragments and support libraries are a bit complicated. But that was a concise tutorial. great.
ReplyDeleteFragment productDetails = new MyFragment();..this throws compiler error for me...
ReplyDelete"change productDetails to MyFragment..."
if i do so fragmentTransaction.replace/add gives error..
what might be the reason folks??
Make sure you Fragment and the Fragment you extended for MyFragment is same, android.support.v4.app.Fragment
Deletethanks....
ReplyDeletebut 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
Make sure you Fragment and the Fragment you extended for MyFragment is same, android.support.v4.app.Fragment
Deletei am just confused about list fragment that when to extend android.support.v4.app.ListFragment and when android.app.ListFragment
ReplyDeleteif you want run your app in lower version os (below api level 11) you need to use support.v4 jar else normal one
Deletenice... wish too see more from you
ReplyDeletegood tutorial...
ReplyDeletethanks labeeb
hello
ReplyDeletemy 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