1

I am getting this error:

Call requires API level 14 (current min is 8): android.view.ViewGroup#canScrollHorizontally.

How I can resolve this in API level before 14?

public class ViewPagerEx extends ViewGroup{
    @Override
    public boolean performAccessibilityAction(View host, int action, Bundle args) {
        if (super.performAccessibilityAction(host, action, args)) {
            return true;
        }
        switch (action) {
            case AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD: {
                if (canScrollHorizontally(1)) {
                    setCurrentItem(mCurItem + 1);
                    return true;
                }
            } return false;
            case AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD: {
                if (canScrollHorizontally(-1)) {
                    setCurrentItem(mCurItem - 1);
                    return true;
                }
            } return false;
        }
        return false;
    }

    public boolean canScrollHorizontally(int direction) {
        if (mAdapter == null) {
            return false;
        }
        final int width = getClientWidth();
        final int scrollX = getScrollX();
        if (direction < 0) {
            return (scrollX > (int) (width * mFirstOffset));
        } else if (direction > 0) {
            return (scrollX < (int) (width * mLastOffset));
        } else {
            return false;
        }
    }
}
Chilledrat
  • 2,593
  • 3
  • 28
  • 38
ishq
  • 833
  • 1
  • 6
  • 11
  • 4
    Use the support library: http://developer.android.com/reference/android/support/v4/view/ViewCompat.html#canScrollHorizontally(android.view.View, int) – Ken Wolf Oct 03 '14 at 08:43
  • it depends on you if you want to support more recent android phones you can increase your api level to 14...else you have to find another alternative to what you want to do,This is just a suggestion – danidee Oct 03 '14 at 08:44
  • 1
    it is not clear if you wrote a method called canScrollHorizotnally or you want to use the one provided by the framework – Blackbelt Oct 03 '14 at 08:45

3 Answers3

1

go to AndroidManifest.xml you will find such code:

`<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />`

android:minSdkVersion is what your looking for, i means that your app will work on devises having the same API level.

android:targetSdkVersion is the API level that your app is designed to work on.

android:maxSdkVersion is the maximum API level your app can work on.

implies your app can work on devices between android:minSdkVersion and android:maxSdkVersion

so what you have to do is is changing you API level from 8 to 14 to support android.view.ViewGroup#canScrollHorizontally. since it was added in level 14 .

take into consideration that lowering android:minSdkVersion will support more android devices but you wouldn't be able to use methods introduced in higher API levels .

for more info check this detailed description

extra info :

  1. kitkat 24.5% (API level 19)
  2. JELLYBEAN 53.8% (API level 16,17,18)
  3. ICECREAMSANDWICH 9.6% (API level 15 )
  4. GINGERBREAD 11.4% (API level 10 )
  5. FROYO 0.7% (API level 8 )

0.7% ??? you might think twice before setting android:minSdkVersion="8"

Hassan Nahhal
  • 31
  • 1
  • 8
  • 1
    I think you are confusing targetSdkVersion with maxSdkVersion attribute. You might want to check that link again :) – Chilledrat Oct 03 '14 at 16:43
  • This answer is misleading. First, as noted, targetSdkVersion is _not_ the maximum API version that you app can run on. Second, lowering minSDK does _not_ make you lose functionality. You can _still code_ using them. What you will need to do, however, is take care of not calling said APIs on devices that do not have them. But it is wrong to say that _"you will loose advanced functionality added in higher API levels"_. That is wrong. – davidcesarino Oct 03 '14 at 17:06
  • @Chilledrat exactly , how didn't i notice that , i will edit my answer . thanks :) – Hassan Nahhal Oct 03 '14 at 20:36
  • @david-cesarino first it was a misconception , i will edit my answer. second as you said :"take care of not calling said APIs on devices that do not have them" , i think its better to use the ` targetSdkVersion` rather than using a library or casting or any other means that can be used to avoid such error , since he cant control what the user may do . as for "you will loose advanced functionality added in higher API levels" i will correct it . am not that fluent at English so no worries , peole already understood what i mean :) – Hassan Nahhal Oct 03 '14 at 20:40
  • @HasanNahhal OK. I'm satisfied. – davidcesarino Oct 03 '14 at 21:18
1

From @KenWolf's suggestion: Using the support-v4 library, you can use ViewCompat to check this:

// From within the View itself, just invoke the ViewCompat
// implementation with 'this' as the View parameter.
if (ViewCompat.canScrollHorizontally(this, 1)) {
    // ...
}
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
0

Give this a try: Try changing your local method name to something else, and call it. For example change it to canScrollHorizontallyLocal() and so where you are calling it. ViewGroup has a method named like that, and it might be trying the super method instead of yours.

soynerdito
  • 501
  • 3
  • 10