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
Related
I have a TextView with a height depending on previous content, but there might be a long Text in it. How can I cut it off at the right point and concatenate three dots or something similar to the new end?
Currently it looks like this:
I found some solutions for Single Line Text, but how does it work with more than one line? I also do not know the number of lines, because this depends on the screen size.
Are there other typical ways on Android to show that the text can be extended? E.g. a colour gradient in the last line?
Edit:
When I do it without a fixed heigth, I have to make the height depend on the element above and my XML will look like this:
<TextView
android:id="#+id/podcastShortDesc"
android:text="Long text"
android:layout_below="#+id/podcastTitle"
android:layout_toRightOf="#+id/podcastLogo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:ellipsize="end"
android:layout_above="#+id/podcastMoreAction" />
When I do specify maxLines I can have luck an it will work:
But if the title is too big, it does not work:
You should add following code for "3 dots" at the end.
android:ellipsize="end"
You should remove fixed height property
android:layout_height="50dip"
instead you should add number of lines
android:maxLines="4"
android:layout_height="wrap_content"
Android will take care everything else. In this way, even if text is smaller than 4 lines, android will take care size. If it is more than 4 lines, it will add "3 dots" :) Specify fixed height may cut your text.
Try this. Hope it will work.
<TextView
android:id="#+id/podcastShortDesc"
android:text="LONG LONG LONG TEXT"
android:layout_width="match_parent"
android:layout_height="50dip"
android:maxHeight="50dp"
android:ellipsize="end"/>
I have tested 50dp can show two line in normal font size. So in 50dp height you should add maxLines 2.
Try this fix layout_height and add scroll in your textview
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="50dip"
android:text="Hello......"
android:scrollbars="vertical"/>
I'm trying to limit a TextView to 1 line, the length of the screen. I'm having trouble keeping it from expanding.
I've tried adding the
android:minLines="1"
android:maxLines="1"
attributes to the TextView xml. This does keep the view from expanding, but this does not limit the characters of the text. For example, if I have entered the maximum number of characters in a line and proceed to add a '(' to the string, I can see the top of it printed below the first line.
Basically, I need a way to limit the amount of characters to the width of the screen. I've tried this:
while(txt.getLineCount() > 1) {
txt.setText(txt.getText().subSequence(0,txt.getText().length()-1));
}
which doesn't work at all. I've also tried that same method with comparing the txt.getHeight() to the txt.getTextSize().
I'm out of ideas. What can I do?
If I use a textview this way it will ocupy only 1 line:
<TextView
android:id="#+id/textView"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Try this. This will limit textview to single line. , if the text was more than one line it will display ... at end.
<TextView
android:id="#+id/textView"
android:singleline="true"
android:ellipsize="end"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
In my app, there is a textview. The textview is populated with the data from the DB. The following is the XML of the textview
android:id="#+id/ViewMessageOne"
android:layout_width="275dp"
android:layout_height="45dp"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="14sp"
android:typeface="sans"
android:background="#drawable/back"
android:layout_marginLeft="35dp"
/>
The following is the lines of code to populate the data in the textview from DB
final TextView ViewMessageOne = (TextView) findViewById(R.id.ViewMessageOne);
ViewMessageOne.setText(Html.fromHtml(getMessages("MSG_ONE")+ "<br>" + "<small>" + getTimeStamp("MSG_ONE") + "</small>"));
When the data from DB is populated, the text size in the screen is small and is not taking the XML value. I checked this by increasing the size of the textview text. But it is not taking the text size from the XML. Can anyone guide me as how to get the value of the text size value from the XML and hence implement it using JAVA code. Thanks
you have follow line in your code:
android:textAppearance="?android:attr/textAppearanceMedium"
which probably overrides your custom textsize
remove that line and try again
Edit: to set text size programmatically:
viewMessageOne.setTextSize(16)
change this
android:layout_height="45dp"
to this
android:layout_height="wrap_content"
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>"));
What I have now is a ListView of TextView elements. each TextView element displays a text (the text length varies from 12 words to 100+). What I want is to make these TextViews display a portion of the text (let's say 20 word or roughly 170 chars).
How to limit the TextView to a fixed number of characters?
Here is an example. I limit the sizewith the maxLength attribute, limit it to a single line with maxLines attribute, then use the ellipsize=end to add a "..." automatically to the end of any line that has been cut-off.
<TextView
android:id="#+id/secondLineTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:maxLength="10"
android:ellipsize="end"/>
If your not interested in xml solutions maybe you can do this:
String s="Hello world";
Textview someTextView;
someTextView.setText(getSafeSubstring(s, 5));
//the text of someTextView will be Hello
...
public String getSafeSubstring(String s, int maxLength){
if(!TextUtils.isEmpty(s)){
if(s.length() >= maxLength){
return s.substring(0, maxLength);
}
}
return s;
}
Use below code in TextView
android:maxLength="65"
Enjoy...
I did this using the maxEms attribute.
<TextView
android:ellipsize="end"
android:maxEms="10"/>
I am sharing an example where I have set maxLength=1 i.e. limit it to a single line with maxLines attribute, then use the ellipsize=end to add a "..." automatically to the end of any line that has been cut-off.
Please Note: layout_width which is 120dp i.e. after 120dp any text
exceeding will triggrer "ellipsize=end" property
paste the below code directly to check.
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:maxLength="40"
android:text="Can I limit TextView's number of characters?"
android:textColor="#color/black"
android:textSize="12sp"
android:textStyle="bold" />
.
You can use setEllipsize method of TextView class
http://developer.android.com/reference/android/widget/TextView.html#setEllipsize(android.text.TextUtils.TruncateAt)
With the constants of TextUtil class for added the suspension points
http://developer.android.com/reference/android/text/TextUtils.TruncateAt.html
Programmatic Kotlin.
Cut off the start of the text:
val maxChars = 10000
if (helloWorldTextView.text.length > maxChars) {
helloWorldTextView.text = helloWorldTextView.text.takeLast(maxChars)
}
Cut off the end of the text:
val maxChars = 10000
if (helloWorldTextView.text.length > maxChars) {
helloWorldTextView.text = helloWorldTextView.text.take(maxChars)
}
Here is the correct Solution. Keep remember, to set width to WRAP CONTENT. and ems is not based on Max Charater. EMS based on width. If ems value width will exceed the text length, then automatically (...) will be added at last of textview.
android:ems="8"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="This is a demo text"
As mentioned in https://stackoverflow.com/a/6165470/1818089 & https://stackoverflow.com/a/6239007/1818089, using
android:minEms="2"
should be enough to achieve the goal stated above.
you can extend the TextView class and overwrite the setText() function.
In this function you check for text length or word cound.