[1523] Add support for default styles to LottieAnimationView #1524
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1523, with an enhancement.
Issue description
The issue was that the
defStyleAttr
would not be respected - the following class would compile, butR.attr.heartViewStyle
would be ignored:This meant that if we had a style:
we'd need to apply it explicitly in the layout each time we used it to a
LottieAnimationView
(orHeartView
, but at this stage, there's no need forHeartView
to exist - it doesn't do anything):Solving the issue
After delegating
defStyleAttr
to theinit
function, and passing it to thecontext.obtainedStyledAttributes
call, we're able to do this (note, there's no explicit style on this view):as long as we set the
heartViewStyle
attribute in our application theme:so every time we use
HeartView
, it'll use the correct style by default, across the app.Enhancing our solution
The addition that isn't covered in the original issue is adding a default style attribute for
LottieAnimationView
in the library,R.attr.lottieAnimationViewStyle
.This allows us to set default Lottie attributes across our app, for example the
autoPlay
andloop
properties:such that in our layouts, we'd only need to specify the url, and we wouldn't need to use a custom view to do it:
We can still override these properties in our layouts, if the default isn't suitable for all cases. And this is what the above looks like:
This solution works alongside any custom views too - so if you want a
HeartView
(or aLoadingIndicatorView
) where you don't have to specify any Lottie attributes - but most of the time, it's ok to use the vanillaLottieAnimationView
because you're not reusing that particular icon, you can (the GIF looks the same with this layout and theme setup):where we've refactored
Widget.LottieSample.Heart
to extend from our defaultWidget.LottieSample.LottieAnimationView
style, so that it also has theautoPlay
andloop
properties: