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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With