Android create Spannable which does not wrap - android

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.

Related

Set Text Customization in TextView

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.

Wrap text in button gracefully at whitespace

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>

Pocket-like TextView automatic spacing based on TextView width?

This is quite hard to understand, but I'm trying to find out how to make a TextView adapt to change the text spacing between words on a line which allows the text to reach the very right side of the TextView.
Consider this as an example (this should get my point across):
This one is a line of text which fills the view itself
This is another that does the same thing
How would I go about making my text react like this? An example application which does this is Pocket, so I know it can be done - I just don't know how.
Any help is appreciated!
What you're referring to is called text justification and is something that has been discussed more than once here on SO in the context of Android.
The short answer is that, unfortunately, justification is currently not (natively) supported by the TextView widget. There are however workarounds that involve either:
Manipulating the text in the TextView in such a way that the result is visually close to that of justification. Example.
Using a WebView to render the text. Example.
Justifying text on a web page is trivial, but the WebView is a more heavyweight component than a TextView, and hence the feature will come with a performance penalty.
Note that I don't know what approach Pocket is using for their articles, but there are ways to figure that out, and they're not too complicated. That's a completely different can of worms though, so I'll leave it at that.

Android TextView carry text by letters

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.

How to prevent words from breaking in textview when using wrap_content?

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.

Categories

Resources