Fragments in Android 2.2.1, 2.3, 2.0. Is this possible?

AndroidAndroid Fragments

Android Problem Overview


Basically I would like to know if we can have fragment layouts in devices with Android OS < 3.0.

My app had a header on top with 5 different buttons and on start always the first button is clicked by default so the view below these buttons is for the first view. Now when you click on the second button beside it, I don't want the header images to be refreshed but the view just below it needs to be refreshed. So its like updating the fragments below the header image buttons.

So can we have fragments in Android in devices with OS < 3.0.

Sana.

Android Solutions


Solution 1 - Android

You have to use the compatibility libraries provided by Google. Here's how you use Fragments on devices < 3.0

  • Open Eclipse
  • Window->Android SDK and AVD
  • Available Packages->Android Support package (install this)

Once installed, right click the Android project you want to add Fragment support for.

  • Build Path->Configure Build Path
  • Libraries tab
  • Add External JARs
  • Add the android-support-v4.jar (should be in the android downloads folder under extras/android/support/v4

Now you application supports Fragments. There are some key differences to using the compatibility package over using SDK 3.0+. For instance

  1. The activity classes that use fragments must extend FragmentActivity NOT Activity.
  2. instead of getFragmentManager() you have to use getSupportFragmentManager

Enjoy!!!

Solution 2 - Android

Yes, fragments are supported from Android 1.6. For more information see: Compatibility Library.

Solution 3 - Android

In Eclipse Indigo, you can right click on the project --> Android Tools --> Add Support Library. Then, instead of using import android.app.Fragment for OS>3.0, use import android.support.v4.app.Fragment;

Solution 4 - Android

For Android Studio you need to right click on the app name>Open module settings>Dependencies tab>click on '+' to add the dependency.

You need to add this in all your fragments:

import android.support.v4.app.Fragment;

import this to your MainActivity:

Use this import android.support.v4.app.FragmentManager; instead of import android.app.FragmentManager;

And instead of getFragmentManager() you have to use getSupportFragmentManager();.

Solution 5 - Android

yes, Android provide provide support library for the backward compatibility. select the project-> right click->android tools->add support library

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSanaView Question on Stackoverflow
Solution 1 - AndroidSpidyView Answer on Stackoverflow
Solution 2 - AndroidinazarukView Answer on Stackoverflow
Solution 3 - Androidktran9.lbsView Answer on Stackoverflow
Solution 4 - Androiduser3877429View Answer on Stackoverflow
Solution 5 - Androiduser1707035View Answer on Stackoverflow