How can I find how many characters are there in each word of a TextView in Android. I want to set few words of my TexView to bold type. So for instance if the text view has a text "Happy Coding, Fellow Coders". I would like to set "Coding" & "Coders" to bold type.
Try this solution. It contains counting words based on the comma, colon or semicolon and dot characters :
String str = "Happy Coding, Fellow Coders";
String[] strArr = str.split(",| |.");
Log.e("TAG", "split String: " + strArr);
If you want to split only from whitespaces, then use :
.split("\\s+") instead of .split(" ")
Then you can simply use the loops to count the characters of every words.
Related
In my string.xml I have a text which contains 2 paragraphs "\n\n". When I try to split it by String.split("\n\n")
or String.split("\\n\\n") it does not work. Knows anywhone the reason?
and here it is the sample data:
it displays well on the screen
val content = resources.getString(R.string.test).split("\n \n ")
strings.xml
<string name="test">first
\n
\n
second</string>
You should add gaps after \n. Use my pattern for spliting and it'll work.
Your text appears to actually have literal \n values in it. In that case, you may try splitting on \\n (which becomes \\\\n in Java syntax):
String yourText = "Part one \\n Part two";
String[] parts = yourText.split("\\\\n");
System.out.println(Arrays.toString(parts));
This prints:
[Part one , Part two]
I have an issue when mixing in one string English, Hebrew and digits.
The order of digits next to Hebrew is getting reversed, no matter what order I make - fist digit and then text, of first text and then Hebrew - it's getting reversed to: on the left digit, on the right text.
My text example is:
String leftPart = "10 gr";
int numder = 8;
String hebrewText = "כפות";
String rightPart = hebrewText + " " + number;
String finalString = leftPart + " · " + rightPart; //10 gr · כפות 8
I want to display the digit 8 in the end of this string, after the Hebrew word, not before it, but I'm unable to do it even here...it's getting reversed because of the English text in the begging.
Even if I change the order to:
String rightPart = number + " " + hebrewText ;
the result is the same...
Any ideas? It's looks like something simple that I'm missing
A tip for forcing English to be shown nicely when mixed with Hebrew:
Wrap the English (or numbers) words with LRI and PDI (check here: https://unicode.org/reports/tr9/ ) .
For example, instead of these (first word is in English) :
<string name="test">ABC היא האפליקציה הכי טובה</string>
<string name="test2">%1$s היא האפליקציה הכי טובה</string>
Use these:
<string name="test">\u2066ABC\u2069 היא האפליקציה הכי טובה</string>
<string name="test2">\u2066%1$s\u2069 היא האפליקציה הכי טובה</string>
Other useful ones can be found here:
https://stackoverflow.com/a/10989502/878126
Nothing is screwing up here, this is actually correct behavior. The number is coming after the end of the hebrew word- the end of the hebrew word is on the left. What you seem to want is for the number to come before the hebrew word. But when you combine it with english like that it doesn't know tht the number is supposed to be bound to the hebrew part and not the english part, so putting it before the hebrew doesn't work either.
I'd suggest putting the number before the hebrew part and wrapping the number and hebrew text in unicode right to left mark characters, to tell it explicitly the 8 is part of the right to left text.
Alternatively you could put the number after the hebrew text but use an rtl mark before the hebrew and a ltr mark after. Which is probably a slightly better way of doing things overall if you want more complex embedding elsewhere.
I have a sentence,
hello, What you are doing?How are you?
I wanted to split the sentence with characters such as .,?
I have achieved it using split function
and the output is:
arr[0]=hello
arr[1]=What you are doing
arr[2]=How are you
but I want the array as
arr[0]=hello
arr[1]=,
arr[2]=What you are doing
arr[3]=?
arr[4]=How are you
arr[5]=?
I have done this code
String text = "hello, What you are doing?How are you?";
String[] arr= text.split("[\\.,!;?:\"]+");
for (String str : arr) {
System.out.println(str);
}
Please help.
The general strategy to preserve text while also splitting on it at the same time is to use lookarounds. Lookarounds assert logic, but do not actually consume any text. They are basically zero width. I split below using the pattern:
(?=[\\.,!;?:\"])|(?<=[\\.,!;?:\"])
This says to split if we lookahead or lookbehind and see a punctuation character.
String sentence = "hello, What you are doing?How are you?";
String[] parts = sentence.split("(?=[\\.,!;?:\"])|(?<=[\\.,!;?:\"])");
for (String part : parts) {
System.out.println(part);
}
hello
,
What you are doing
?
How are you
?
Demo
You might also want to run String#trim on each term to get the exact output you want.
how to bold a part of text Android String and in parallel use Message format?
I can do the part of the text bold only if the text is fixed,
but I want to use also in Message.format in order to set custom text.
something like that:
hello <b>{0}</b>, my name is <b>{1}</b>, my age is <b>{2}</b>....
hello world, my name is Josh, my age is seven
try Html.fromHtml() function on text like below
textView.setText("hello"+Html.fromHtml("<b>{0}</b>")+", my name is"+Html.fromHtml("<b>{1}</b>"));
Just build your String in HTML and set it:
String array[]={"World","Josh","Seven"};
String sourceString = "hello <b>"+array[0]+"</b>, my name is <b>"+array[1]+"</b>, my age is <b>"+array[2]+"</b>";
mytextview.setText(Html.fromHtml(sourceString));
OP: hello world, my name is Josh, my age is seven
So basically by default the text view in android wraps contents because of which my text looks something like this
I'd like to disable the text wrapping property and set equal number of characters in the text view.
How do I do it?
Your question is not 100% clear but if you're talking about justification, Android doesn't support it. But here is a library which does.
If you literally don't want the text to wrap use:
android:singleline="true"
You have to use single line "true", and define the padding between the cells.
ps: to set a number of characters , you have get the refference from this textView and edit the content(just format the string).
Example:
char text[] = originalText.toCharArray();
String newText = "":
for(int i=0; i< text.length(); i++){
if(i<x){
newText =newText + text[i];
}
else{
break;
}
}
To achieve this, simply use the newline syntax "\n" where you want a new line to begin.
For example:
String text = "Who is the Boss? \n You are the Boss";
This can also be achieved programmatically of course in code if you're getting a string without one.
Simply write a method that checks for white space and insert the "\n" say after each successive 3 whitespaces have been detected. Then programmatically set the string to the TextView.
try adding this attribute to your textview and then try
android:gravity="center_horizontal