Whenever a text (That I set programmatically from an Activity) is longer than n charachters, I want to cut off and limit the texts length and add 3 dots at the end of the textview. Text is initially set to empty (just "") in xml, and is set from activity later.
This is how I tried:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:maxLength="10"
android:ellipsize="end"
android:maxLines="1"/>
This is how it ends up looking with a random text, and I set the maxlength to 10 charachters:
I was expecting it to look like jrkfkgkglg... because the text is jrkfkgkglggfgirng, but the dots are not added at the end. Any suggestions?
In order for this to work, your width must be match_parent or have a any other size defined instead of being wrap_content.
You are telling your edit text to be maximum 10 chars in length, in your screenshot, you have a string with that length so if you will try to add anything to that string it won't get added.
You can remove the android:maxLength="10" limit and check your text from your code programmatically, something like this:
if (yourText.length() > 10) {
yourText = yourText.substring(0, 9); //get the first 10 chars
textView.setText(yourText + "..."); //display
} else {
textView.setText(original string < 10chars);
}
For this you have to remove the attribute
android:maxLines="1"
and add the new attribute
android:singleLine="true"
and in addition make your textview width match_parent.
It will work properly when you will add this then you don't need to use anything else.
Related
I am testing it on Galaxy S2,there is no problem on it. But some devices looks like These screenshots- samsung Galaxy Note -.
What should I do? I cant find any topic about it?
<TextView ...
....
android:ellipsize="none"
..></TextView>
You need to set the ellipsize to none for that textview and there will be no "..."
Second thing this only happens when you have set your textview to SingleLine = true, so if you want to streatch your textview to multiline based on the text size then remove the singleline.
this is because the width of the textView is less and you are trying to add characters more than that,,
1) try to add more width to the textView
you can check out here how to set width
Well you can use android:layout_width = "wrap_content".
Alternatively you can use android:layout_width = "45dp" (or your defined width) and set the android:maxLines = "2". This will wrap the test in two lines and you will find what you are asking for.
instead of taking android:singleLine in XMl file you can declare it in your java code where you define your textView.
text.setSingleLine(true);
or in XML file
android:singleLine="true"
android:ellipsize="none"
how can i get the exact value of the specific word in a textview when:
1 - the user had taped the word (the word can be just one of many in some text chunk),
2 - the textview has in its xml layout, android:textIsSelectable="true" ,
so by default android lets the user to highlight a specific word
and opens a small menu(copy, paste, share). can i manipulate this menu or maybe add more options to it in order to extend it and do something i want with the highlighted word?
i need to get the exact word based on some interaction with the user(click , tap, select....)
i didn't find any straight forward answer. is it even possible?
(i have android htc one x)
Thanks.
A more suitable component to use for your scenario is EditText.
It is declared like this in your manifest file:
<EditText
android:text=" This is not an editable EditText"
android:id="#+id/EditText01"
android:layout_width="wrap_content"
android:textColor = "#android:color/white"
android:editable = "false"
android:cursorVisible="false"
android:layout_height="wrap_content"
android:background = "#android:drawable/dark_header">
</EditText>
Then, it is only two lines of code to get the selected text:
EditText et = (EditText)findViewById(R.id.title);
String selectedText = et.getText().substring(et.getSelectionStart(), et.getSelectionEnd());
Here, we assumed that the text was selected from left to right. If it was selected from right to left, then above would result in an exception so you can avoid that by checking if start position is higher than the end position.
My button's layout_width set to match_parent.
In order to display multi lines on the button, I tried:
insert '\n' into the text on button
set Singleline false set Maxlines to 2 or 3
convert html from Html.fromHtml
Nothing worked. '\n' showed up as a small square on the button while showing single line of text.
Does anybody have any idea why this is happening and how I can fix this?
UPDATE: I just found out I was using custom button that has its own text drawing. That's the reason. Sorry for the confusion. I just punished myself by banging my head.
If you're trying to add a new line in a layout XML file:
Use
(new line)
android:text="Hi
Hello"
If you're trying to add a new line in code, just use '\n', same as in any other text.
If you can't see the second line, it may be that your Button doesn't have enough height. IE, in my case, the layout containing the button had a fixed height that just happened to make my button perfectly display one line of text.
I just tried and it worked:
1) Define in ../res/values/strings.xml:
<string name="multilines">Line1Line1\nLine2Line2</string>
2) Refer it in the layout file:
<Button
android:id="#+id/btn_multilines"
android:text="#string/multilines"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</Button>
In case you want to do that programmaticaly you can use System.getProperty("line.separator") in the string to change lines.
Like this:
String mybuttontext=line1+System.getProperty("line.separator")+line2;
and then set this String as buttons text.
I have a textview with fixed height. I want to set a long text, but text comes at the last line getting cut by half, I want to avoid it and want to show a continuation symbol. I am giving an image here, I wan to achieve it like in the image.
Use the ellipsize attribute of TextView. By setting :
<TextView
....
android:ellipsize="end">
</TextView>
if the text is longer than your TextView you will have dots(...) at the end of the TextView.
Two line of string only possible to do like this. set ellipsize = end in text view properties.
you should use \n and special symbols from google code.
Hey, Is it possible to specify a textview to have 25 characters in a line? I have tried android:maxLength="25" though it shows the first part of the text and the other part disappears.. Thanks
Edit1: I can also put the text into an EditText.. but I want that the characters that are after 25 chars, go to a new line..
http://developer.android.com/reference/android/widget/TextView.html#attr_android:maxLength
I am afraid, maxLength works for input EditText only. :-(
yes , sure android:maxLength="5" works for TextView to set the maximum number of characters for TextView. You can set android:layout_width="130dip" for the TextView too. That will show the character in the format abc... if the text exceed the layout width.