I'm using this code to search words within in a textView.
The buttons and the EditText are displayed but they don't work.
In the code there is a warning that says
The method findAll(String) from the type WebView is deprecated
for the line
wv.findAll(findBox.getText().toString());
The rest of the code is in this link.
Use
wv.findAll(findBox.getText().toString());
for API versions < 16 and Use
wv.findAllAsync(findBox.getText().toString());
for API versions > 16.
Use the following code to ensure that your code works on all versions:
int API = android.os.Build.VERSION.SDK_INT;
if(API < 16) {
wv.findAll(findBox.getText().toString());
} else {
wv.findAllAsync(findBox.getText().toString());
}
Related
I would like to remove the deprecation warning for Html.fromHtml(string).
I tried to do like this:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
htmlSpanned = Html.fromHtml(string,Html.FROM_HTML_MODE_LEGACY);
} else {
//noinspection deprecation
htmlSpanned = Html.fromHtml(string);
}
but it still gives me a warning during compilation:
Warning:(18, 31) [deprecation] fromHtml(String) in Html has been
deprecated
Well, the one-parameter fromHtml() is deprecated. The Build checks ensure that you will not call it on older devices, but it does not change the fact that it is deprecated with a compileSdkVersion of 24.
You have four choices:
Drop your compileSdkVersion below 24. This has rippling effects (e.g., you cannot use 24.x.x versions of the support libraries) and is not a great option.
Set your minSdkVersion to 24 and get rid of the one-parameter fromHtml() call. This is impractical in 2016.
Live with the strikethrough and Lint complaints.
Add the appropriate #SuppressLint annotation to the method, to get the IDE to stop complaining. As Ahlem Jarrar notes, the simplest way to add this is via the quick-fix.
If your minSdkVersion is 24 or higher, use the version of fromHtml() that takes some flags as a parameter . AFAIK, FROM_HTML_MODE_LEGACY would be the flag value to use for compatibility with the older flag-less fromHtml().
If your minSdkVersion is lower than 24, your choices are:
Always use the fromHtml() you are, possibly using the quick-fix (Alt-Enter) to suppress the Lint warning
Use both versions of fromHtml(): the one taking the flags if your app is running on an API Level 24+ device, or the one without the flags on older devices.
This was my solution at one point:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textField.setText(Html.fromHtml(htmlText, Html.FROM_HTML_MODE_COMPACT));
} else {
textField.setText(Html.fromHtml(htmlText);
}
It worked just fine.
You can try it. I mean your code looks well, I tried it and it worked for me.
// get our html content
String htmlAsString = getString(R.string.html);
Spanned htmlAsSpanned = Html.fromHtml(htmlAsString);
// used by TextView
// set the html content on the TextView
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(htmlAsSpanned);
Our you can try it:
#SuppressWarnings("deprecation")
Android or Kotlin or Google has created HtmlCompat which can be used instead of the method Html. Add this dependency implementation 'androidx.core:core:1.0.1' to the build.gradle file of your app. Make sure you use the latest version of androidx.core:core.
This allows you to use:
HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY);
You can convert the HTML.FROM_HTML_MODE_LEGACY into an additional parameter if you want. This gives you more control about it which flag to use.
You can read more about the different flags on the HTML Class documentation
I'm using the following code to find and highlight text in a webView:
int API = android.os.Build.VERSION.SDK_INT;
if(API < 16) {
wv.findAll(findBox.getText().toString());
} else {
wv.findAllAsync(findBox.getText().toString());
}
However, it fails on api 15 only. It finds the word but doesen't highlight it. Do you have a solution?
This is a known problem without a solution that I know of. This occurs on the following:
Platform Version API Level VERSION_CODE
Android 4.0.3, 4.0.4 15 ICE_CREAM_SANDWICH_MR1
This problem is discussed: Here and Here.
I want to change the background of my framelayout (which holds all my pages!) by:
FrameLayout fl = (FrameLayout)findViewById(R.id.container);
fl.setBackground(getResources().getDrawable(R.drawable.juraquiz_app_background));
but apparently I cant. Is there a way to do it, so its compatible with APIs lower than 16?
for API's lower that 16 you can use setBackgroundDrawable
Use different methods for different APIs:
final Drawable drw = getResources().getDrawable(R.drawable.juraquiz_app_background);
if(android.os.Build.VERSION.SDK_INT < 16)
{
fl.setBackgroundDrawable(drw);
}
else
{
fl.setBackground(drw);
}
You will need to add an annotation to your method (or to your class, if you prefer) to get rid of Lint warnings:
#SuppressLint("NewApi")
I want to use the LayoutTransition class that it can achieve animation.But Eclipse tell me Call requires API level 11 (current min is 7). And I just want to call this API in Android2.1+. So, Here What way can be deal with it(such as the Open Sources library)?
Thank you very much for your answer.
This error comes from Android Lint. You can suppress it by adding an
#SuppressLint("NewApi"). Then in the body of the method using LayoutTransition you can add the following code:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//use LayoutTransition in your code
} else {
//on Android 7-11, don't use LayoutTransition
}
On my settings screen I have a date picker widget. In the designer in Eclipse, it shows as I want (3 spinners for D-M-Y) but when I test on my device, I get a rather odd view with a side spinner on the left and a calendar on the right. Never seen this before(!) but doing some research I think I'm seeing the "CalendarView".
I found that I should be able to set a "calendarViewShown" property to false- but my XML throws an error with this. I found another question on here that suggested the API level was to blame (my minSDKLevel is 7, but I'm targetting 11 so I can get the action bar button rather than the oldskool menu).
So I thought I'd try setting it in code:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 11)
minDateSelector.setCalendarViewShown = false;
But again, this fails- setCalendarViewShown isn't found. But the docs here say it should exist.
Any ideas?!
If you are targeting a later version of the API, you can use the following XML (no need to write Java code) in your <DatePicker>:
android:calendarViewShown="false"
The method in DatePicker
public void setCalendarViewShown (boolean shown)
exists starting with API 11. If you minSdkLevel = 7 the compiler does not recognize this as a valid method - the method does not exist on android 2.3 or 2.2. The best way is to solve this is using reflection. Something like this should work properly:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 11) {
try {
Method m = minDateSelector.getClass().getMethod("setCalendarViewShown", boolean.class);
m.invoke(minDateSelector, false);
}
catch (Exception e) {} // eat exception in our case
}
I made it work with the following XML configuration:
android:datePickerMode="spinner"
android:calendarViewShown="false"
Only the following configuration didn't work for me:
android:calendarViewShown="false"
In those cases I use
import android.os.Build;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void someThing() {
[...]
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
minDateSelector.setCalendarViewShown(false);
}
}
I think the readability is better than using reflection and the style is better than catch and ignore exceptions. Of course the reflection thing is also working.
I had the same problem as you, I couldn't make the change appear via XML.
You are on the right track, try changing your last line to:
minDateSelector.setCalendarViewShown(false);