I've read through both of these questions, which seem to have the same problem i'm struggling with, namely I have text for my buttons that i'd like to display as a superscript. In my fragment class in the onCreateView
method I have added
if (rootView.findViewById(R.id.squared) != null) {
((TextView) rootView.findViewById(R.id.squared)).setText(Html.fromHtml("X <sup><small> 2 </small></sup>"));
rootView.findViewById(R.id.squared).setOnClickListener(this);
}
if (rootView.findViewById(R.id.cubed) != null) {
((TextView) rootView.findViewById(R.id.cubed)).setText(Html.fromHtml("X <sup><small> 3 </small></sup>"));
rootView.findViewById(R.id.cubed).setOnClickListener(this);
In my fragment_main
the buttons are coded as
<Button
android:layout_height="match_parent"
android:layout_width="0dp"
android:fontFamily="helvetica"
android:id="@+id/squared"
android:layout_weight="0.25"
style="?android:attr/borderlessButtonStyle"
android:textSize="15sp" />
<Button
android:layout_height="match_parent"
android:layout_width="0dp"
android:fontFamily="helvetica"
android:id="@+id/cubed"
android:layout_weight="0.25"
style="?android:attr/borderlessButtonStyle"
android:textSize="15sp" />
However, when I run my application the layout doesn't any superscripted text.
Even when I change the text size from 15sp
to 10sp
, the text only gets smaller, but not superscripted. What am I not doing correctly?
I'd comment if I could, but at this point I can't. I JUST posted something similar to this, an issue I'm having as well. I had my problem solved halfway. The method Html.fromHtml works for me only on a textview, but not on my butons. I just wanted to say this in case it helps you or anyone else figure out how to employ this method on the buttons. I'd very much like the answer myself.
Basically:
textview.setText(Html.fromHtml("x<sup>2</sup"));
displays properly
BUT button.setText(Html.fromHtml("x<sup>2</sup"));
displays x2 (without superscript).
EDIT: AlanV helped me out. Here's a link to the solution he provided someone else. https://stackoverflow.com/a/29056795/4535064
All you need to do is add this to your button:
<button
android:textAllCaps="false";/>
The new version 5.0 (I think) forces buttons to produce all caps. This is something I noticed when I produce the same string in a textview and button, and the button was all caps whereas the textview was not. Because of this, you don't see the HTML formatting such as and . Setting the textAllCaps feature to "false" allows the HTML to be utilized.
Again, giving credit where it's due, AlanV is the man! :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With