Android: Refer a property value into string.xml - android

Consider following project hierarchy
Base
Common
Flix
We have Base and Common as library project which we are using in several other projects like Flix. We would like to define some sort of property file into Base and Common project and would like to refer the properties defined at Base or Common level into string.xml file of Flix project.
For example
Consider we have a property file named base.properties under "Base/assets" folder with following content
app.props.cms.url=http://test.flixv2.dev.com/api
We have string.xml file into Flix project, where we have a string resource
<string name="cms_url">HERE WE WOULD LIKE TO REFER THE PROPERTY</string>
we have tried following which is not working
<string name="cms_url">${app.props.cms.url}</string>
Please let know your views.
Thanks

Are you using any kind of build system like Maven or Gradle? This seems to me like a dependency thing.

There is another way out, which is not exact solution of my question
Ref: Concatenate multiple strings in XML?
we tried following:
created a string resource in Base project as below
<string name="app.props.cms.url">http://test.popcornflixv2.device.screenmedia.net/api/</string>
And refer the same into Flix project as shown below:
<string name="cmsservice_url">#string/app.props.cms.url</string>

There seems to be a suggestion that the "official way" of doing this (where you insert the package name prefix) does not work. However, this is a very old report, so I would try that first, before the "reflection" approach - both of which are described here:
android import ressources from library project

Related

A string resource from an Android lib overrides a resource from the app with the same name

I created a simple Android project out of my curiosity. It contains two modules: app and lib. Each module has its own package name. I created one string resource in the lib module and one string resource in the app module. Like this:
<string name="my_str">my_str_from_lib</string>
<string name="my_str">my_str_from_app</string>
Then I set the texts to two TextViews in the app module:
fromLib.text = resources.getString(ru.maksim.sample.lib1.R.string.my_str) // here I expected to see the string from the lib.
fromApp.text = resources.getString(R.string.my_str)
In both cases it was my_str_from_app
I ran Lint and thought it would detect the fact of resource overriding. But it didn't.
Is there a chance to detect this situation? Not necessarily with Lint. Other tools suit me too.
Well you can do a global search in your root folder. Find all instances of my_str and make sure its only the string.xml in your app that uses it as name.
The answer here is a script that parses a resulting XML to which all the string from the main app module and all its libraries are merged and finds all the duplicates.

How to read Asset resources from "Android Library Project"

I have:
one Main App
one Android Library Project with a Fonts in it in "assets/fonts/Old_English.ttf
The Main App references the Android Library Project and it works fine except the following:
I would like the use the shared asset resource, but I don't know how to do. Can someone help ?
Typeface.createFromAsset(getAssets(), "fonts/Old English.ttf")
These might work:
Storing and accessing assets.
library structure
MyLibProject/src/main/assets/Old_English.ttf
Project Reference
Typeface.createFromAsset("file:///android_asset/Old_English.ttf");
The solution is as per the link mentioned. I didn't try its working. Please comment if it works or not.
Make sure that you store your fonts according to the following structure :
Now use the following line :
Typeface typeface=Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Old_English.ttf");

Android - Failure [INSTALL_PARSE_FAILED_MANIFEST_MALFORMED]

I have finished to write my small program and now I want to add locale.
Before I had next structure: /res/value/strings.xml
I have added to the structure new folder with file with another language:
/res/value-uk/strings.xml (it's ukrainian language)
after that I built my APK in AndroidStudio and I got error:
Failure [INSTALL_PARSE_FAILED_MANIFEST_MALFORMED]
Unknown failure (Failure)
Error while Installing APK
I was searching this problem in google and nothing helped me.
Please reccomend me how to solve this problem. Thank you.
I had the same problem and I found a lot of people managed to solve the problem by adding the full package name inside the various manifest "android:name".
It didn't work for me and I found out that my problem was different:
I had two string files, "strings.xml" and "strings-en.xml. In the former one I had some values with the field "translatable=false", such as:
<string name="db_name" translatable="false">my_db</string>
To solve the problem I had to REMOVE that string resource from the strings-en.xml, in order to have those strings only in the default strings.xml.
I hope this can help future developers.
The xliff namespace declaration in your strings.xml file seems to be wrong. Try using <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> instead. Or strip out all the <xliff:g> tags and see if it makes a difference - they are not needed any more after translation. And Chol is right: the default folder name for Ukrainian is values-uk, not value-uk.

Android Studio can't find R.layout.simple_list_item_1, or any other default layout IDs

IDs for things like R.layout.simple_list_item_1 don't show up on autocompletion in Android Studio. Nor can they be resolved when the name is typed in manually. I'm trying to use an ArrayAdapter, and I had to manually create a simple_list_item_1.xml in my project layout directory, and paste in the contents from the Android repository on Github
Am I missing something, or is that what's supposed to be done? Most resources I go to seem to just reference the list item XML without doing anything else
to use android predefined layouts, colors etc. You should use
android.R.layout.simple_list_item_1
probably you try to reach your own resources. Check your imports and you should see your R file.
as #Orhan Obut explained, use
android.R.layout.simple_list_item_1.
Do NOT leave out the word android in the above statement. That is how you access android's predefined resources
It's two years later but never too late because I spent the day reading all posts about this and the answer on the Android Developer site was simple: make sure the google depository is installed for your project in the repositories section of its build.gradle:
maven {
url "https://maven.google.com"
}
Worked for me as soon as the project was synched after adding the .
I got the same error. The error was resolved when I imported android.R.layout and typed layout.simple_list_item_1 instead of R.layout.simple_list_item_1.

Why is eclipse not adding my strings to the resource file?

Ok so I am working on an Android OS and I have followed the instructions and pulled down the source, plus setup eclipse properly. I have made a change in my strings.xml file in one of the framework projects. However, after I perform a make on the project that string does not get added to the appropriate resource file in out/target/common/. Hence my Eclipse is not picking up any new resource.
I have performed the following.
1.) A clean on the project.
2.) Completely delete the out so that everything would be rebuilt.
3.) Tested to make sure the build was actually pulling changes from that same project. Basically I added my string directly to the project instead of using a resource.
Try out following points they may work...
Check all activities if import android.R;is imported in any java file remove that import statement.
Then delete R.java file.
It may works, just check
Ok so I figured out the problem. If you are working on the OS source and you want to add a string to the string.xml file you MUST also add the following line of code to your public.xml file that exists inside your values folder.
<java-symbol type="string" name="my_string_resource_name" /> <!--In public.xml-->

Categories

Resources