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.
Related
My problem is very simple: I've started an app for playing Darts. The app will have several activities ('pages').
One page will be about the rules of the game. I'll be using a scroll layout because it's quite some text. But how to get the text there?!
I assume working with strings is not the best way? Do I use the XML file to get the text on screen then or does it work via Java (Assetmanager)?
Maybe there are sample apps in which large chunks of text are used?
I know this really might seem like a trivial question but I haven't a clue where to begin.
Thanks in advance!
You should put your string in your strings.xml in your res\values folder.
You can define strings by ID which allows easier internationalization (i18n), so that you can easily adjust the strings used in your app to locale (which is done automatically using resource identifiers, and it falls back to strings.xml if it can't find a strings-hu.xml in case you have Hungarian locale set as system language).
You can also define string-array and the like in XMLs. Then all you need is create a layout XML with a ScrollView in it that has a TextView in it and then you set android:text="#string/rules" for that TextView and you're done.
It is so simple my friend.
You can simply use TextView and in "android:text" you refer to the string that you delared in strings.xml file (by its name)
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="#string/text_name"
/>
If your text is dynamic, you can modify it in Java code!
Make a String Resource like this.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="string1"> your text </string>
<string name="string2"> your text </string>
</resources>
and access like this if you are in Activity.
getResources().getString(R.string.string1);
If you are a begginer you should read some tutorials after post a question...
I give you a three nice tutorials below :
Want to Learn How to Program for Android? Start Here
Android Programming Tutorial
Android Development with Android Studio or Eclipse ADT
About your question, if you don't know how to use the string.xml resource just read the string-resource guide
Hope it helps.
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 don't want to specify hard coded test size values in my layout.xml, hence i am using the following specification :
android:layout_height="#integer/intervalViewHt"
and
#integer/intervalViewHt value is as follows:
<integer name="medium">15</integer>
Now, the while inflating android is creating a problem saying that it cannot inflate the view. I want to actually specify value in dp so the actual value should be like
android:layout_height="15dp"
Can anyone help me here ?
What you're looking for are "dimensions" rather than plain integers.
Declaration:
<dimen name="intervalViewHt">15dp</dimen>
Usage:
android:layout_height="#dimen/intervalViewHt"
Have a look at the given link for more examples in the Android docs.
I use something like this for text size across different devices. It's java based, not xml.
tvOutput.setTextSize(TypedValue.COMPLEX_UNIT_DP, getResource().getInteger(R.integer.medium));
I haven't tested the code so you might have to play with it. I'm sure you could do something like.
button1.setHeight(TypedValue.COMPLEX_UNIT_DP, getResource().getInteger(R.integer.medium));
Though again I haven't tried it and you may need something different then TypedValue
What if you give it a String rather than an int. i.e in strings resource you have a string "15dp" that you reference
I want to build a help page for my app so obviously it is gona be texty. Other than putting those texts inside my string.xml or in my layout xml, is there any other ways for me to do it? how do I include paragraphs in them? any help would be very much appreciated!
You should put your help text into strings.xml file and use \n\n for line break as #alex suggested because in future if you want help text in different language then you can easily do this by adding different strings.xml file for different localization
But if you still do not want to put help text into strings.xml then you can use
static final String helpText = "help text";
and set help text into textView.
Put your text strings in strings.xml.
Reference them in your layout's XML.
Use \n\n to mark the beginning of a new paragraph.
I have a textview which should render its content with HTML formatting. From code, this can be achieved like this,
textView.setText(Html.fromHtml(someText));
Is there a way to do this through the XML?
I ask because i do not want to set the text through the code, In my case, the text comes from a DB call and displayed through an adapter.
Yes there are a bunch of (simple) tags that are understood by TextView -- if the text is set in XML from a string resource.
So basically
<TextView text="#string/foo" .. />
It is also possible to give templates like "Hello <b>%s</b>", but here you still need to run some code to fill in the value for %s
Have a look at http://developer.android.com/guide/topics/resources/string-resource.html for formatting hints and short examples.