I have a list view where each list item is a message with a sender/recipient, date, and content. I want to display the sender/recipient in the top left, the date in the top right, and the content on another line below. My problem is that if either the name or content is too long I want to be able to cut off the string and end it with an ellipsis (...). Is there any way to do this?
Thanks for any help!
You can use
android:ellipsize="end"
Or programmatically
myTextView.setEllipsize (TextUtils.TruncateAt.END);
For my details: android:ellipsize
Use java! You could add extra features, like detecting the end of a word.
if (mystring.length() > LIMIT) {
mystring = mystring.substring(0, LIMIT) + "...";
}
Related
As the question is clearly stated, I want to add a small line spacing after every \n defined in a string in a resource file in Android.
Let's say we have a string defined in xml like this:
<string name="line_test">This is the 1st line. This is still the 1st line.\nThis is the 2nd line.\nThis is the 3rd line. This is still the 3rd line.\nThis is the 4th line.</string>
The normal output when setting it to a TextView will be like the left side of this image. What I want to achieve is the right side of this image.
There are some attributes in xml like:
android:lineSpacingMultiplier="2"
and
android:lineSpacingExtra="10dp"
but these attributes will only lead to something like this:
So I don't want to add line spacing after every new line because the width of the line is consumed, but only after every defined \n.
Any way to achieve what I want through code or html? By saying html, I mean something like this, but with some modified stuff after adding <br/>:
textView.setText(Html.fromHtml("This is the 1st line. This is still the 1st line.<br/>This is the 2nd line.<br/>This is the 3rd line. This is still the 3rd line.<br/>This is the 4th line."));
It is more like I want to create many paragraphs in the TextView with small line spacing after each paragraph all in one string. I thought that this would be possible to be done through HTML, so that's why I added html tag.
Edit:
I also don't want to add two line breaks because I only want a small line spacing, and putting two line breaks together will have a large line spacing, which I don't want due to some design issues in my app.
What about giving two line breaks like this
<string name="line_test">This is the 1st line. This is still the 1st line.\n\nThis is the 2nd line.\n\nThis is the 3rd line. This is still the 3rd line.\n\nThis is the 4th line.</string>
In case of HTML use two <br/> tags
textView.setText(Html.fromHtml("This is the 1st line. This is still the 1st line.<br/><br/>This is the 2nd line.<br/><br/>This is the 3rd line. This is still the 3rd line.<br/><br/>This is the 4th line."));
You can try using this:
public String addNewLine(String string) {
int count = string.split("\n", -1).length - 1;
StringBuilder sb = new StringBuilder(count);
String[] splitString = string.split("\n");
for (int i = 0; i < splitString.length; i++) {
sb.append(splitString[i]);
if (i != splitString.length - 1) sb.append("\n\n");
}
return sb.toString();
}
Good Day i want to hide some specified or certain part of text in textview!Important: Im not talking about hide the full textview with TextView.setVisibility(View.Gone) I'm not talking about transparent of TEXT in textview!im not talking about hiding full text in textview!So please help me to hide some text.
Example: lets say i have a textview with this text (10-Sporting Goods)
I want to hide the (10-) and show only Sporting Goods text.Any help will be appreciated!Thank you very much beforehand!
Although even i would appreciate for your case to strongly go with DroidWorm/Gabriella approach , just for the information of all the other folks who may see this in future.
If you really wish to hide just a portion of your textview which has the entire string in itself, you should use a SpannableString , as below:-
tvHello = (TextView) findViewById(R.id.tvHello);
SpannableString customText = new SpannableString("10-Sporting Good");
customText.setSpan(new RelativeSizeSpan(.1f), 0, 3, 0);
tvHello.setText(customText);
This code will technically HIDE the 10- from 10-Sporting Good without using a substring.
You could try to get the whole text like
String text = textView.getText().toString();
and then make substring of it like this:
String wantedSubstr = text.substring(4); //for example - everything from the 4th index to the end
then set this substring as text of your textView like this:
textView.setText(wantedSubstr);
There is one the possible solution of it is that..First you have to find the index(position) of "-" and than split the string according to it therefore use below code
String text = textView.getText().toString();
int position=text.indexOf('-');
String wantedSubstr = text.substring(position+1);
textView.setText(wantedSubstr);
Will there always been "10_" in front of it? Or will there always be 3 characters before the text you want? Or will there always be a "-" or "_" before the text you want?
If so, you could just do a simple method which takes the substring and then updates the textview. If so I can help you write a simple method
You cannot hide part of textView, instead you can make a substring of the specific string and setText using it.
Do it like:
String originalString = "10-Sporting Goods";
String subString = originalString.substring(3);
textView.setText(asubstring);
So basically by default the text view in android wraps contents because of which my text looks something like this
I'd like to disable the text wrapping property and set equal number of characters in the text view.
How do I do it?
Your question is not 100% clear but if you're talking about justification, Android doesn't support it. But here is a library which does.
If you literally don't want the text to wrap use:
android:singleline="true"
You have to use single line "true", and define the padding between the cells.
ps: to set a number of characters , you have get the refference from this textView and edit the content(just format the string).
Example:
char text[] = originalText.toCharArray();
String newText = "":
for(int i=0; i< text.length(); i++){
if(i<x){
newText =newText + text[i];
}
else{
break;
}
}
To achieve this, simply use the newline syntax "\n" where you want a new line to begin.
For example:
String text = "Who is the Boss? \n You are the Boss";
This can also be achieved programmatically of course in code if you're getting a string without one.
Simply write a method that checks for white space and insert the "\n" say after each successive 3 whitespaces have been detected. Then programmatically set the string to the TextView.
try adding this attribute to your textview and then try
android:gravity="center_horizontal
i've got a database with a field in var_char(2000).
in this field there's text with some new line, like a normal text written:
hello
i am davide
bye
i put this text in a textview but i see the text like a unique line (hello i am davide bye), without newlines.
in iphone it is all normal and i've done nothing particular... but here no.
how can i?
i've tried with replace \n or replace \r\n o other things but without success.
Also with Html.fromHtml()
the singleLine(false) is deprecated, and it doesn't work.
also text doesn't work. it see the newline as a space
Try setting android:singleLine="false" to your textView.
Edit:
If this does not work check whether the string has a new line character using below code
char[] chararray= mString.toCharArray();
for(char temp:chararry){
int value = temp;
System.out.println(value);
}
Decimal value of Newline is either 10 or 13. While space character is 32.
Edit2 : I think TextView does not go to next line for \r which is 13.
So do
mString = mString.replaceAll("\r","\n");
did you try changing the datatype in sqlite?, your varchar to just text? if not, try doing so maybe it'll help
Try to disable the multi-line feature of the text view.
If you created the text view from an XML file, add this attribute to the text view :
<TextView
[...]
android:singleLine="false"
[...] />
If you created the text view programmatically, try to use the following method instead :
TextView myText = [...]
myText.setSingleLine ( false );
I know that if you do something like
myTextView.setText("This is on first line \n This is on second line");
Then it will display properly like this:
This is on first line
This is on second line
When I store that string in a database and then set it to the view it displays as such:
This is on first line \n This is on second line
Here is the line of code I use to extract the string from the database:
factView.setText(factsCursor.getString(MyDBAdapter.FACT_COLUMN));
I simply populate the database from a text file where each line is a new entry into the table so a line would look like this "This is on first line \n This is on second line" and it is stored as text.
Is there a reason that it isn't displaying the \n characters properly? It must be something to do with the string being in the database. Any suggestions?
I found this question Austyn Mahoney's answer is correct but here's a little help:
private String unescape(String description) {
return description.replaceAll("\\\\n", "\\\n");
}
description being the string coming out of your SQLite DB
As Falmarri said in his comment, your string is being escaped when it is put into the database. You could try and unescape the string by calling String s = unescape(stringFromDatabase) before you place it in your TextView.
As a side note, make sure you are using DatabaseUtils.sqlEscapeString() on any kind of data that is from the user or an unknown changeable source when inserting data into the database. This will protect you from errors and SQL Injection.
Try \\n instead of \n. If it throws an exception than use newline keyword in place of \n....newline is one character, ascii 10; it's often entered in a string literal...and will serve your purpose....:)
"This is on first line"||x'0A'||"This is on second line"
The || concatenates strings and the x'0A' is an unescaped newline.
If you're inserting records you'll have to replace every newline with "||x'0A'||" (If your string is double quoted). This may seem clumsy compared to the other asnswers. However if your lines are in separate columns this also works in a select:
SELECT firstline||x'0A'||secondline FROM wherever;
I found this while having the same problem you are: http://www.mail-archive.com/sqlite-users#sqlite.org/msg43557.html
A text area can be in multi line or single line mode. When it is in single line mode newline characters '\n' will be treated as spaces. When in doubt, to switch multi line mode on you can use the following code:
setInputType(getInputType() | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
I had the problem that the same code did not work on honeycomb and on froyo, which seem to have different defaults. I am now also excluding the flag when I want to force a field to be single lined.
From the Android doc:
public static final int TYPE_TEXT_FLAG_MULTI_LINE Added in API level 3
Flag for TYPE_CLASS_TEXT: multiple lines of text can be entered into
the field. If this flag is not set, the text field will be
constrained to a single line. Constant Value: 131072 (0x00020000)
http://developer.android.com/reference/android/text/InputType.html#TYPE_TEXT_FLAG_MULTI_LINE
You have to set the flag before you populate the field.