I have developer the whole my project with
import android.app.FragmentManager;
import android.app.FragmentTransaction;
and using Tabs with ActionBar. Now i need to swipe between tabs. I have seen many examples even the sample project of Android, but all imports they used are with support.v4.app and hence they can use FragmentPagerAdapter. My min sdk is 14. Is there any way to use FragmentPagerAdapter in my project or any alternative for Viewpager. I have gone through some of related post like :
FragmentPagerAdapter Exists Only In Android.Support.V4.App (and not Android.App)
Problems with FragmentPagerAdapter
When i import android.support.v13.app.FragmentPagerAdapter., it couldn't be resolved.
You could copy the source code and make necessary changes to use the non-support versions of the classes referenced in FragmentPagerAdapter (like Fragment). It should be enough to just change the imports to the non-support versions of those classes.
Import the library android-sdk\extras\android\support\v13\android-support-v13.jar
There is everything you need.
Related
How would I use android.app.Fragment; inside FragmentStatePagerAdapter
It only supports returning: android.support.v4.app.Fragment. An existing fragment I am using inherits from RxFragment which doesn't use the support library fragment.
You can't.
FragmentStatePagerAdapter and FragmentPagerAdapter are part of the support library, and only support the support Fragments.
If you really want to do this, you could copy the source for those classes and replace the support Fragment imports with framework Fragment imports.
You can use android.app.Fragment if you use android.support.v13.app.FragmentStatePagerAdapter from the v13 Support Library. See this answer from #brillenheini
Its very hard to describe my problem but I'll do my best <<
when I want to make a ViewPager in MainActivity , I go to xml then :
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/viewPager"
/>
then I make an Adapter
public class MyAdapter extends FragmentPagerAdapter
here , I need to import
android.support.v13.app.FragmentPagerAdapter;
the QUESTION is ;
is there any problem when I use v4 , v13 on one application
note : I add the v13 library and make it as (provided)
On one hand you can use both v4 and v13 support packages. Sometimes you have to.
For example android.support.v13.app.FragmentPagerAdapter and android.support.v4.app.FragmentPagerAdapter are similar but not identical : they don't use the same kind of fragments (native android.app.Fragment vs support android.support.v4.app.Fragment).
So if you want to use a ViewPager with native fragment you have to extend the v13 FragmentPagerAdapter and set it on the ViewPager (v4, there is only one). Of course this will only work on v13+ devices.
On the other hand you don't have to declare both library : the v13 support lib includes the v4, adding something like
compile "com.android.support:support-v13:23.1.1"
in your build.gradle gives you access to both v4 an v13 support classes.
By the way, see Fragment or Support Fragment for more details about native vs support fragments.
Edit: There shouldn't be a problem using both, just don't set it as provided. Use compile as the classes are not provided by the system.
I couldn't find any guidance on that yet.
I want to make a small change on an Android API class (ViewPager) and import it to my project. I want to customize a parameter of a method that is not public.
I tried to extend this class and override the method, but since the it is protected I cannot access super.method() from my package, so I don't know how to proceed.
I decompiled it in Android Studio, tried to copy and paste the class, but it wouldn't work. I'm clueless right now.
How can I achieve it?
This is what I tried:
You can find the source of ViewPager on the AOSP project: https://android.googlesource.com/platform/frameworks/support/+/android-5.1.1_r2/v4/java/android/support/v4/view/ViewPager.java and copy it into your project. You can then modify it and extend it as you need.
However, you will not get the benefit of updates that come from new AOSP versions of ViewPager if you follow this course. I highly recommend finding another way to accomplish your goal without duplicating ViewPager like this.
The possibilities you have:
Extend the class and override a public or protected method.
Copy and paste the whole class from the Android source code, make the needed changes.
Change the behavior using reflection.
Regarding the velocity of the ViewPager: check the following SO questions:
Slowing speed of Viewpager controller in android
Android: velocity-based ViewPager scrolling
If I do not need to grant support for older versions of android and can just use API 17, is there an alternative way to implement the much advertised horizontal swipe (like in gmail) and not use viewpager, fragmentsactivities (new APIs already have fragments...) etc. to get rid of the support library?
All the tutorials I've found for horizontal swipe show how to do it using the support library and FragmentActivities. Otherwise what is the advantage of the new APIs if I cannot use their native classes and methods?
to get rid of the support library
ViewPager only exists in the Android Support Library. The Android Support Library is not just for backports.
new APIs already have fragments
ViewPager can use native API Level 11 fragments, though you may need to create your own PagerAdapter. Or, you can use ViewPager without any fragments, using just ordinary View or ViewGroup objects for the pages.
is there an alternative way to implement the much advertised horizontal swipe
You are welcome to write your own replacement for ViewPager. Most developers prefer to reuse ViewPager, for reduced development and maintenance costs.
There is also HorizontalScrollView, an open source HorizontalListView floating around, and so on.
You can write your own FragmentPagerAdapter which works with android.app.Fragment instead of android.support.v4.app.Fragment to minimize dependency on the support library. You only need to create your own class for the adapter (based on FragmentPagerAdapter from support library which source can be found in folder <android-sdks>/extras/android/support/v4/src/java/android/support/v4/app/). Only modification is in the import section - you should reference Fragment, FragmentManager and FragmentTransaction from android.app instead of android.support.v4.app.
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.
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
The activity classes that use fragments must extend FragmentActivity NOT Activity.
instead of getFragmentManager() you have to use getSupportFragmentManager
Enjoy!!!
Yes, fragments are supported from Android 1.6. For more information see: Compatibility Library.
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;
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();.
yes,
Android provide provide support library for the backward compatibility.
select the project-> right click->android tools->add support library