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);
Related
How can I replace the selected text with something in android's edittext?
For example:
Default Text
Assume "Default" is selected now.
I want to replace it with !boldDefault.
How can I do it? I tried using get selection method but I couldn't replace it.
If the EditText currently has text selected, you can access the start and end points of the selection like so:
int start = editText.getSelectionStart();
int end = editText.getSelectionEnd();
You can access the EditText's Editable text like this:
Editable edit = et.getText();
Now you can replace anything inside that Editable using the replace() method:
String newText = "this will replace the current selection";
edit.replace(start, end, newText);
Once you've done that, you probably want to change the selection so that you don't have part of your new text still selected:
editText.setSelection(start + newText.length());
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.
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);
I want to calculate string font width in pixels in android. I am having string array and I am appending all these array elements to form one string. so I have to set fixed spacing in between two consecutive array element.i am placing this final string into textview. Finally I am adding this textview to tablerow. In short i want to view table structure with title and values. I have seen many post over here but not getting solution for this.
Thanks in advance...!!!
Looks like there is a measureText method available on Paint. I also found an example:
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(5);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setTextSize(64);
mPaint.setTypeface(Typeface.create(Typeface.SERIF, Typeface.ITALIC));
// ...
float w = mPaint.measureText(text, 0, text.length());
Thanks Jake for the working solution, yet I found this to work for me too:
someText = "Lorem Ipsum";
TextView myTextView = (TextView) findViewById(R.id.myTextView);
float w = myTextView.getPaint().measureText(someText);
Getting the Paint directly from the TextView that I'm going to work with makes me feel that the anwser will be more accurate.
I don't know if this works, but it can.
int textSize=20;
Paint p=new Paint();
p.setTextSize(textSize);
String string="Hello im a string.";
public int getStringWidt()
{
return string.length*textSize;
}