Highlighting Text Color using Html.fromHtml() in Android?

后端 未结 9 1296
忘了有多久
忘了有多久 2020-11-28 19:26

I am developing an application in which there will be a search screen where user can search for specific keywords and that keyword should be highlighted. I have found Html.f

相关标签:
9条回答
  • 2020-11-28 20:19
     String name = modelOrderList.get(position).getName();   //get name from List
        String text = "<font color='#000000'>" + name + "</font>"; //set Black color of name
        /* check API version, according to version call method of Html class  */
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N) {
            Log.d(TAG, "onBindViewHolder: if");
            holder.textViewName.setText(context.getString(R.string._5687982) + " ");
            holder.textViewName.append(Html.fromHtml(text));
        } else {
            Log.d(TAG, "onBindViewHolder: else");
            holder.textViewName.setText("123456" + " ");   //set text 
            holder.textViewName.append(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY));   //append text into textView
        }
    
    0 讨论(0)
  • 2020-11-28 20:22

    Alternative solution: Using a WebView instead. Html is easy to work with.

    WebView webview = new WebView(this);
    
    String summary = "<html><body>Sorry, <span style=\"background: red;\">Madonna</span> gave no results</body></html>";
    
    webview.loadData(summary, "text/html", "utf-8");
    
    0 讨论(0)
  • 2020-11-28 20:23
    textview.setText(Html.fromHtml("<font color='rgb'>"+text contain+"</font>"));
    

    It will give the color exactly what you have made in html editor , just set the textview and concat it with the textview value. Android does not support span color, change it to font color in editor and you are all set to go.

    0 讨论(0)
提交回复
热议问题