spannablestring

In Android, if I use binding, how to apply ClickableSpan?

℡╲_俬逩灬. 提交于 2019-12-25 01:35:49
问题 I know that if binding is not applied, then I can use Textview.setText(ClickableSpan) to apply spannig effect to make part of the text clickable. However, my app used binding and the textview's text is binded to a ViewModel inside xml like android:text="@{model.certainString}" and in the ViewModel I declared String certainString = "" , in this case how do I apply spanning inside the ViewModel then? 回答1: You could write your own BindingAdapter which accepts either the model or the string value

NullPointerException when long press on TextView with SpannableString

旧时模样 提交于 2019-12-24 17:53:46
问题 I have a TextView with SpannableString because I want the do differnet thing when user click different position of the view. Here is my code: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); String str = "ClickMe"; SpannableString spStr = new SpannableString(str); ClickableSpan clickSpan = new CustomizedClickableSpan(str); spStr.setSpan(clickSpan, 0, str.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); tv.setText(

Android 中 TextView 部分文字有不同的颜色和部分文字点击事

混江龙づ霸主 提交于 2019-12-24 08:32:58
我们在开发中,经常遇到一段文本,需要单独给它部分文字设置不同的样式,有的文字设置为粗体,有的文字设置特殊的颜色,有的地方要加入表情,遇到数学公式还可能要设置上下标,这时候该怎么办呢? 有的人会采用不同的 TextView,这样就可以完美解决了。先不说这个方法行不行得通,事实上,若采用这种方式,当碰上一段文字需要设置非常多的样式时,光是这一堆TextView就够浪费资源的了,布局还复杂,也不利于维护,因此这种方式一般不会被采用。 那么有其他办法吗?有,并且还很简单,今天介绍的这个SpannableString就是用来解决这个问题的。 xml 代码片段: <TextView android:id="@+id/tv_value" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="要变色的值" /> 第一种方法: TextView tvValue = findViewById(R.id.tv_value); String str="默认颜色<font color='#FF0000'><small>红颜色 </small></font>"; tvValue.setTextSize(18); tvValue.setText(Html.fromHtml(str));

Insert line separator in spannablestringbuilder after imagespans (Android)

孤街浪徒 提交于 2019-12-24 06:39:46
问题 I have a spannablestringbuilder with a number of imagespans in it. I would like to insert a line separator after the image to ensure that text following it starts on the next line. I am trying to do this in a loop, but it doesn't insert the line seprator and breaks the spans. ImageSpan[] imageSpans = strBuilder.getSpans(0, strBuilder.length(), ImageSpan.class); for (ImageSpan imageSpan : imageSpans) { strBuilder = strBuilder.insert(strBuilder.getSpanEnd(imageSpan), System.getProperty("line

Dotted underline TextView not wrapping to the next line using SpannableString in Android

五迷三道 提交于 2019-12-24 02:48:48
问题 I done the dotted underline textview using this Dotted underline in TextView using SpannableString in Android. But dotted underline textview not wrapping to the next line . I have attached screenshot for reference. Please advice your ideas. Thanks class DottedUnderlineSpan(mColor: Int, private val mSpan: String) : ReplacementSpan() { private val paint: Paint private var width: Int = 0 private var spanLength: Float = 0f private val lengthIsCached = false internal var strokeWidth: Float = 0f

Align bitmap in SpannableStringBuilder

会有一股神秘感。 提交于 2019-12-23 20:13:55
问题 How can I align a bitmap to the text in a SpannableString? SpannableStringBuilder ssb = new SpannableStringBuilder(arr_messages.get(position)); String msg1 = ssb.toString(); for (int i=0; i<smileys.length; i++) { if (msg1.indexOf(smileys[i]) > 0) { int t = msg1.indexOf(smileys[i]); ssb.setSpan(new ImageSpan(bitmapArray.get(i)), t, t+2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } holder.txtName.setText(ssb, BufferType.SPANNABLE); I would like to demonstrate the problem with these images: What I

Html Tag Handler not called in Android N for “ul”, “li”

一曲冷凌霜 提交于 2019-12-23 07:36:59
问题 We have a custom TagHandler in our app for bulleted list etc. html = "<ul><li>First item</li><li>Second item</li></ul>"; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { result = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY, null, new ListHTMLTagHandler(density)); } else { //noinspection deprecation result = Html.fromHtml(html, null, new ListHTMLTagHandler(density)); } The handleTag() function in my TagHandler is called for ul , li in API-23 and below but not

Html Tag Handler not called in Android N for “ul”, “li”

試著忘記壹切 提交于 2019-12-23 07:35:00
问题 We have a custom TagHandler in our app for bulleted list etc. html = "<ul><li>First item</li><li>Second item</li></ul>"; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { result = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY, null, new ListHTMLTagHandler(density)); } else { //noinspection deprecation result = Html.fromHtml(html, null, new ListHTMLTagHandler(density)); } The handleTag() function in my TagHandler is called for ul , li in API-23 and below but not

How to make spannable with multiple text?

自作多情 提交于 2019-12-23 02:45:12
问题 String sentence="this is @part 1 and #here another and #another one @user #newone @itsme "; textView.setMovementMethod(LinkMovementMethod.getInstance()); textView.setText(addClickablePart(sentence), BufferType.SPANNABLE); private SpannableStringBuilder addClickablePart(String str) { SpannableStringBuilder ssb=new SpannableStringBuilder(str); int idx1=Math.min(str.indexOf("@"),str.indexOf("#") ); int idx2=0; while (idx1!=-1) { idx2=str.indexOf(" ", idx1) + 1; final String clickString=str

How to highlight TextView in Android

醉酒当歌 提交于 2019-12-23 02:35:02
问题 I would like to highlight a TextView and achieve the design shown below. Does anyone know how to achieve this using a TextView ? I took this screenshot from an existing Android app. By using this code I get results shown below, which is not what I want: sp.setSpan(new BackgroundColorSpan(color), start, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 回答1: You need to use a Spannable String: TextView textView = (TextView)findViewById(R.id.textView1); String text = "Test"; Spannable spanText =