17

So in trying to use the Slide Activity transition but with a different gravity, the app crashes on using Gravity.START, using this:

getWindow().setExitTransition(new Slide(Gravity.START));

and I get this error:

IllegalArgumentException: Invalid slide direction

But yet if you look in the source code, that specific constructor above calls setSlideEdge() in which case that method goes through a switch statement to set the Gravity you specified earlier:

switch (slideEdge) {
        case Gravity.LEFT:
            mSlideCalculator = sCalculateLeft;
            break;
        case Gravity.TOP:
            mSlideCalculator = sCalculateTop;
            break;
        case Gravity.RIGHT:
            mSlideCalculator = sCalculateRight;
            break;
        case Gravity.BOTTOM:
            mSlideCalculator = sCalculateBottom;
            break;
        case Gravity.START:
            mSlideCalculator = sCalculateStart;
            break;
        case Gravity.END:
            mSlideCalculator = sCalculateEnd;
            break;
        default:
            throw new IllegalArgumentException("Invalid slide direction");
    }

Gravity.LEFT works just fine, but because I want RTL support, it only makes sense to instead use Gravity.START. I'm confused as to why the default case is executed in this switch statement, and the only explanation for it is it's a bug.

I'd report it to Google but they don't have public ways of reporting API bugs like this, and in this case the bug isn't exactly obvious to fix. So, PSA to anyone that wants to use the Slide animation with a Gravity of START.

Liuting
  • 1,098
  • 17
  • 35
StackOverflowMaster
  • 3,478
  • 3
  • 18
  • 19
  • 1
    I'm experiencing the same problem. Transitions work fine on Nexus 5 5.1, but cause a crash on OnePlus One 5.0.2. – EyesClear May 17 '15 at 14:16
  • 1
    Slide animation is not compatible in api 21 it is work greater than 21. – Abhinav singh Sep 18 '15 at 08:03
  • @Destro, not that's not correction according to the `Slide` the method `Slide. setSlideEdge()` class was addded in API 21. – fahmy May 04 '16 at 13:55
  • Same issue, for me its crashing on Samsung Note 3. – fahmy May 04 '16 at 13:56
  • 1
    I have opened an issue [here](https://github.com/android/platform_frameworks_base/issues/116) – StuStirling Jul 20 '16 at 11:46
  • I also encountered this error on a Samsung Galaxy Tab S 10.5-Inch (Android 5.0.2, API 21). I didn't get this error on my Samsung Galaxy S7 Edge (Android 6.0.1, API 23). `Gravity.LEFT` works, but `Gravity.START` throws `IllegalArgumentException: Invalid slide direction` – kimbaudi Aug 05 '16 at 23:30
  • 1
    Hi all, although `Slide` was added in API 21, it didn't support `Gravity.START` and `Gravity.END` until API 22, see my answer below. – Lewis McGeary Sep 15 '16 at 13:49

1 Answers1

29

This is a problem that crashes on API 21 devices. The reason is that the initial version of Slide in API 21 only supports LEFT,TOP,RIGHT and BOTTOM gravitys.

Slide source from API 21

Slide source from API 22

One way to solve this is by using GravityCompat from the Support Library.

new Slide(GravityCompat.getAbsoluteGravity(GravityCompat.START, getResources().getConfiguration().getLayoutDirection()));

The getAbsoluteGravity() method takes in the preferred gravity (START or END) and the current layout direction and returns the gravity as LEFT or RIGHT as appropriate for the current configuration.

Lewis McGeary
  • 7,692
  • 2
  • 40
  • 46