Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create superscript for Android Button text with Html.fromHtml()

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.

enter image description here

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?

like image 506
idclark Avatar asked Dec 05 '22 04:12

idclark


1 Answers

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! :-)

like image 134
lilgodwin Avatar answered Apr 28 '23 23:04

lilgodwin