I'm using indexof() to find the length of a specific character, then color it with different color. I can start the coloring exactly with the character using the indexof() which is great, but I can't end it to the word.
if(title.contains("x")){
SpannableString WordtoSpan = new SpannableString(title);
WordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), title.indexOf("x"), title.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.txtTitle.setText(WordtoSpan);
} else {
holder.txtTitle.setText(title);
}
Now if it contains the character x it will color it with red from the first x it finds. But it won't end until the last of the string which I dont like.
I want it to end with the end of the word that started with an x.
ex. Now: (Bold is the color red atm)
I love x-man and chocolate.
What I want is:
I love x-man and chocolate.
how do I achieve that?
You must find where the word ends, not simply passing length();
Consider a more robust algorithm to find word separator (ie include tabs), in the example I simply find the first whitespace
String s = "I love x-man and chocolate.";
int idx = s.indexOf("x");
if (idx >= 0) {
int wordEnd = s.indexOf(" ", idx);
if (wordEnd < 0) {
wordEnd = s.length();
}
WordtoSpan.setSpan(new ForegroundColorSpan(Color.RED),
idx,
wordEnd,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Related
How to set span for special characters, like \n \t \r , etc ? Right now if i do this:
getText().setSpan(DynamicListView.mBackgroundColor, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Thanks
Not 100% sure I understand the question, as the special characters '\n' nd '\r' are line breaks, so how could they be styled as they will be invisible?
That said, here is a method for styling certain chars in an EditText. You can add as many chars as you like to the end of the method and any instances of those characters will be styled.
//...
editText.setText(getSpannedText(editText.getText(), `u`, `r`));
//...
private static SpannableString getSpannedText(String text, char... triggers) {
SpannableString spanString = new SpannableString(text);
for (int i = 0; i < spanString.length(); i++) {
for (char trigger : triggers) {
if (spanString.charAt(i) == trigger) {
spanString.setSpan(new ForegroundColorSpan(Color.CYAN), i, i+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
return spanString;
}
Fixed on my side by Draw functions, here is code of EditText:
if(getText().toString().substring(start,end).indexOf("\t")>=0
|| getText().toString().substring(start,end).indexOf("\n")>=0
|| getText().toString().substring(start,end).indexOf("\r")>=0) {
TextPaint paint = new TextPaint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(mBackgroundColor);
paint.bgColor = mBackgroundColor;
Layout layout = getLayout();
int line = layout.getLineForOffset(start);
int baseline = layout.getLineBaseline(line);
int ascent = layout.getLineAscent(line);
float x = layout.getPrimaryHorizontal(start);
float y = baseline + ascent;
Rect rect = new Rect();
//rect.set(start, 0, Math.round(layout.getSecondaryHorizontal(end)), getLineHeight()+5);
rect.set(start, 0, Math.round(layout.getSecondaryHorizontal(end)), getLineHeight()+5);
rect.offset(Math.round(x), Math.round(y));
Log.d("debug","tabs " + rect.toString());
canvas.drawRect(rect, paint);
}
I have a span like this:
private SpannableStringBuilder spandex(List<String> ret, Boolean startDark) {
SpannableStringBuilder spanBuilder = new SpannableStringBuilder();
Spannable span = null;
int color = 0;
int startDarkMod = 0;
if(startDark)
startDarkMod = 1;
for (String x : ret) {
if ((ret.indexOf(x) + startDarkMod) % 2 > 0)
color = WzTheme.NOT_HIGHLIGHTED_COLOR;
else
color = WzTheme.ALT_NOT_HIGHLIGHTED_COLOR;
span = new SpannableString(x.toUpperCase());
span.setSpan(new ForegroundColorSpan(color),
0,
x.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanBuilder.append(span);
}
return spanBuilder;
}
public static int NOT_HIGHLIGHTED_COLOR = Color.rgb(68, 68, 68);
public static int ALT_NOT_HIGHLIGHTED_COLOR = Color.rgb(110, 110, 110);
It produces light gray and dark grey code like this:
Then there is that bright white "T". It happens after every ß character, which, as the other thread mentioned, gets converted to SS when capitalized. I would prefer no capatalization of that char, but I can live with the conversion to SS. What I need to stop from happening is the big white "T". Any ideas?
ugh, this fixes it:
private String myUpperCase(String word) {
word = word.replaceAll("ß", "XXX");
return word.toUpperCase().replaceAll("XXX", "ß");
}
so as you probably already know, the issue is that ß gets expanded to SS in java utf-8 toUpperCase() and that increases the length of the string in the span by one, but "x" (in my code above) is one char short (length) so i get the default white text.
i am happy with the fix.
I have a TextView with an OnTouchListener. What I want is the character index the user is pointing to when I get the MotionEvent. Is there any way to get to the underlying font metrics of the TextView?
Have you tried something like this:
Layout layout = this.getLayout();
if (layout != null)
{
int line = layout.getLineForVertical(y);
int offset = layout.getOffsetForHorizontal(line, x);
// At this point, "offset" should be what you want - the character index
}
Hope this helps...
I am not aware of a simple direct way to do this but you should be able to put something together using the Paint object of the TextView via a call to TextView.getPaint()
Once you have the paint object you will have access to the underlying FontMetrices via a call to Paint.getFontMetrics() and have access to other functions like Paint.measureText() Paint.getTextBounds(), and Paint.getTextWidths() for accessing the actual size of the displayed text.
While it generally works I had a few problems with the answer from Tony Blues.
Firstly getOffsetForHorizontal returns an offset even if the x coordinate is way beyond the last character of the line.
Secondly the returned character offset sometimes belongs to the next character, not the character directly underneath the pointer. Apparently the method returns the offset of the nearest cursor position. This may be to the left or to the right of the character depending on what's closer by.
My solution uses getPrimaryHorizontal instead to determine the cursor position of a certain offset and uses binary search to find the offset underneath the pointer's x coordinate.
public static int getCharacterOffset(TextView textView, int x, int y) {
x += textView.getScrollX() - textView.getTotalPaddingLeft();
y += textView.getScrollY() - textView.getTotalPaddingTop();
final Layout layout = textView.getLayout();
final int lineCount = layout.getLineCount();
if (lineCount == 0 || y < layout.getLineTop(0) || y >= layout.getLineBottom(lineCount - 1))
return -1;
final int line = layout.getLineForVertical(y);
if (x < layout.getLineLeft(line) || x >= layout.getLineRight(line))
return -1;
int start = layout.getLineStart(line);
int end = layout.getLineEnd(line);
while (end > start + 1) {
int middle = start + (end - start) / 2;
if (x >= layout.getPrimaryHorizontal(middle)) {
start = middle;
}
else {
end = middle;
}
}
return start;
}
Edit: This updated version works better with unnatural line breaks, when a long word does not fit in a line and gets split somewhere in the middle.
Caveats: In hyphenated texts, clicking on the hyphen at the end of a line return the index of the character next to it. Also this method does not work well with RTL texts.
I'm developing an android application and i need to know the number of characters that could be shown in one line to determine the number of lines of my string with some method .
How could do this ?
tanks a lot .
This should do the job for you (give or take a few mistakes from coding without an ide :-/ )
int countLineBreaks(final TextView view, final String toMeasure) {
final Paint paint = textView.getPaint(); // Get the paint used by the TextView
int startPos = 0;
int breakCount = 0;
final int endPos = toMeasure.length();
// Loop through the string, moving along the number of characters that will
// fit on a line in the TextView. The number of iterations = the number of line breaks
while (startPos < endPos) {
startPos += paint.breakText(toMeasure.substring(startPos, endPos),
true, tv.getWidth(),(float[]) null);
lineCount++;
}
// Line count will now equal the number of line-breaks the string will require
return lineCount;
}
..you get the idea ;-)
Every character has its own width.
As any String which contains 10 characters is not same to another String which also contains 10 char.
u can set it by --> textView.setTypeface(Typeface.MONOSPACE);
Monospace typeface works for making will regular char equal width.
Then u can do any stuff to get that how many characters could be placed in one line?
You can try this
set the Typeface.MONOSPACE as geet suggested. then do the following
Paint.measureText(String s) returns the width of the String s. By using this you can get the required width of 1 character. And from the View.getwidth() method you can get the width of the view. So from these two values you can calculate
try this:
int totalLine = textView.getMeasuredHeight() / textView.getLineHeight();
So I have a TextView in android that has the width of the whole length of the screen and a padding of dip 5. How can I calculate the number of characters that will fit a single line on the screen? I guess in other words, I'm trying to get the number of columns of a textview?
I considered manual calculation depending on textsize and width, but 1) don't know the correlation and 2) due to the padding in the units of dip, different screens will use different number of actual pixels to pad.
Overall Question: I am trying to use this to solve: if given a string how can I manually edit to string such that when the textview prints the string character by character, I will know when to start a word that won't fit on one line on the next. Note: I know that textview automatically puts words that won't fit onto the next line, however, since I'm printing character by character, like typing animation, textview doesn't know the word won't fit until it prints out the overflowing characters of that word.
Been searching everywhere for this...
Thanks!
Added solutions:
one possible solution:
public String measure2 (TextView t, String s) {
String u = "";
int start = 0;
int end = 1;
int space = 0;
boolean ellipsized = false;
float fwidth = t.getMeasuredWidth();
for(;;) {
//t.setText(s.substring(start, end));
float twidth = t.getPaint().measureText(s.substring(start, end));
if (twidth < fwidth){
if (end < s.length())
end++;
else {
if (!ellipsized)
return s;
return u + s.subSequence(start, end);
}
}
else {
ellipsized = true;
space = (u + s.substring(start, end)).lastIndexOf(" ");
if (space == -1)
space = end - 1;
u += s.subSequence(start, space) + "\n";
start = space + 1;
end = start + 1;
}
}
}
solution 2, but still uses solution1 sometimes:
public String measure3 (TextView t, String s) {
List<String> wlist = Arrays.asList(s.split(" "));
if (wlist.size() == 1)
return measure2(t, s);
String u = "";
int end = 1;
float fwidth = t.getMeasuredWidth();
for(;;) {
//t.setText(s.substring(start, end));
if (wlist.isEmpty())
return u;
String temp = listStr(wlist, end);
float twidth = t.getPaint().measureText(temp);
if (twidth < fwidth){
if (end < wlist.size())
end++;
else {
return u + temp;
}
}
else {
temp = listStr(wlist, end-1);
if (end == 1)
temp = measure2(t, temp);
if (wlist.isEmpty())
return u + temp;
else
u = u + temp + "\n";
wlist = wlist.subList(end - 1, wlist.size());
end = 1;
}
}
}
public String listStr (List<String> arr, int end) {
String s = "";
for (String e : arr.subList(0, end) ){
s = s + e + " ";
}
return s.trim();
}
I used the above code to generate off a original string s, a string u that would be printed. However, I think this approach is very inefficient. Is there another approach or a better algorithm? Note: there are some errors in measure3 that I fixed, but was too lazy to edit
Try this:
private boolean isTooLarge (TextView text, String newText) {
float textWidth = text.getPaint().measureText(newText);
return (textWidth >= text.getMeasuredWidth ());
}
Detecting how many characters fit will be impossible due to the variable width of the characters. The above function will test if a particular string will fit or not in the TextView. The content of newText should be all the characters in a particular line. If true, then start a new line (and using a new string to pass as parameter).
Answer to the comment:
because the app can be run in many systems is exactly why you need to measure it.
This is a way to solve your "overall question". What is the difference between using str.size()>numCol vs is too large? You will need to implement your animation (hint #1: insert a newline character)
as I said before when you start a new line, you start a new string (hint #2: if you extend TextView, you can implement all this in overriding setText). (hint #3: Keep track of the lines created with a static int lines; and use newString.split("\\r?\\n")[lines-1] to check for length).
You can get total line of Textview and get string for each characters by below code.Then you can set style to each line whichever you want.
I set first line bold.
private void setLayoutListner( final TextView textView ) {
textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
final Layout layout = textView.getLayout();
// Loop over all the lines and do whatever you need with
// the width of the line
for (int i = 0; i < layout.getLineCount(); i++) {
int end = layout.getLineEnd(0);
SpannableString content = new SpannableString( textView.getText().toString() );
content.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, end, 0);
content.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), end, content.length(), 0);
textView.setText( content );
}
}
});
}
Try this way.You can apply multiple style this way.
I had the same issue and I calculated the number characters per line by 2 steps:
Step 1: Calculate the number of lines
val widthOfTvComment = widthOfScreen - marginLeft - marginRight
var bounds = Rect()
var paint = Paint()
paint.textSize = textSize
paint.getTextBounds(comment,0,comment.length,bounds)
val lines = ( bounds.width()/widthOfTvComment)
Step 2: Calculated the number characters per line
val charactersPerLine = comment.length / lines