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}"
Related
I have a string with placeholder e.g
<string name="str_1">Hello %s</string>
I want to use this in xml layout as android:text="#string/str_1". Is there any way to use this in xml layout to fill the placeholder?
Thanks in advance. I already know String.format(str,str...) in java/kotlin but i want to use this in xml layout without data binding.
You can use something like this.
Write your string in your (strings.xml) with declaring a string variable (%1$s) inside it. For decimal, we use (%2$d).
<string name="my_string">My string name is %1$s</string>
And inside the android code (yourFile.java), use this string where you want it.
String.format(getResources().getString(R.string.my_string), stringName);
This is not a good answer but it may help you get some idea to get going.
Thanks.
It's not possible to format your strings directly in your XML. Inside your code you can use both String.format and context.getString() to get and format your string.
If you want to show something just in the XML and not have it in the final build (only have it in compile time), you can also use tools: namespace like following:
<TextView
...
tools:text="Hello, this is a test"
/>
This will only appear in the layout editor and will not have any effect in the APK. It can also be used for other fields too, like tools:visiblity.
So, let's assume that I want to show a text such as : "Hello stranger ", using TextView. How would I do that? I realized that I cannot edit only a part of the text in TextView by changing textStyle attribute.
Use yourTextViewName.setText(Html.fromHtml(<b>Hello</b> <i>stranger</i>);
Reference here: https://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String,%20int,%20android.text.Html.ImageGetter,%20android.text.Html.TagHandler)
Don't bother with complexity.
Place a TextView with the uneditable text on the left of the EditText. Change the margins and everything that makes it look like a unified view and you're done.
You can define the string in string.xml like this
<string name="text"><b>Hello</b><i>stranger</i></string>
and call it in your TextView like android:text="#string/text". It will work for you.
I am working with android textview. I need print subscript under superscript.
This is my code:
<string name="abc">Text<sup>supertext</sup><sub>subtext</sub></string>
And this is what i get:
Textsuertextsubtext
But i need something like:
Is it possible to do that only in XML without using java?
You could use 2 text views. One with "Text" and next to it a multi line text view with "Supertext...".
However only do that if it's not strings that may ever be translated.
http://docs.fusioncharts.com/charts/contents/Styles/Font.html
I tried this, along with a lot of things but failed to do so.
Here's what I want.
<string name="ss">Bold. Underlined. Italic. Big. Small</string>
I want to format a little bit of the string.
Where it's written bold, I want it to be bold...and same for others.
I tried a lot of tags ...but well nothing worked, and I couldn't find anything on Google or SO.
I know how to do it in a textview, but that's not what I want...
I'm sending some text resource to an activity that shows it...
If I did it with different text views, I'd have to create several of them, a new one for whenever I want bold text, and that's not very elegant.
Is there a way to simple do this in the XML file ? or some other way ?
Try wrapping your marked up text in CDATA tags. For example:
<string name="ss"><![CDATA[<b>Bold.</b> <u>Underlined.</u> <i>Italic.</i> <big>Big.</big> <small>Small</small>]]></string>
And then use Html.fromHtml wherever you're wanting to display it:
Html.fromHtml(getString(R.string.ss))
This problem has been driving me crazy for ages. It's something sooo simple that you just want it to work!!!
Anyway I've found an answer here at http://www.coderzheaven.com/2011/06/19/styling-text-in-android-through-xml/
The key is to load the resource as a CharSequence using getResources().getText(R.string.xxxx) this will retain all the style information and allow you to use inline styling tags.
My mistake was using getString() because when loading your resource getString() will cause the string to lose all its style information.
exemple:
<string name="ss"><font size="15"><b>Parrainage</b></font><u>subscribe</u></string>
b = bold et u = underline .....etc
This is working for me.
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
txt.setText(Html.fromHtml(getString(R.string.welcome_messages)));
more details check Official site:
https://developer.android.com/guide/topics/resources/string-resource.html#StylingWithSpannables
in dimens file write:
<dimen name="size_edittext">180dp</dimen>
and in your xml layout or activity call it:
android:#dimen/ size_edittext
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.