Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to highlight text in android studio

textView.setText("Hello, I am Awesome, Most Awesome"); // set text first
setHighLightedText(textView, "a"); // highlight all `a` in TextView
Comment

how to highlight text in android studio

/**
     * use this method to highlight a text in TextView
     *
     * @param tv              TextView or Edittext or Button (or derived from TextView)
     * @param textToHighlight Text to highlight
     */
    public void setHighLightedText(TextView tv, String textToHighlight) {
        String tvt = tv.getText().toString();
        int ofe = tvt.indexOf(textToHighlight, 0);
        Spannable wordToSpan = new SpannableString(tv.getText());
        for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
            ofe = tvt.indexOf(textToHighlight, ofs);
            if (ofe == -1)
                break;
            else {
                // set color here
                wordToSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe + textToHighlight.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
            }
        }
    }
Comment

PREVIOUS NEXT
Code Example
Java :: bfs java 
Java :: how to do annotation configuration in spring 
Java :: Number to decimal places in java 
Java :: remove java ubuntu 20.04 stackoverflow 
Java :: remove tableview separator lines in javafx 
Java :: java try-with-resources 
Java :: java Convert a string IPv4 IP address to the equivalent long numeric value. 
Java :: java convert ip to long 
Java :: how to send http post create request using curl command 
Java :: frame background changing in java 
Java :: array reverse in java 
Java :: deifine an integer array in java 
Java :: transformer un string en Biginteger java 
Java :: get free player inventory slots spigot 
Java :: how to create a console in java gui 
Java :: string to char 
Java :: java check if int is null 
Java :: horizontal recyclerview item width half of screen android 
Java :: recursion of numbers in decending order in java 
Java :: mod 10e9+7 in java 
Java :: how to remove letters from string java 
Java :: java inner method 
Java :: java run jnlp 
Java :: jsoup remove element 
Java :: Write code to declare an array that will hold calendar months (.e. January to December) java 
Java :: java string split underscore 
Java :: maths.random in Java 
Java :: java string loop backwards 
Java :: random number 
Java :: url string from url java 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =