I am relatively new to android, so please be patient. In my project, I need two spinner objects and therefore need two different string-arrays within my strings.xml resource file. When I try to add the second string-array to the xml and run the app, I get a resource compilation failed error. It points to :app:mergeDebugResources which failed. I don't really know what else is wrong aside from this one message, as there is nothing that populates in the Logcat window. Please let me know if more info is needed.
<resources>
<string name="app_name">MindYourPet2</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="submit">submit</string>
<string name="pet_profiles_title">Pets</string>
<string name="add_pet_title">Add Pet</string>
<string name="reminders_title">Reminders</string>
<string-array name="species">
<item>-</item>
<item>Cat</item>
<item>Dog</item>
</string-array>
<string-array name="frequency">
<item>Don't Repeat</item>
<item>Every Day</item>
<item>Every Week</item>
<item>Every Month</item>
</string-array>
I think the problem could be a syntax error in the XML preventing Android Studio/SDK from finishing the parsing of your XML resources for generation of things such as R.java
One problem I notice in your XML is that apostrophe needs escaping.
<item>Don't Repeat</item>
Should be
<item>Don\'t Repeat</item>
This way it does not treat the apostrophe as a quote and cause problems in the format of the XML, it instead treats it like any other character.
Related
I am using Android studio and have an array in my string.xml file as:
<string-array name="my_array">
<item>text1</item>
<item>text2</item>
<item>text3</item>
</string-array>
I know how to access the array (and get the 1st item) in my MainActivity.java file:
myButton.setText(getResources().getStringArray(R.array.my_array)[0]);
My question: Is there anyway to set the text directly in the activity_main.xml file? I tried:
<Button
android:id="#+id/myButton"
android:text="#array/my_array[0]"
... />
but that causes an error. Without the "[0]" it displays the 1st value (text1), but maybe that is just because of the button's size and it's not showing the rest - I can't get it to display other items (e.g., text2).
Is it possible to access one value of the array directly in the layout file?
Thanks.
I found a good answer here: https://stackoverflow.com/a/4161645/933969
Basically you create named strings first (and use those where you would want mystrings[x]) and then create your array using references to those named strings:
<string name="earth">Earth</string>
<string name="moon">Moon</string>
<string-array name="system">
<item>#string/earth</item>
<item>#string/moon</item>
</string-array>
I use Lint to check for missing translations in strings.xml (in values-de, values-en, values-hu, etc.). However, I noticed that if there are no strings in another language in other strings.xml files, Lint does not find the missing translations.
Example:
values/strings.xml:
<string name="abc">test</string>
values-en/strings.xml:
<!--empty-->
-> not work
values/strings.xml:
<string name="dummy">dummy</string>
<string name="abc">test</string>
values-en/strings.xml:
<string name="dummy">dummy</string>
-> works
Is there any way to force Lint to control that translation for string called "abc" is missing without adding dummy string? Unfortunately, even empty strings.xml files are not enough.
If you want to have a string only in strings.xml try that.
<resources xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingTranslation">
<string translatable="false" name="abc">test</string>
</resources>
In my Android app I'm using strings.xml for all texts. I have many situations where I use almost the same string,
e.g. "Name" and "Name:" - translation is the same only additional colon is difference.
Is there any other way to have these two string except creating two string items like this:
<string name="name">Name</string>
<string name="name2">Name:</string>
There is no way you can concatenate strings in the strings.xml file.
All you can do is specify the format,
<string name="name">Name</string>
<string name="string_with_colon">%s:</string>
Then pass the name programatically,
String.format(getString(R.string.string_with_colon), getString(R.string.name));
Yes, you can do so without writing any Java/Kotlin code, only XML by using this small library I created which does so at buildtime: https://github.com/LikeTheSalad/android-stem
Usage
Based on your example, you'd have to set your strings like this:
<string name="name">Name</string>
<string name="name2">${name}:</string>
And then after building your project, you'll get:
<!-- Auto generated during compilation -->
<string name="name2">Name:</string>
I can make string to be a reference to another string just like that:
<resources>
<string name="my_string">My String</string>
<string name="my_alias">#string/my_string</string>
</resources>
And i tried to make string-array reference the same way:
<string-array name="my_string_array">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<string-array name="my_string_array_alias">#array/my_string_array</string-array>
But this does not work, unfortunately. I tried to use this array and alias in ListPreference:
<ListPreference
android:key="my_key"
android:title="#string/my_title"
android:entries="#array/my_string_array_alias"
android:entryValues="#array/my_string_array"
android:dialogTitle="#string/my_dialog_title" />
and entries is just an empty array. Of course, i can make both entries and entryValues to take the same array, but i like it to be a different ones.
Found this on the Android dev site.
Note: Not all resources offer a mechanism by which you can create an alias to another resource. In particular, animation, menu, raw, and other unspecified resources in the xml/ directory do not offer this feature.
It's a little vague but I believe this means the framework doesn't support it.
Android allows to create aliases of resource strings like:
<resources>
<string name="foo">somestring</string>
<string name="bar">#string/foo</string>
</resources>
by which the resource "bar" becomes an alias for the resource named "foo".
What I would for my app is a possibility to combine an existing resource prefix with different suffixes, i.e. to extend it like:
<resources>
<string name="foo">foo</string>
<string name="bar">#string/foo+bar</string>
<string name="else">#string/foo+else</string>
</resources>
where the resource "bar" would yield the string "foobar". Its clear that '+' doesn't work here but is there some other option to achieve such a string concatenation, so that one could define a bunch of string resources that have a common prefix?
I realize of course that I could do such resource string concatenation at runtime but defining them statically in the resources would seem so much more elegant and simpler.
Michael
No, it's not possible.
You can just use <string name="bar">This is my %s</string>
and then use String.format() in your app to fill in the variable with for example
getResources().getString(R.string.foo);
I know it's late, but anyways now you can do so by using this library I've created: https://github.com/LikeTheSalad/android-stem
It will concat all of the strings you want to at build time and will generate new strings that you can use anywhere in your project as with any other manually added string.
For your case, you'd have to define your strings like so:
<resources>
<string name="foo">foo</string>
<string name="bar">${foo} bar</string>
<string name="else">${foo} else</string>
</resources>
And then, after building your project, you'll get:
<!-- Auto generated during compilation -->
<resources>
<string name="bar">foo bar</string>
<string name="else">foo else</string>
</resources>
These auto generated strings will keep themselves updated for any changes that you make to your templates and values. They also work with localized and flavor strings. More info on the repo's page.