Using finishAndRemoveTask() method of Activity class on API levels lower than 21 - android

I need to delete app from recently used apps list. There is no problem on API level 21 and above. I use finishAndRemoveTask() method. But that method cannot be used on API levels lover than 21. How can I call that method on API levels lover than 21?
I tried that solutions but they does not work :
Close application and remove from recent apps/

Prease, check code below
if (Build.VERSION.SDK_INT >= 21)
finishAndRemoveTask();
else
finish();
System.exit(0);

I solved the problem with that library :
https://github.com/Ereza/CustomActivityOnCrash

Related

Change Api level android

I'm trying to implement ViewOutlineProvider in my project for set a shadow in my tab, but the logcat said me to change API level version to 21. (my min API version is 15) Are you advice this?
Thanks!
That's because ViewOutlineProvider support API>21, you can use if statement to apply this feature for devices with an API higher that 21 :
if(Build.VERSION.SDK_INT >= 21){
// set shadow ....
}

Comparing resources in API 21 x API 23

My app is running well in API 23 and above, but there seems to be a bug in it when I run it in API 21. I've narrowed it down to this piece of code:
if (myButton.getBackground().getConstantState() == ResourcesCompat.getDrawable(getResources(), R.drawable.myDrawable, null).getConstantState())
{...}
In API 21 it always returns false (even when it shouldn't), so I guess one of those methods doesn't work in API 21.
Any ideas? Is there any other way (compatible with API 21 and above) to check if a button's background has a specific drawable assigned to it?
ps: my minsdk is 21, but Lint is not giving me any warnings.
You can try using ContextCompat:
ContextCompat.getDrawable(yourContext, R.drawable.myDrawable).getConstantState();

How to use txtV1.setBackground(getResources().getDrawable(R.drawable.bcgrtxt));`in Older Devices?

How to use
txtV1.setBackground(getResources().getDrawable(R.drawable.bcgrtxt));
in Older Devices ?
View.setBackground replaced View.setBackgroundDrawable as of API level 16.
.setBackground was added in API Level 16
and .setBackgroundResource was added in API Level 1
so you can use .setBackgroundResource instead of .setBackground

API Level error

In an Activity I have a code that shows this error, but only if you press save.
Call requires API level 11 (current min is 8): android.widget.SearchView#setSearchableInfo
If I change the android:minSdkVersion to 7, it works, but when I save the code again, the same error is thrown. The minSdk must then be changed back to 8,...
What is wrong?
SearchView is available since API lvl 11.
Since your minimum sdk is 8 (lower than 11), Lint will give an error when using SearchView.
You can remove that error by using #TargetApi annotation before your method or class.
But you have to make sure you use a conditional statement before using SearchView to check if it is available, and provide an alternative for earlier versions.
Here's what your code should look like:
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
void yourMethod(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
// use SearchView
} else {
// use some other backward compatible custom view
}
}
The SearchView exists in Android from the version 11 ant more.
So, if you would like to use the SearchView in your code, you have to put the minSdkVersion in your manifest to be 11. In the case you put a number smaller than 11, you will get an error, which is normal beause you are giving access to your app to some android versions which will not support your app.
This can be seen (thanks to #JesseJ) here: http://developer.android.com/reference/android/widget/SearchView.html
Added in API level 11

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