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.
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.
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'm looking for a way to provide an additional line or two of text (in a smaller than usual font), in some of settings form components in my app. Such an additional text is referred to as 'secondary text' in many places in Android design guide (e.g. here: http://developer.android.com/design/patterns/settings.html).
However, I cannot find any way to specify it in components, such as TextView or Checkbox.
I'm sorry, but as a new user, I'm not allowed to post pictures here. The examples can be found in the Design Guide linked above :)
Thanks :)
The 'secondary text' is known as the summary.
So in xml you could have
<CheckBoxPreference
android:key="#string/keep_screen_on_KEY"
android:summary="#string/general_battery_warning_one"
android:title="#string/keep_screen_on_title" />
where
<string name="keep_screen_on_KEY">keep_screen_on_key</string>
<string name="general_battery_warning_one">Increases battery drain</string>
<string name="keep_screen_on_title">Keep display on</string>
This would give you a checkbox in preferences
You can change this in code with the .setSummary(CharSequence summary) method, which is available for ListPreference and CheckBoxPreference.
I've started learning coding for the android, I know the basics of programming in general and thought that android would be fun, Which it has so far.
Now in my exercises in the book I have, It says to add more text to the application. The application is nothing at the moment but 1 string, And I have to add another string.
Now when I have added the string to the strings.xml file and then on the main.xml I type:
android:text="#string/AppName" />
AppName is the new string I made which in the strings.xml it looks like this:
This App is called Droid1
The weird thing is when I type in the main xml to referr to the string, It doesnt even get colour coded when I type the android:text part. The whole line stays as the black text colour. Im sure im not missing anything as the string is all colour coded and so is the last string that I referred to while following the examples in the book which is:
android:text="#string/hello" />
And this is what is confusing me. So please point out the obvious or not so obvious thing that I have done wrong. Any help at all will be appreciated
Android uses the XML format to define interfaces and such. The line
android:text="#string/AppName" />
is not valid XML, and the black syntax is Eclipse's way of showing this. This is probably because you have forgotten to put some lines above it. What you want to have is something like this:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/AppName" />
This tells Android that you would like to put a TextView (or, a text label or whatever) with your string in it. Also note that the android:layout_width and android:layout_height attributes here are always required: without them, you will get an error and you can't build your application.
If you are not already familiar XML, I would highly recommend taking a look at an XML tutorial (for instance at Tizag or W3Schools) and learn the basics of XML, since understanding the XML language simplifies Android programming a lot.
If you want your text to have color, you can set android:textColor="######" in your xml either textview or whatever you used to display the string. ###### is your color code.
I'm trying to make an app with localisation built in, but I want a way that I can create a web link within the text, the URL being defined elsewhere (for ease of maintenance).
So, I have my links in res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<string name="link1">http://some.link.com</string>
<string name="link2">http://some.link2.com</string>
</resources>
and my localised text in res/values-en-rGB/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<string name="sampleText">Sample text\nMore text and link1\nMore text and link2.</string>
</resources>
I've not tested this bit, but from the localization section of developer.android.com it says that this approach to reducing content duplication should work, although I'm not sure what folder I should put Italian, for example. Would it be in 'res/values-it-rIT/strings.xml'? Lets assume that I have various other languages too.
I'm looking for a way of taking the base localised 'sampleText' and inserting my html links in, and getting them to work when clicked on. I've tried two approaches so far:
1,
Putting some formatting in the 'sampleText' (%s):
<string name="sampleText">Sample text\nMore text and link1\nMore text and link2.</string>
and then processing the text like this:
TextView tv = (TextView) findViewById(R.id.textHolder);
tv.setText(getResources().getString(R.string.sampleText, getResources().getString(R.string.link1), getResources().getString(R.string.link2)));
But this didn't work when I click on the link, even though the link text is being put in to the correct places.
2, I tried to use Linkify but the regular expression route may be difficult as I'm looking at supporting non-Latin based languages. I tried to put a custom xml tag around the link text and then do something like this:
Pattern wordMatcher = Pattern.compile("<span1>.*</span1>");
String viewURL = "content://" + getResources().getString(R.string.someLink);
Linkify.addLinks(tv, wordMatcher , viewURL );
But this didn't work either.
So, I'd like to know if there's a way of dynamically adding multiple URLs to different sections of the same text which will link to web content?
The problem is your "a href" link tags are within strings.xml and being parsed as tags when strings.xml is parsed, which you don't want. Meaning you need to have it ignore the tags using XML's CDATA:
<string name="sampleText">Sample text <![CDATA[link1]]></string>
And then you can continue with Html.fromHtml() and make it clickable with LinkMovementMethod:
TextView tv = (TextView) findViewById(R.id.textHolder);
tv.setText(Html.fromHtml(getString(R.string.sampleText)));
tv.setMovementMethod(LinkMovementMethod.getInstance());
In your layout set android:autoLink to web
<TextView android:text="#string/text_with_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web" />
And in strings.xml just add the URL(s).
<string name="text_with_url">http://stackoverflow.com/ FTW!</string>
Try using Html.fromHtml() to convert the HTML into a Spannable that you put into the TextView. With what you have in #1, I would expect the TextView to show the HTML source, not rendered HTML.
You have to implement
setMovementMethod(LinkMovementMethod.getInstance());
on your Textview
Here is a better example:
clickable-urls-in-android-textviews