TextView Superscript not showing correctly - android

I'm using the code found: Subscript and Superscript a String in Android in the first answer, but concatenating that from a previous string, so my code looks similar to:
TextView text = (TextView) findViewById(R.id.text);
text.setText(text.getText().toString()+Html.fromHtml("<sup>-2</sup>"));
Say, the contents of text was "3x", after setting the text using setText, it formats to "3x-2" with no subscript.
The XML for the TextView is:
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="55dp"
android:layout_marginTop="210dp"
android:text="3x"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="14pt"
android:visibility="VISIBLE"
/>
Thanks for helping.

Instead of this line..
text.setText(text.getText().toString()+Html.fromHtml("<sup>-2</sup>"));
try like this
text.setText(Html.fromHtml(text.getText().toString()+"<sup>-2</sup>"));

Please check the below its working fine for me
"TEXT<sup><small>TM</small></sup> TEXT"
The above code will make the TM to Subscript and also make the text size small

Also for small size you can use-
text.setText(Html.fromHtml(text.getText().toString()+"<sup><small>-2</small></sup>"));
Put your whole text into only one Html.fromHtml("Your whole string")
From strings.xml
<string name="superscript_str">YourString<sup><small>SuperScriptString</small></sup></string>

If you want display small as super text use
text.setText(Html.fromHtml(text.getText().toString()+"<sup>small>-2</small>/sup>"));

Related

TextView Bold the second line

I don't know if that's possibole or not, but i'm trying to achieve some thing like this :
Second line is bold, what i already done :
<TextView
android:id="#+id/checkIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/text"
android:layout_below="#+id/text"
android:layout_marginLeft="5dp"
android:padding="2dp"
android:text="Check-in Date \n 10-08-2017"
android:drawableLeft="#drawable/ic_calendar"
android:textColor="#color/hintColor"
android:drawablePadding="2dp"
android:transitionName="price"
android:textSize="#dimen/textInfoSmall" />
Simple way is to Use two TextViews
Or try SpannableString as explained here
How to make part of the text Bold in android at runtime?
You should either use two textviews or you can use
Textview tvcheckIn = (TextView) findViewById(R.id.checkIn);
tvcheckIn.setText( Html.fromHtml("First Line<br><b>Second Line"));
I think the better practice is separate it into two TextView wrapped inside an Layout.
Because the texts "Check-in Date" and "Check-out Date" is always static. So use separate TextView will reduce String concatenate (Eg: textView.setText("Check-in Date \n" + checkInDate);), and make the source easier to read and extend.
If you still want to highlight part of text in TextView, there is already an answered question here: Android - Highlight a Word In a TextView?.
This is all about SpannableString.

Set different text size in Android TextView

I have one TextView with only 1 line of text. Now I have to put an extra line to set the state of the user. So I set the property android:lines="2"
the textview looks like:
<TextView
android:id="#+id/providerName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:ellipsize="marquee"
android:fontFamily="sans-serif-light"
android:lines="2"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
The way I add the text is like:
mNameAndStatus.setText(providerNameText + "\n" + mUserStatus);
Image how is actually.
The problem is that the first lines must me in Large text, but the second line must be medium or small and I don't know if is it possible.
How can I achieve this?
try
String asHtml = firstLine+"<br/><small>"+secondLine+"</small>";
textView.setText(Html.fromHtml(asHtml));
or simply use Spannable (AbsoluteSizeSpan)
You need to do something like this :
mNameAndStatus.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
where your text size is given in sp (24sp in the example).
Here are all TypedValues http://developer.android.com/reference/android/util/TypedValue.html

Text View Text analysis in Android

I have a textview and the text is loaded dynamically.
<TextView
android:id="#+id/txt_song_title"
style="?attr/txtcolorHightlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignTop="#id/frlyt_img_cover"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_toLeftOf="#id/img_audio_source_icon"
android:ellipsize="end"
android:fontFamily="#string/roboto_light"
android:maxLines="3"
android:text=""
android:textSize="#dimen/font_36" />
The textview has to show the entire text which is set (10 to 100+)characters. Based on text length, the font size should be changed inorder to fit the entire text. How to calculate num of words in the textview to change font size and display entire text without clipping dynamically?
I think you can do it from java code level. TextView class provides methods to implement this.
Following link may be useful.
Auto Scale TextView Text to Fit within Bounds
Try using a library, for example a simple google search gave me this:
https://github.com/grantland/android-autofittextview
I have not tried it but it seems ok :)

Align a sentance in a TextView of two lines

I have a textview which get text from server and that text is a full paragraph like and has a html format. Now i am using maxline=2 in xml to show some of the text in two lines. I have tried many tricks but nothing works. Here is my xml code, please tell me what going wrong with it?
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/subject"
android:layout_toLeftOf="#+id/date"
android:maxLines="2"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:textColor="#666666"
android:textAppearance="?android:attr/textAppearanceMedium" />
And this is how i set text into that textview.
message.setText(Html.fromHtml(details.Description()));
It always looks like this,
This is the text
that came from server
As you can notice above where the first line start from the second on not starting from there below. I want both to start from same position. The problem is text coming from server is a single sentence and i can't use "\n" or </br> like tags and also this text is in the listview setting by getview. And i have also tried android:gravity="center_horizontal" and android:layout_gravity="center_horizontal"

android append '…' at the end textview _EDIT

I have a lsit view and i that i need to add some text.
in the adapter, I need to append ... at the end and i used the following
when i give
android:ellipsize="end"
in the only shows 2 line
eg:
asdnfsdfdsf asdfsdfasdf
sdfsd sdfsdf sdfsd ad...
And when i give
android:ellipsize="start"
it is as :
asdnfsdfdsf asdfsdfasdfd
...sdfsd sdfsdf sdfsd ad
The complete code i used:
<TextView android:layout_width="wrap_content" android:background="#016A7D"
android:layout_height="80dp" android:textColor="#000000"
android:maxLines="4"
android:id="#+id/list_report_desciption"
android:ellipsize="end"
/>
And the output i got is:
What exactly will a smarter planet
look like? How's IT changing? A...
actually it contains more than 6 lines
Is there any thing i need to set so that i need to get 3 lines
Thanks
Text ellipsization seems buggy, accroding to this report:
http://code.google.com/p/android/issues/detail?id=10554
A workaround allows ellipsizing one line of text, by setting the android:singleLine attribute to true. But this attribute is deprecated.
Nothing worked for me:-(
I just fixed the length. Cut the string in that length and append '...' after that.
Now its working for me.
I was looking for a simple solution for this myself, I ended up with something like:
private String getTextWithMoreIndicator(TextView textView, String text) {
long maxVisibleChars = textView.getPaint().breakText(text, true, textView.getMeasuredWidth(), null);
if(maxVisibleChars < text.length()) {
text = text.substring(0, (int)maxVisibleChars -3) + "...";
}
return text;
}
The solution you accepted will not scale correctly on all devices.
Reason : the width of "a" is lesser than "A", so, if you are truncating string based on some size (say 15 letters) and adding "..." through code. You might see results not expected.
Now coming to the solution:-
Solutions:
Solution # 1 :
Add the following 3 attributes to your TextView and you'll get the result you want :)
<TextView
android:ellipsize="end"
android:lines="1"
android:scrollHorizontally="true" />
Solution # 2 :
Another workaround might be, you could decide to marquee text (the fancy animation moving your text from right to left). For that you need following attributes in your TextView xml:-
<TextView
android:id="#+id/my_text_view"
android:ellipsize="marquee"
android:lines="1"
android:scrollHorizontally="true"
android:marqueeRepeatLimit="marquee_forever" />
And then in you code you need to get the TextView by its id and put-in the following line:-
TextView myTextView = (TextView) findViewById(R.id.my_text_view);
myTextView.setSelected(true);
#### Edit:- ####
I just figured out that for my solution to work in android versions greater than 2.3.x we need to add the following line in our TextView xml :-
android:singleLine="true"
Although its a deprecated attribute, but you have to add this, otherwise marquee or "..." wont work.
Hope this answers the question :)

Categories

Resources