Styling Text inside Android TextView - android

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

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

how to superscript in a title in androidplot?

I'm trying to superscript a number in a title in androidplot, like so:
strings file:
<string name="plot_title_3m">stuff per m<sup>3</sup></string>
xml file:
androidPlot.title="#string/plot_title_3m"
but it doesn't superscript at all, the number is normal styling. I've also tried using
<string name="plot_title_3m">stuff per m<sup>3</sup></string>
but no dice, it actually shows the sup tags in the title
also tried this
<string name="plot_title_3m">stuff per m<small><sup>3</sup></small></string>
and actually found out that the 'small' tags don't work either...
You can use HTML tags to achieve this. To be more exact, you need to use Spans on your input text to achieve this look on TextView, but we have a friendly neighborhood helper class that converts some common HTML tags into spans.
E.g.:
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));

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 .

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.

How to create multiple lines of text in android?

In my project I need to display the instructions page. The page consists of 8 instructions. So do we need to create 8 textviews to display those instructions? Is it possible to display all those instructions using one single textview?
I read about multi-line textviews.
But for my case can I do so to display all 8 instructions in one textview?
As far as I recall, the newline character \n is recognised when used in strings.xml, so you could declare your instructions there and reference it in the layout. I never found a way to do it within the layout XML itself, but there may be something.
Edit: for clarity - I mean using the escaped character, e.g.:
<string name="instructions">Line 1\nLine 2</string>
You can use \n new line character from string resource or thru code:textView.setText("1st line\n 2nd line");
You can use html to format your string: textView.setText(Html.fromHtml("item 1<br>item 2<br>"));
You can use: StringBuilder to it.

Categories

Resources