0

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.

jww
  • 97,681
  • 90
  • 411
  • 885
lilgodwin
  • 1,098
  • 3
  • 13
  • 26

2 Answers2

1

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

Community
  • 1
  • 1
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • I've seen that solution before, but that's where my situation makes it more difficult. I don't know if you saw my edit/update. I have variables set (see above for a simple example) and I'd like to be able to set those strings as such, with the superscript in place. The app is set up for practice questions and each page pulls a question string and the 4 possible answers placed in the buttons. With your solution, it only seems possible if I wasn't using another class/structure to hold the variables/questions/answers. – lilgodwin Mar 23 '15 at 03:29
  • I've edited, once again, the main question. This time I provided some hopefully better information and code to help you to understand my issue. – lilgodwin Mar 23 '15 at 03:40
  • I dont understand why you can't add your string dynamically to `Html.fromHtml`? – Jared Burrows Mar 23 '15 at 03:42
  • Maybe I don't quite understand. Did you see my most recent update? Does this help explain my situation better? How might I go about doing so with my particular setup? – lilgodwin Mar 23 '15 at 03:43
  • Yes. If you are statically storing them in `global.java`, then why not: `question[0].answer[3] ="X2"`? – Jared Burrows Mar 23 '15 at 03:46
  • I tried that and when it displayed in the app it displayed the text just like it was written. `questions[4].q = "x2";` displays x2 – lilgodwin Mar 23 '15 at 03:48
  • Ah, should have just said. What about http://stackoverflow.com/a/15387283/950427 or http://stackoverflow.com/a/8872986/950427? – Jared Burrows Mar 23 '15 at 03:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73548/discussion-between-lilgodwin-and-jared-burrows). – lilgodwin Mar 23 '15 at 03:55
  • Yes I did. Thanks again for your help. I posted an answer, giving you credit of course. I was able to get the last bit of help elsewhere. – lilgodwin Mar 23 '15 at 19:44
1

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.

lilgodwin
  • 1,098
  • 3
  • 13
  • 26
  • 1
    I would if it completely solved the issue, but someone else experiencing the same thing would also want the full answer. Though, I definitely give him credit, along with AlanV who helped me elsewhere. – lilgodwin Mar 25 '15 at 06:42