ListView with maybe more than 1 line - android

I want to create a list view which contain TextViews that are built from conceptually separate strings.
Some list items may have 1 line; others might have more than 1 line, for example: Comment name = "alice", and comment text is a long string of text that must be wrapped to multiple lines:
alice commenttttttttttttttttttttttttttttttttt
ttttttttttttttt
but I want it to show:
alice commenttttttttttttttttttttttttttttttttt
ttttttttttttttt
How to set the layout ?

Rather than use two TextView in your layout, just use a single layout, built from your 2 source strings:
txt1.setText(comment.postedBy + " - " + comment.text);
It is not possible using the idiomatic Android ViewGroup layouts (e.g., RelativeLayout, LinearLayout, etc) to have the 2nd view ("commentttt..." in your example) both: a) "word-wrap" to a second line, and b) "float" around the first view ("alice" in your example). The only way to achieve that type of flowing layout is to combine the two strings into a single TextView. If you want varying styles as well, you can use something like Html.fromHtml(String) to parse things like "<b>This is bold</b> and this is not." There are other Span types that you can apply to text as needed.

Related

Is it possible to have TextView with maxLines=2 but if the text has one single long word not break it in 2 lines?

I have a TextView and I set text which I receive from Backend.
the text is either from 1 to 3 words.
Maximum the textview can be 2 lines.
I am using setAutoSizeTextTypeUniformWithConfiguration
and text.breakStrategy = LineBreaker.BREAK_STRATEGY_SIMPLE
And I don't have any success.
wondering is it possible if the text is single word I don't want to split it. I would like to have it in single line with small textSize. if the text is 2 words and long I am fine to show it in 2 lines. The problem is it always breaks the word in 2 lines if it is long while I don't want.
The only solution that comes to my mind is before setting the text to a textview, check whether text has spaces in it.
If it has not that that means it's a single word, so set ptogrammatically maxlines to 1, otherwise set maxlines to 2.
EDIT: I missed text size part. I use this library for autoresizing and it worked so far https://github.com/jivimberg/AutoResizeTextView
it's automatically handled in androidx.appcompat.widget.AppCompatEditText and just set android:maxLines="2"
remove all other properties you set

Styling Text inside Android TextView

In my app, each Target Text has a mix of numbers and strings. I want the numbers to be highlighted in Bold and string to be italicised. For e.g.
You have reached 25% of your goal. 30 more days to achieve target
Is there an easier way to do it than create 5 separate textviews for this - 3 for string and 2 for number. Managing layout of 5 textboxes for each target is becoming a nightmare in my layout. I have 9 such rows leading to potentially 45 textviews in one layout.
You can use <i> and <b> html tags within your string, like this:
<string name="my_string"><i>You have reached</i> <b>25%</b> <i>of your goal.</i> <b>30</b> <i>more days to achieve target</i></string>
You can read more here, under the Styling with HTML markup section: https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
You can use the following example,
For Kotlin
textView.text = Html.fromHtml("<b>Hello</b> <i>World</i>")
For Java
textView.setText(Html.fromHtml("<b>Hello</b> <i>World</i>"))

2-column alignment in a Textview

I've got a TextView in my app that will, on each line, have a label and then a data value. For instance, some line could look like:
Pressure (atm) 0.983
Acceleration 10.277
Now, I have a handful of data values at once, the labels all of various character lengths. I want to data values themselves to be spaced over a bit from the labels, and all lined up, like so:
Pressure (atm) 0.983
Acceleration 10.277
Is there any way to do this? Thanks!
In my experience this is easier to do by separating value and label into two separate TextViews that you add to a LinearLayout for each row. And in the LinearLayout you can use layout weights to distribute it how you like.
You should add tabulator to the text.
The problem is how to add it in XML, because \t don't work. The solution is add that represents a tabulator.
For example in XML definition on Strings.xml
<string name="hello">Hello World, TesttabsActivity!</string>
In code you can also use the \t option:
TextView hello = (TextView) findViewById(R.id.helloTextView);
hello.setText("Hello\t\t\tWorld");
But this option don't align, only adds spaces (the XML solution also align)
Hope it helps.

Load lengthy strings to TextViews

I need to add two TextViews horizontally in my layout as follows, to display data from database and populate.
TextView1_content, TextView2_content.
If the content is too long(TextView1+TextView2) to fit to the layout width, it can go to the next line, but like this.
Load first string and if not enough to type the rest, then go to next line.
I tried android:layout_weight="1" but it separately print both TextViews in second line, it just divide the screen in half for each text view.
Use only one TextView and just populate it with TextView1_content + TextView2_content, something like
TextView.setText(TextView1_content + TextView2_content)
Edit: For different formatting the two values, you can use something like:
String content="<big><b>" + TextView1_content + "</b></big>, " + TextView2_content;
TextView.setText(Html.fromHtml(content));
You can find more HTML tags supported in Android 2.1 in this article: HTML Tags Supported By TextView.

Access text of Multiline TextView in Android

I want to access text of particular line in a multi-line TextView. Any suggestions on how I could do this?
Fetch the Layout (android.text.Layout) of the TextView by calling mTextView.getLayout(); There you can for instance use the getLineStart or getLineEnd methods to get the offset of the text. That combined with getText and used using normal String operations should probably be enough for u!
By default, text view is multi line. Set the text with new line characters for ex. "First line \nSecond line".
Another option is listed here : https://stackoverflow.com/questions/2610280/how-to-get-multi-line-text-on-a-button-in-android

Categories

Resources