How to use ViewPropertyAnimatorCompat? - android

I'm trying to make an animation in my app available in older android version and I'm using ViewPropertyAnimator by calling view.animate().
But how can I get a ViewPropertyAnimatorCompat of the view?

Use ViewCompat.
ViewPropertyAnimatorCompat animator = ViewCompat.animate(view);

Related

How to use androidx.animation.AnimatorSet?

I need to use reverse() method of Android AnimatorSet class. The problem is that this method was added only in api 26. But I found reverse method in source code in package androidx.animation. And I couldn't find any androidx library that allows to use that AnimatorSet (androidx.animation.AnimatorSet). How can I use that class?
For Api >=26 you just create an AnimatorSet object and after you define the animation you call reverse on it:
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(ObjectAnimator.ofFloat(...), ...);
animatorSet.setDuration(...);
animatorSet.reverse();
animatorSet.start();
For older APIs you just reverse the order within the .ofFloat methods in your ObjectAnimators:
If initially you have ObjectAnimator.ofFloat(0, 1) you change it to animator.setFloatValues(1, 0)
If you don't care it's still in alpha, you can import it from here:
// To use the Animator APIs
implementation "androidx.core:core-animation:1.0.0-alpha01"
// To test the Animator APIs
androidTestImplementation "androidx.core:core-animation-testing:1.0.0-alpha01"
```

How to use setImageTintList() on Android API < 21

imgView.setImageTintList(getResources()
.getColorStateList(R.color.my_clr_selector));
It says 'Call requires API level 21'.
How can I make it work on Android devices below API 21?
I can make it work by using ImageView#setColorFilter() but I prefer to use a ColorStateList to set tint.
You should use ImageViewCompat#setImageTintList() to achieve this. On API 21+, it will use ImageView#setImageTintList() as you would expect... and on older platform versions, it will delegate to AppCompatImageView which provides a backported implementation.
ColorStateList csl = AppCompatResources.getColorStateList(context, R.color.my_color_state_list);
ImageViewCompat.setImageTintList(imageView, csl);
This is now available in Support Library 25.4.0. See Link
ImageViewCompat.setImageTintList(imageView, colorStateList)
int colorInt = ContextCompat.getColor(context, R.color.primaryColor);
ImageViewCompat.setImageTintList(ivImage, ColorStateList.valueOf(colorInt));

Can i use objectAnimator in xml in api level 2.2?

Can i use objectAnimator in xml in api level 2.2 for fragment transition to to get more types of animation?? or provide me xml for animations like glide, cube, stack etc provided with API 11 and above wich supports API 8 too.
private void transitionFade() {
mFragmentTransaction.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out,android.R.anim.fade_in,android.R.anim.fade_out);
}
The above method with "anim" works fine in API8
but below code doesn't work with "animator"
private void transitionGlide() {
mFragmentTransaction.setCustomAnimations(R.animator.glide_fragment_horizontal_in, R.animator.glide_fragment_horizontal_out, R.animator.glide_fragment_horizontal_in, R.animator.glide_fragment_horizontal_out);
}
No, you cannot directly, as ObjectAnimator appeared in API11, however you can try to use backport of Honeycomb Animation system NineOldAndroids

image.setAlpha on Android lollipop not work

After testing my app on Android 5.0, I noticed that image.setAlpha() is not working on this Android version.
I tried with image.setImagealpha() function, but it returns this error:
"The method setImageAlpha(int) is undefined for the type Drawable"
The API level that I´m using on my app is 8
What can I do?
Update 2019:
With kotlin now we can do it like this:
imageView.post {imageView.alpha = 1.0f}
I have used View.post so that it get updated on immediate next UI updation cycle.
Without posting on UI thread sometimes setting alpha of TextViews doesn't reflect on UI as discussed here
ImageView has the method setAlpha(float) after API 11. Before API 11 it uses setAlpha(int). Since you want to support API 8 and above, you have to specify the different states. So to resolve this, use the following code:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB){
//For API 11 and above use a float such as 0.54.(54% transparency)
imageView.setAlpha(float);
}
else
//For API Below 11 use an int such as 54.
imageView.setAlpha(int);

TextView.getAlpha doesnt exist in Android

I want to get current alpha of textview i am using the following code but i am getting an error that
java.lang.NoSuchMethodError: android.widget.TextView.getAlpha
Please guide me.
This method since API Level 11. Check your API version.
View.getAlpha() only exists since API level 11. You are trying to running your code on a too-old version of Android.
If you absolutely require this functionality, then update your app's minSdkVersion in AndroidManifest.xml to prevent it running on older Android versions. If you can live without it, do a runtime check to see if the API level is high enough.
you can use Alpha method like below
Textview tv_password;
tv_password =(TextView) findviewById(R.id.tv1);
tv_password.getBackground().setAlpha(50);
you can't use getAlpha() method with Textview
View.getAlpha() available from API level 11.But if you want to use this on older api then you may use NineOldAndroids library available for using the Honeycomb (Android 3.0) animation API on all versions of the platform back to 1.0!
So use need to change for e.g.
mAlpha = mView.getAlpha();
to
mAlpha = ViewHelper.getAlpha(mView);
where mView is your view.
Note : Don't forget to import com.nineoldandroids.view.ViewHelper;

Categories

Resources