Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use fragment in android 2.2?

I have an application which support android 2.2 library.In this app I want to use fragment someone suggest me ,if possible how to do it.

Thanks

like image 842
Narendra Avatar asked Dec 17 '11 06:12

Narendra


People also ask

How does fragment work in Android?

A fragment has its own layout and its own behaviour with its own life cycle callbacks. You can add or remove fragments in an activity while the activity is running. You can combine multiple fragments in a single activity to build a multi-pane UI. A fragment can be used in multiple activities.

Where we can use fragment in Android?

According to the Android documentation, a fragment is a part of applications user interface that is bound to an activity. Fragments have their lifecycle and layouts or UI components. Fragments help enrich your UI design, pass data between different screens, and adapt to different device configurations.

Can fragment to activity in Android?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.


2 Answers

Yes you need to use the support library. What is important to know is that you can use the support library on honeycomb and icecream sandwich as well. In that way you can use the same build on all the platforms. You need to use the SDK 4.0 as target with minsdk 2.2. A different problem are the settings activities. For Honeycomb+ you can use the PreferenceFragment that are not supported by the support library yet. If you use the new onBuildHeaders method of PreferenceActivity and the "header" tag in the settings.xml you need to put that xml in a separated folder like for example xml-v12 to specify that it is used only with API-12+.

For the settings have a look to: http://developer.android.com/reference/android/preference/PreferenceActivity.html there is a good example of the onBuildHeaders

For the support library: http://developer.android.com/sdk/compatibility-library.html

like image 52
kingston Avatar answered Sep 18 '22 14:09

kingston


I think that, in order to include fragment in Android 2.2, you need to add android-support-v4.jar to your project.

like image 24
Richa Avatar answered Sep 20 '22 14:09

Richa