problem in this code ?
its work on android 4 successfully ! but don't work on 2.2 !
i use nineoldandroids library for android API 8
if(Build.VERSION.SDK_INT > 13) {
v.setTranslationX(0.0F);
v.setTranslationY(height);
v.setRotationX(45.0F);
v.setScaleX(0.7F);
v.setScaleY(0.55F);
ViewPropertyAnimator localViewPropertyAnimator =
v.animate().rotationX(0.0F).rotationY(0.0F).translationX(0).translationY(0).setDuration(animDuration).scaleX(
1.0F).scaleY(1.0F).setInterpolator(interpolator);
localViewPropertyAnimator.setStartDelay(0).start();
} else {
com.nineoldandroids.view.ViewPropertyAnimator.animate(v).translationX(0.0F).translationY(height)
.rotationX(45.0F).scaleX(0.7F).scaleY(0.55F);
com.nineoldandroids.view.ViewPropertyAnimator.animate(v).setStartDelay(0).start();
com.nineoldandroids.view.ViewPropertyAnimator.animate(v).rotationX(0.0F).rotationY(0.0F).translationX(0).translationY(0).setDuration(animDuration).scaleX(
1.0F).scaleY(1.0F).setInterpolator(interpolator);
com.nineoldandroids.view.ViewPropertyAnimator.animate(v).setStartDelay(animDuration).start();
}
sorry for bad english !
tnx to all
-------------------------------EDIT-----------------------------
code executed right on android 2.2 but not like android 4 !
ViewPropertyAnimator is for api level >11.However; you can use nineoldandroids library project which is a proxy for this animations (and quite good).
Or you can simply use Animation class
UPDATE:
i missed the part you said u already use nineoldandroids. did you check your imports maybe you imported the native
ViewPropertyAnimation
it may cause problem too. That libray uses the native api if api level is >11 you dont need to import native one.
You need to import ViewHelper class of nineoldandroid like
import com.nineoldandroids.view.ViewHelper;
and then use following code
ViewHelper.setTranslationX( Your View, 0.0F);
ViewHelper.setTranslationY( Your View, height);
ViewHelper.setRotationX(Your View,45.0F);
ViewHelper.setScaleX(Your View,0.7F);
ViewHelper.setScaleY(Your View,0.55F);
instead of
v.setTranslationX(0.0F);
v.setTranslationY(height);
v.setRotationX(45.0F);
v.setScaleX(0.7F);
v.setScaleY(0.55F);
Since Nineoldandroids allow api 1> use animation methods.
However, I run in API8 and Force Close is occured!
HERE is the solution, it's because of Nineoldandroids
[http://answer.techwikihow.com/962376/nineoldandroids-animation-working-api10.html][1]
Use Library of NineOldndroids Folder as Dependency instead of .jar,
Modification some code in
ObjectAnimator.Class
by following the answer in the link above!
Related
I know there are several similar questions regarding this has been asked here, however none of the solutions worked for me.
I have just transferred my application from Ecilpse(juno) to Android Studio 1.5.1; and from API 19 to API 23(compileSdkVersion).
Currently I encountered this error whereby "getResources().getColor(R.color.my_color)" is depreciated and cannot be used.
After searching online, I tried to use "ContextCompat.getColor(context, R.color.my_color)" instead. The error lies at the "getColor" part as it says that it cannot be resolved:
The method "ContextCompat" however allow me to use "getDrawable", "getExternalCacheDirs", "getExternalFilesDirs" and "getObbDirs"; just except for "getColor"
I have also ensure that I have these 2 in my app build.gradle (under dependencies{}):
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
Below are my imports for this class (DetailsActivity):
Additionally, I have also tried using "ResourcesCompat" other than "ContextCompat"; and it still did not work out for me.
I am still a beginner at Android Development, working on a school project.
Can anyone kindly provide some suggestions as of what I am doing wrong and point me towards the right direction? Thanks a lot in advance!
You can use this method.
This is the ContextCompat Developer Link from the Support Library:
public static final int getColor(Context context, int id) {
final int version = Build.VERSION.SDK_INT;
if (version >= 23) {
return ContextCompat.getColor(context, id);
} else {
return context.getResources().getColor(id);
}
}
You don't have to use ContextCompat there if you are compiling with an API level greater than 24 as this is only a compatibility feature for early versions. You can also still use Context.getColor() But notice the method signature! If you just want a color in the default theme you only have to provide the int value of its reference: http://developer.android.com/reference/android/content/Context.html#getColor(int)
You are currently trying to access getColor() with two parameters (Color and Theme) which cannot work there as there is no method who supports these parameters. If you remove the 2nd parameter your current solution will work.
But it would be more convenient to use Context or Resources directly and drop the v4 Appcompat reference if you don't need it.
Before to use this method, fist of all add the Dependencies in build.gradle script. Which is as follows:
dependencies {
// other stuff here
compile 'com.android.support:support-v4:23.0.0'
}
I have a button that I want to set the background of using a png file from internal storage. For android api 16 and up, this works fine:
filePath = getActivity().getFileStreamPath(colorCodes.get(i-1));
temp.setBackground(Drawable.createFromPath(filePath.toString()));
When running on an android tablet with 4.0.4, this part crashes the app with a nosuchmethod error (setBackground). After a little research, I see that setBackground is only available for api 16+. After looking around on SO and a few other places, it looks like I need to use setBackgroundDrawable (deprecated) or setBackgroundResource. I tried this:
filePath = getActivity().getFileStreamPath(colorCodes.get(i-1));
if (android.os.Build.VERSION.SDK_INT < 16) {
temp.setBackgroundDrawable(Drawable.createFromPath(filePath.toString()));
} else {
temp.setBackground(Drawable.createFromPath(filePath.toString()));
}
When logging it out, it shows that setBackgroundDrawable is running and not setBackground, but I get the same nosuchmethod error (setBackground).
The other option is setBackgroundResource, but it accepts an int and not a drawable. Can I convert from drawable to int for this purpose?
What can I do here to set the background of the button to a file in internal storage for APIs < 16?
Thanks.
***EDIT - ok, this is working. just missed a little part elsewhere in the code that had the same problem. However, is using a deprecated method really the only way?
Deprecation is a status applied to a computer software feature,
characteristic, or practice indicating it should be avoided, typically
because of it being superseded. The term is also sometimes used for a
feature, design, or practice that is permitted but no longer
recommended in other areas, such as hardware design or compliance to
building codes. (source link)
Now we can answer your question.
Before API level 16 there is a method named setBackgroundDrawable. After API Level 16 google decided to write a new method setBackground for same purpose and recommend us to use new method. (Reason of this may be found by googling.)
You can use setBackgroundDrawable method for all api levels. There aren't any constraint for this. But using new method setBackground is recommended after API Level 16.
But you can only use setBackground method for devices which is running on API Level 16 or higher. So if you only implement setBackground method in your code, you are going to get MethodNotFoundException for devices which run below API Level 16.
To sum up; it is a best practice(for me it is a must) to use new methods then deprecated ones with supportted api version check such as;
if (android.os.Build.VERSION.SDK_INT < 16) {
temp.setBackgroundDrawable(Drawable.createFromPath(filePath.toString()));
} else {
temp.setBackground(Drawable.createFromPath(filePath.toString()));
}
I am not quite sure whether it is the only way to achieve this but in my opinion it is the correct one. Because the annotation #Deprecated defines the method to be superseded (in most cases) it automatically implies you can (I would even say should) use it to address older versions which are the targeted versions of this method.
I'm working on an app that is targeted for version 2.3 so that it will run on my sister's phone. However, I can run 4.0 on my phone. I want to add some swipe animations and such but I don't run the animations to run on her phone.
Is this even possible?
Yes, simply put the API specific code in a if/else block, so it is only called when the system supports it:
Like this:
if (currentapiVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
//do things that are only supported on JellyBean
} else {
//do the other stuff
}
You can use a ViewPager and FragmentPagerAdapter to provide swipe functionality among various Fragments. All of which are available in the support library.
ViewPager(Note, don't do anything with the ActionBar, as they are not yet in the support library):
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
FragmentPagerAdapter(pretty much the example code you will need):
http://developer.android.com/reference/android/support/v13/app/FragmentPagerAdapter.html
Fragment:
http://developer.android.com/reference/android/support/v4/app/Fragment.html
Is there any kind of conditional compiling for Android?
I had to make my project for Android 3 (API 11) just because ExifInterface has almost no useful attributes in Android 2.3 (API 10), despite the fact that it appeared in API 5 (!!??). I don't want to restrict my app to ICS users.
Thanks!
You can check dynamically the current API version of the device and do different stuff depending on that:
if(Build.VERSION.SDK_INT < 14) {
// Crappy stuff for old devices
}
else {
// Do awesome stuff on ICS
}
But be careful that if you need to instantiate classes that are not available for all APIs then you should do it in a runnable or in a separate wrapper class, e.g:
if(Build.VERSION.SDK_INT < 14) {
// Crappy stuff for old devices
}
else {
// Do awesome stuff on ICS
new Runnable() {
new AmazingClassAvailableOnICS();
(...)
}.run();
}
import android.annotation.TargetApi;
and then use annotations:
#TargetApi(11)
public void methodUsesAPI11()
{
...
Using this trick does a very simple thing: it allows compiling some code which contains API level 11 calls (classes, methods, etc) and still set android:minSdkVersion="8" in the manifest. Nothing more, nothing else.
The rest is up to you. You must check platform version before you call methodUsesAPI11() or you handle exceptions in order to prevent app crash and perform other action on older platforms.
Checking Build.VERSION.SDK_INT or using annotations should suffice, however, this link I'd bookmarked might be relevant to your case:
http://android-developers.blogspot.com/2010/07/how-to-have-your-cupcake-and-eat-it-too.html?m=1
You can use what they describe there to have classes that may not be compatible, but will never be loaded. It's not conditional compilation, but it may be what you need, however, it is a bit more complex.
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;