blob: 9c1171900e50178ea169c138933da647d57c38fa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package it.niedermann.owncloud.notes.shared.util;
import android.os.Build;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
import androidx.annotation.NonNull;
import static android.os.Build.VERSION_CODES.N;
/**
* Some helper functionality in alike the Android support library.
* Currently, it offers methods for working with HTML string resources.
*/
public class SupportUtil {
private SupportUtil() {
}
/**
* Fills a {@link TextView} with HTML content and activates links in that {@link TextView}.
*
* @param view The {@link TextView} which should be filled.
* @param stringId The string resource containing HTML tags (escaped by <code><</code>)
* @param formatArgs Arguments for the string resource.
*/
public static void setHtml(@NonNull TextView view, int stringId, Object... formatArgs) {
view.setText(SupportUtil.fromHtml(view.getResources().getString(stringId, formatArgs)));
view.setMovementMethod(LinkMovementMethod.getInstance());
}
/**
* Creates a {@link Spanned} from a HTML string on all SDK versions.
*
* @param source Source string with HTML markup
* @return Spannable for using in a {@link TextView}
* @see Html#fromHtml(String)
* @see Html#fromHtml(String, int)
*/
private static Spanned fromHtml(String source) {
if (Build.VERSION.SDK_INT >= N) {
return Html.fromHtml(source, Html.FROM_HTML_MODE_LEGACY);
} else {
//noinspection deprecation
return Html.fromHtml(source);
}
}
}
|