2-column alignment in a Textview - android

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.

Related

Kotlin/Android: Text of TextView - fixed and bound part

I am using data binding to fill the text attribute of my TextView:
android:text="#{String.valueOf(todaysData.totalYield)}"
This works fine. Now I would like to a fixed text before but I have no clue if that is possible in xml. I tried:
android:text="Totals: #{String.valueOf(todaysData.totalYield)}"
android:text="Totals: {String.valueOf(todaysData.totalYield)}"
but in both cases the bound value is no longer used but the whole expression is displayed.
Is it possible to combine fixed and bound text parts in xml?
Cheers
Yes you can do That by doing the following:
android:text= "#{#string/generic_text(todaysData.totalYield)}"
and then in your strings you will have:
<string name="generic_text">My Name is %s</string>
Or you can also do the following:
android:text="#{`Totals: ` + todaysData.totalYield}"

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>"))

Android textview different style for bold text

I have a long string, with multiple bold words inside the string.
The styling of the non bold sentences are done inside the TextView XML part and this is looking fine.
But i want to make the bold words bigger and thicker.
Is this possible to change this with XML?
for example:
<string name="long_text">This should be a <b>long</b> text with different <b>styling</b></string>
The bold words are supposed to be 1.5 times bigger
Or should i do this in the Java code?
yes you have to do this in java because there is no way to as per your requirement,
you can use TextAppearanceSpan in java code with Spannable
if i am getting you right than try the following..
textview.setText(Html.fromHtml(getResources().getString(R.string.long_text)));
Mark as right if it works for you .

Android textview wrap around textview

Is it possible to wrap a textview around a textview, where the 2nd textview would wrap to the next line under the first textview?
For example:
<This is one textview> <This is
another textview>
I have tried using android:layout_weight="1" and relative layouts, however, they dont produce this effect.
No. If you're looking to format the two bits of text differently, consider using spans within one TextView.
This will be helpful to you. You can get same effect with HTML in text view
How to display HTML in TextView?
This is impossible because the "another textview" would have a non-rectangular widget area then.

ListView with maybe more than 1 line

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.

Categories

Resources