I want to color specific words in a string.
At the moment I am using this:
SpannableString txt = new SpannableString(txt.toString());
Pattern color = Pattern.compile("My Text");
Matcher mat = color.matcher(txt);
while(mat.find()) {
txtt.setSpan(new ForegroundColorSpan(Color.BLUE), mat.start(), mat.end(), 0);
}
This is working perfect for one text.. but the problem is that I want color more than one piece of text in differed colors.
I already tried it with:
line = line.replace("My Text",
"<font color='#0000ff'>" + "$0" + "</font>");
But StringBuilder seems not to work with this
textViewTs.setText(Html.fromHtml(line));
UPDATE:
if i try to continue the while loop like this
SpannableString txtt = new SpannableString(txt.toString());
Pattern color = Pattern.compile("first text");
Matcher mat = color.matcher(txt);
Pattern color2 = Pattern.compile("second text");
Matcher mat2 = color2.matcher(txt);
while(mat.find()) {
txtt.setSpan(new ForegroundColorSpan(Color.BLUE), mat.start(), mat.end(), 0);
} while(mat2.find()) {
txtt.setSpan(new ForegroundColorSpan(Color.YELLOW), mat.start(), mat.end(), 0);
}
textViewTs.setText(txtt);
I get a error that reads java.lang.IllegalStateException: No successful match so far
Any good way to solve this?
try with the replace() method from the SpannableStringBuilder (http://developer.android.com/reference/android/text/SpannableStringBuilder.html) instead of using the setSpan().
I have something perfect for you! I wrote some code and uploaded it to my website. You can find the page with intrsuctions on how to use it here and the code to add to your src folder here.
I made this for LibGDX but it should work the same. All you need to do is change the code to change the color of the text and the code to draw the text! For example, if your using SurfaceView you could replace this:
if(character == 'r'){
font.setColor(Color.RED);
}
with this:
if(character == 'r'){
paint.setColor(Color.RED);
}
Just an example, I don't thinks that's what your using, right? Just change it however you need it.
How it Works:
To change the color just add codes to the string you want to draw. For example, just say you want to draw "Hello World!" with "Hello" in red and "World" in blue at coordinates 50, 40 just run this:
ColorText.draw("&rHello &bWorld!", 1, batch, 50, 40);
Note! You will have to do a lot of finetuning for this to work for you but the basic code is there for handling the string.
Related
I searched for this issue and I found the below codes, but these codes are for Java-Android. What are the similar codes that I can use with Java
The codes are:
ArrayList<Integer> positions = new ArrayList();
Pattern p = Pattern.compile("mp"); // insert your pattern here
Matcher m = p.matcher("Simple Text, bumping , jumping");
while (m.find()) {
positions.add(m.start());
}
Spannable spanning = new SpannableString("Simple Text");
spanning.setSpan(new ForegroundColorSpan(Color.BLUE),positions.get(i), positions.get(i)+1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(spanning);
Are you using Java Swing?
If yes, you can put "<html>YOUR TEXT</html>" with needly customizations for you design.
Put it inside an new JLabel("<html>YOUR..."); It is the principle of Java graphics design, and today we have the JavaFX that will replace these method of interface programming
Your solution.
You can change the colour of the label.
Add this to your jframe or jlabel as you have used in your project.
Thank you
Label label = new Label("Your text");
//sets the colour of text
label.setForeground(Color.BLACK);
//sets the backround of text
label.setBackground(Color.BLUE);
I am looking to add formatted text to a ListView (I would like the text to be the color #33B5E5). This is what I tried so far:
ArrayList myarraylist;
String mystring = "Value I want to add"
myarraylist.add(mystring);
//other functions to set the arraylist
My question is, how would I change the color of mystring? Is it possible to set the text color of a string?
You can use ArrayAdapter, and create a List<SpannableString>, SpannableString is implementation of CharSequence, so it can behaviors like CharSequence.
the code below is to create a MAGENTA color text.
msp = new SpannableString("Hello I am colo Magenta");
msp.setSpan(new ForegroundColorSpan(Color.MAGENTA), 12, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
I think you are looking for this:
myTextView.setTextColor(getResources().getColor(android.R.color.orange.black));
Add fontcolor to your string...
ArrayList<String> myarraylist = new ArrayList<String>();
String mystring = "<font color='#33B5E5'>Value I want to add</font>";
myarraylist.add(mystring);
add it to textview...
((TextView)findViewById(R.id.tvTime)).setText(Html.fromHtml(myarraylist.get(0)), TextView.BufferType.SPANNABLE);
I have put in my app one textview inside a fragment. Each page has a different text. I want every time you find for example the word "tree", this word is painted color red and the rest words white. How could I do it? thanks
Pattern pattern = Pattern.compile("tree");
Matcher matcher = pattern.matcher(yourTextViewText);
final SpannableStringBuilder spannableBuilder = new SpannableStringBuilder(yourTextViewText);
final ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
while (matcher.find()) {
spannableBuilder.setSpan(
span, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
yourTextView.setText(spannableBuilder);
This will replace all the word "tree" with RED Color.
See More About Pattern Here
To change the colour of the text, use:
textview.setTextColor(Color.RED);
I have a TextView, and I need to change a color for a 1 symbol from the TextView. I have made it already, but I forgot how it is possble. There is specia class for working with strings - it possible to change a color, to underline the text, etc. Please, tell me this class. Thank you.
like this:
final SpannableStringBuilder result = new SpannableStringBuilder();
....
result.setSpan(new ForegroundColorSpan(redColor), result.length() - 1, result.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
....
recognitionText.setText(result);
What would be an easy way to set the background color of all characters from some start to an end position in Java.
Eg:
red background from position D(4) to J(10)
String alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
Many thanks in advance.
Edit:
Oh no, 2 '-'? Ok here is what I'm doing.
JTextArea textArea = new JTextArea("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
textArea.setFont(new Font("Courier", Font.PLAIN, 16));
//then I want red background from position D(4) to J(10)
please read Using Text Components and tons examples for that
look here for excelent workaround
Split the String into 3 separate Strings.
String a = alpha.substring(0,4);
String b = alpha.substring(4,11);
String c = alpha.substring(11);
Then display a,b, and c next to each other while only formatting b as desired.