Building a custom span that occupies the full width of the container - android

I need to create a custom Span that renders its content like source code. Here's an example of what I am trying to achieve. Let's say we have the following text, where I have annotated the start and end of the custom span using square brackets:
Some text [some\nmulti line\ntext] some more text
The prefix ("Some text") and suffix ("some more text") should be rendered normally, but the custom span should be rendered like a code block:
some
multi line
text
I have been looking at using ReplacementSpan for this, since i Have previously used that to create various interesting custom spans, but for this case I just can't get it to do what I want.
I somehow need to get my span to render a full block of graphics, occupying the entire width of the container. Is it even possible to do this?

Android processes multiline spans line-by-line.
The only way I've found is to override TextView.onDraw, compose blocks for each block-span and draw them before (of after, if you want) TextView.onDraw.
Here is an example activity
https://github.com/Babay88/AndroidCodeSamplesB/blob/master/blockquotespanexample/src/main/java/ru/babay/blockquotespanexample/MainActivity.java
It uses customized TextView
https://github.com/Babay88/AndroidCodeSamplesB/blob/master/blockquotespan/src/main/java/ru/babay/blockquotespan/TextViewWithBlockBackgrounds.java
And special BlockQuoteSpan.
You should ensure that each block span starts and ends with a \n.

Related

Jetpack Compose - TextField ellipsis

I am trying to create a single line text input in Jetpack Compose.
Since this input should have a fix width of 200.dp and is only one line, longer text is going to be cut off. Sometimes it looks like the cut off text does not even exist because the cut is made between two letters. In order to show the user that there is "more" text already typed I would prefer an ellipsis (e.g. This is a sample inpu...) effect.
I tried to use the default TextField and BasicTextField composables but there seems to be no easy solution.
Is there a way to create this ellipsis effect in Jetpack Compose?

Breaking to new line in FlexboxLayout

I am using a FlexboxLayout to add few EditTexts. Each EditText contains just 1 character of words.I am using editText for space as well but keeping it invisible.I am using layout setting FlexWrap as "wrap". For instance, if I have two words, some letters of the second word goes to the next line. However, I want the next word( a group of editText) to go to next line instead of just the last few letters(editText).
Is there someway to group these edittext and make the whole word go to next line.Or is there some way to break line within the programme itself.
I tried adding 'EditText' to linear layout and adding it to 'FlexboxLayout', it did not work.
Use this property to start a new line in FlexboxLayout.
app:layout_wrapBefore="true"
In your case, just apply this property to the EditText which you want start a new line.
Quote from FlexboxLayout docs:
layout_wrapBefore (boolean)
This attribute forces a flex line wrapping, the default value is
false. i.e. if this is set to true for a flex item, the item will
become the first item of a flex line. (A wrapping happens regardless
of the flex items being processed in the previous flex line) This
attribute is ignored if the flex_wrap attribute is set to nowrap. The
equivalent attribute isn't defined in the original CSS Flexible Box
Module specification, but having this attribute is useful for Android
developers. For example, to flatten the layouts when building a
grid-like layout or for a situation where developers want to put a new
flex line to make a semantic difference from the previous one, etc.

Android create Spannable which does not wrap

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.

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>

android text wrapping

In my android application, I am displaying a long string into multiple pages. I achieve this by breaking the long string into a string array where every array element holds the number of characters which can fit on one screen (without scrolling). Also by using a previous/next button at the bottom of my activity I change the content of my textview to switch between pages(array elements).
Where I am stuck is in finding out how many characters will show on one page/screen. Remember I don’t want user to scroll. I use the paint.breaktext with screenwidth as the parameter and find out how many characters go in one line and then multiply it by number of lines on one screen to get the number of characters in a page. Android’s text wrapping at the end of each line is what gets my calculation of finding characters in a page, wrong. I am trying to put my own logic to accommodate for text wrapping but that is not working.
So my question is:
Is there any way I can find out that how many characters will show in one screen?
Or what can also help me is if I find out what logic is followed by android for wrapping the text for newline. Because if I know that, then I can readjust my number of characters on a page accordingly.
Or other option could be to disable text wrapping (not sure if possible). Because then I can use my own logic to wrap the text rather than trying to figure out Android’s text wrapping logic and adjusting my character-on-a-page accordingly.
Any help is much apreciated.

Categories

Resources