Is there any way to carry text in TextView by letters in Android?
If no settings have been set – TextView carries text by words and situation is possible when with too long word you have big whitespace in TextView.
Example – what I’m talking about:
If you can get it at the TextView in XML, I would try giving it the attribute:
android:singleLine
If you have to do it in Java, there are a couple of options to keep it on the same line:
textView.setSingleLine();
textView.setTransformationMethod(new SingleLineTransformationMethod());
If you do want multiple lines, but you want it to break in a way that doesn't split it on the word, you might have to do it manually by analyzing the width of the TextView and how many characters can fit on a line, then inserting newlines appropriately. The two above methods will keep the contents of the TextView on one line, and it'll scroll horizontally. You can look into how this person is doing it.
Another option is to look into the android:ellipsize attribute, but I don't think it'll do what you're looking for.
Not sure if I understood correctly what you want, but I would guess.... if you would like to display the TextView in a single line, without "breaking" the sentence, you should add android:singleLine="true" to the TextView.
Otherwise, you may replace the "_" characters with space, in this case I believe it will carry the text, from the last space.
Related
I have a text that is sometimes too high and at times one word.
How to put the text in this TextView so that the first two lines are located around the specified spacing?
Should this be done by 2 TextView?
If yes, how can I figure out how much text is placed on the top two lines?
You can use a Spannable to add customizations to a string inside a TextView.
The styling android blog has an excellent post about it: https://blog.stylingandroid.com/introduction-to-spans/
But looking to your print, it seems that the first row will always have a style and the rest another. With this, using two TextViews and customizing the view directly is a better option for code and performance.
I have a following issue with laying out text on Android. I'm basically trying to have two lines of text with minimal spacing and each should be styled differently. I've had quite good working solution with two singlelined TextViews one placed below the other, but I've been still getting a little bit cropped text on certain devices..
So I decided to switch to just one TextView an use Spannables instead which should be generally a better solution in all circumstances.
That means I needed to remove the single line property from my TextView -> in order to be able to wrap the line before starting the second Spannable..But there is an issue when is the text displayed at the first line actually longer than it..TextView wraps Automaticaly which is an unwanted behavior. Below you can see several screenshots, which should you better tell what I'm trying to achieve and where I'm now.
The first image shows new layout with spannables and you can see there the wrapped line as well.
The second image is the initial version of the layout woth two textviews layed out verically in a LinearLayout.
There is also a problem it's actually an appwidget, that means I do not have an access to that textview instance directly. I have been thinking about ditching textviews at all and instead use just ImageView and render all manually on canvas..That seems like an overkill to me, so I'm looking for a better solution. Unfortunately I'm kind of out of ideas and knowledge:)
Thank you
If you want to prevent a multi-word string from wrapping, you can replace the spaces with non-breaking spaces ('\u00A0'). TextView treats these as word characters, but renders them as spaces.
I have an android application in which a user can create what is basically a macro and label that macro with some text. I then create a button for them with their descriptive text. The button is a custom view extending Button. In the constructor I set the layout as follows:
this.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
These buttons are then placed within a GridView. Functionally, it's working as intended but I'm running into a layout issue.
If the text is too long, it will break and wrap to the next line, thus increasing the height of the button while maintaining a constant width. The problem is in how the text wraps, it will break in the middle of a word, instead of gracefully wrapping at whitespace. For instance the test "Perform an Action" will render as
Perform an Ac
tion
Ideally, I'd like to wrap gracefully at whitespace instead of breaking words across lines. I suppose I could do this by checking the length of the text and the font against the width of the button and then doing some fancy insertion of newlines myself, but that gives me traumatic flashbacks to making win32 UIs. Is there a better way?
you can add this attribute to your Button's XML that will magically put the whole text in one line:
android:singleLine="true"
or you can verify the text before you insert it to the button and check the number of words.. if it is too long like more than 25 characters then break it on the second or third whitespace then set it to the button.
hope I got your question right.
AFAIK there's no simple way to do precisely what you want. You can get a decent look using android:singleLine="true" and android:ellipsize="marquee". Also, since you have already implemented your own Button class, take a peek at this question
I had a button which said "Off" and had a drawable to the left, but it was wrapping to two lines. i.e.
Image O
ff
The solution was that I removed the drawablePadding style. i.e.
<item name="android:drawablePadding">5dp</item>
When I display quoted text in a textview, occasionally the wrapping will happen such that the final quote character (") is wrapped but nothing else is. So I wind up with a dangling quote as the last line, which doesn't look right.
Is there some property to set to keep the text together in these instances? My only thought would be that the period at the end of a quote would get marked as whitespace or the end of a word and so the ui feels free to wrap what comes after.
I was having a problem with dangling colon's in my TextViews, and this bit of code stopped them from wrapping:
tv.setEllipsize(null);
tv.setHorizontallyScrolling(true);
and my unverified stab at the equivalent xml:
android:ellipsize=0
android:singleLine="true"
Change the text font a half-size smaller or something similar to that. This could be an easy fix if not the best solution.
I am in the process of trying to convert a desktop app to Android - and am struggling with some very basic stuff.
When I specify a layout including a textview that holds a sizable amount of text wrap_content seems to arbitrarily break in the middle of a word and I can not find any documentation indicating this can be controlled.
Try useing Ellipsize property of TextView.
"If set, causes words that are longer than the view is wide to be ellipsized instead of broken in the middle"
Just posting this since none of the other solutions worked for me.
I had copied and pasted some text from online and I didn't realize it had weird spacing characters in that caused the text to wrap mid-word rather than at whitespace.
Here is the nasty character " " that looks like a space but isn't actually a space. I did a find and replace for this character and that solved my problem.