I´m writing a calculator for chemistry in Android Studio and I have no idea about how to write the index of the elements in the given form:
H2O
With the index in the bottom left.
How can I achieve this?
private static CharSequence makeStringLikeFormula(String str) {
if (str == null) return "";
final SpannableString spannable = new SpannableString(str);
final Matcher matcher = Pattern.compile("\\d+").matcher(str);
while (matcher.find()) {
spannable.setSpan(new SubscriptSpan(), matcher.start(), matcher.end(), 0);
}
return spannable;
}
example
textView.setText(makeStringLikeFormula("H2O22"));
You will have to use SubscriptSpan. From official android documentation :
https://developer.android.com/reference/android/text/style/SubscriptSpan
You might also want to take a look on this answer Subscript and Superscript a String in Android
Basically, since you need both subscript and superscript, a way to do this is through HTML, and you can add HTML on android.
TextView allows you to insert HTML.
The simplest way is to use html- Do like this:-
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("H<sub>2</sub>O"));
Related
I have this code
TextView text1 = (TextView) view.findViewById(R.layout.myLayout);
Spanned myBold = (Html.fromHtml("<b>Test<b>", Html.FROM_HTML_MODE_LEGACY));
If I do
text1.setText(myBold);
Then myBold is in bold,which is ok. But when I want to add a string more, like
text1.setText(myBold+"bla");
Then the whole TextView is not bold anymore. Why does the new String "bla" affect this?
Thanks.
Why does the new String "bla" affect this?
Because what you are really doing is:
text1.setText(myBold.toString() + "bla");
A String has no style information. A Spanned object does.
Use TextUtils.concat() instead:
text1.setText(TextUtils.concat(myBold, "bla"));
A better choice would be to use a Bold StyleSpan. In the next sample only the "hello" world will be set to bold by using such technique:
Java:
final SpannableString caption = new SpannableString("hello world");
// Set to bold from index 0 to the length of 'hello'
caption.setSpan(new StyleSpan(Typeface.BOLD), 0, "hello".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
yourTextView.setText(caption);
Kotlin:
yourTextView.text = SpannableString("hello world").apply {
// Set to bold from index 0 to the length of 'hello'
setSpan(StyleSpan(Typeface.BOLD), 0, "hello".length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE))
}
This would be a more optimal solution rather than using the Html.fromHtml technicque, as it doesn't have to go through the overhead of parsing/interpreting the HTML tags.
In addition, it allows you to combine more styles, sizes, etc, in the same SpannableString.
I have a string name "classe". It contains many words and phrases in it. But I want to change color when string contains the word "idiom". I did that programmatically, but it changed the color of all string "classe". I just want to change the color of "idiom" (a part of word, not the whole phrase). How can I do that programmatically?
if (classe.contains("idiom")) {
txtclasse.setTextColor(getResources().getColor(R.color.orange));
}
Use ForegroundColorSpan.
if(classe != null && classe.contains(“idiom”))
{
Spannable spannable = new SpannableString(classe);
spannable.setSpan(new ForegroundColorSpan(Color.BLUE), classe.indexOf(“idiom”), classe.indexOf(“idiom”) + “idiom”.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txtclasse.setText(spannable);
}
You could use HTML to solve it.
origString = txtclasse.getText().toString();
origString = origString.replaceAll("idiom","<font color='red'>idiom</font>");
txtclasse.setText(Html.fromHtml(origString));
I am trying to create a function that will take a string "word" as an argument, apply span styling to the string and return it as a spannable string that I can ""+insert+"" into a string in a TextView. Thank you in advance for any help you can offer with this.
Here is the function I have so far.
public Spannable fr(String word){
Spannable WordtoSpan = new SpannableString(word);
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0,
word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
WordtoSpan.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0,
word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return WordtoSpan;
}
and the code that should display the spannable inline with my text.
TextView tview = (TextView) findViewById(R.id.accents_view);
tview.setText(Html.fromHtml("<strong>Text String</strong> "+fr("style_this"));
The application will either load the text without styling or the app will crash. Not sure what I'm doing wrong but I'm guessing it has something to do with mixing fromHtml text with spannables. Any work arounds?
If I remove the Html.fromHtml() and wrap the entire text with my fr() function it works. I get bold blue text. But I need to be able to make only certain words styled... It would also be nice to be able to reference vars from my string.xml file instead of hard coding them in, for multilingual support.
I think you may find this helpful,
TextView tview = (TextView) findViewById(R.id.accents_view);
tview.setText(Html.fromHtml("<font color='blue'>Text</font> "
+ "<font color='red'>String</font>"));
This may be a blunder,But I need to know... :)
Iam develeoping an android application,In in I want to display two type face in a single textview and found this One very useful,A custom typeface span that extends typeface span,
As per my understanding , we are creating a spannable as follows using two words
String firstWord = "first ";
String secondWord = "second";
// Create a new spannable with the two strings
Spannable spannable = new SpannableString(firstWord+secondWord);
then sets two type face to that words using our custom span like this
// Set the custom typeface to span over a section of the spannable object
spannable.setSpan( new CustomTypefaceSpan("sans-serif",CUSTOM_TYPEFACE), 0,
firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan( new CustomTypefaceSpan("sans-serif",SECOND_CUSTOM_TYPEFACE), rstWord.length(), secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Now my question, I have a large amount of text so If i am able to put the spans directly in my text its will be a more easy for us ,is that possible to put spans directly to the text like we put <b>bold </b> as my need is to use my custom typeface span like <typeface1> sample </typeface1> <typeface2> Doc </typeface2>.
WHAT I NEED IS, DIRECTLY APPLY THE CHANGE TO TEXT WHAT THE SET SPAN DO TO THE TEXT
Is that possible, if possible what i need to write as span in my text,How can I achieve this.. or am i compleatly wrong ?
Thank you for all
Very late answer but what you asked for is pretty easy.
For instance, lets say we want to change everything between quotation marks into italic font.
String s = "\"Hello my name is Edward\" Hola, my nombre es Edward";
Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher(text);
Spannable spannable = new SpannableString(s);
while (m.find()) {
int textLocation = text.indexOf(m.group(1));
// Set the custom typeface to span over a section of the spannable object
spannable.setSpan( new CustomTypefaceSpan("sans-serif",my_italic_font),
textLocation-1, textLocation + m.group(1).length(),spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Set the text of a textView with the spannable object
TextView textbox = (TextView) v.findViewById(R.id.cardtextbox);
}
textbox.setText( spannable );
Result of s would be
"Hello my name is Edward" Hola, my nombre es Edward
Now instead of quotation marks you would use a regex pattern for tags like <b>bold </b>;
And you could also remove the tags afterwards with a simple .replace();
I'm using a SpannableString to underline certain words, however, I realized the code I have only highlights the first word if there are multiple words. Not exactly sure how to accomplish highlighting multiple words:
String keyword = "test";
String text = "This is a test to underline the three test words in this test";
SpannableString output = new SpannableString(text);
if (text.indexOf(keyword) > -1)
{
int keywordIndex = text.indexOf(keyword);
int keywordLength = keyword.length();
int start = keywordIndex;
int end = keywordIndex + (keywordLength);
output.setSpan(new UnderlineSpan(), start, end, 0);
}
I was thinking I could split the text at every space and loop through it, but wasn't sure if there was a better way.
I do have this code to highlight multiple words using a regular expression, however, I'm try to avoid regular expressions since it's in an Android app and I'm using it in a ListView and I'm told they are very expensive. Also this code I have only highlight whole words, so using the example text above, if the word "protest" was in the sentence, it wouldn't get highlighted using this code:
Matcher matcher = Pattern.compile("\\b(?:test")\\b").matcher(text);
while (matcher.find())
{
output.setSpan(new UnderlineSpan(), matcher.start(), matcher.end(), 0);
}