Reputation: 1108
I'm working on an Android app, and I need to be able to include the exponents/powers into a string (Java). I've done some searching and it seems that my scenario is making it difficult. I've looked into options such as ALT+ codes and Unicode. I'm not entirely sure if either of those, or any other options, are actually possible (I know the ALT+ codes only have '1' '2' and '3' subscripts oddly enough).
So, what's my best option, if any?
I'd like to keep it as simple as possible. I have a lot of strings to write (like math problems) and the need to subscript/superscript numbers is random. Can this be done easily?
EDIT: I suppose I was vague. My app is for studying. And the questions/answers are put into arrays and randomized each time. I'd like to be able to set the strings in the Java file as they are now
question[0].text = "solve x^2 if x=5";
But instead of the user seeing "^2", I'd like to use the actual superscripts.
ANOTHER EDIT:
global.java (class/structure that holds questions/answers)
question[0].question = "Question Text. I would like to add a superscript or two here, as if it were a math problem";
question[0].answer[0]. = "Answer 1. Add a subscript here";
question[0].answer[1]. = "Answer 2. Add another superscript here.";
question[0].answer[2]. = "Answer 3. Etc.";
question[0].answer[3]. = "Answer 4. Etc.";
Activity XML
<!--QUESTION-->
<TextView
android:id="@+id/textQuestion"/>
<!--ANSWERS-->
<Button
android:id="@+id/answerOne"
android:onClick="checkAnswer"/>
<Button
android:id="@+id/answerTwo"
android:onClick="checkAnswer"/>
<Button
android:id="@+id/answerThree"
android:onClick="checkAnswer"/>
<Button
android:id="@+id/answerFour"
android:onClick="checkAnswer"/>
Activity Java
//SET QUESTION STRING TO TEXTVIEW
textQuestion.setText(questionDatabase.getQuestion(index));
//SET ANSWER STRINGS TO BUTTONS' TEXT
buttonOne.setText(questionDatabase.getAnswer(index, 0));
buttonTwo.setText(questionDatabase.getAnswer(index, 1));
buttonThree.setText(questionDatabase.getAnswer(index, 2));
buttonFour.setText(questionDatabase.getAnswer(index, 3));
"NEXT" and "PREVIOUS" buttons allow the user to move through the questions by incrementing/decrementing the "index".
So, because of this, the activity never knows (I don't think) which questions/answers need text to be superscript, and why I'd like to be able to set the questions/answers sub/superscript myself when I enter the questions/answers into the array. Can this be done?
EDIT: @jared burrows was very helpful in chat and has helped me almost reach the end of my issue. I'm now able to use setText(Html.fromHtml()); on my textview (question text) and it shows the subscript and superscript properly. But on my buttons (answers) I tried to employ the same tactic and it doesn't work the same. "X2" just becomes "X2". Why is this? Why don't my buttons accept the html just like the textview.
Upvotes: 0
Views: 6589
Reputation: 1108
The answer to this was a combination of help from @Jared Burrows and AlanV. So thank you to you both.
view.setText(Html.fromHtml(stringGoesHere));
This does work, but out of the box it doesn't work for buttons due to some frustrating feature in (I think) Android 5.0. Buttons are automatically set to all caps, and because of this, it overrides the html formatting.
So, to allow buttons the ability to use html properly, you must set the attribute to false.
<button
android:textAllCaps="false";/>
Thanks again to Jared Burrows and AlanV.
Upvotes: 1
Reputation: 55527
Superscript:
textView.setText(Html.fromHtml("X<sup>2</sup>"));
Subscript:
textView.setText(Html.fromHtml("X<sub>2</sub>"));
References:
Subscript and Superscript a String in Android
http://www.w3schools.com/tags/tag_sub.asp
http://www.w3schools.com/tags/tag_sup.asp
Upvotes: 1