I am trying to find the original android source code, where the android:text attribute from the layout files is resolved to the actual string referred to via @string/ (for instance: android:text=”@string/hello”). The reason is the following: I have created a completely different way of setting up my layouts using a custom XML which in turn uses pre-written Widgets, an example:
<dialog name="Homepage">
<field type="Combobox" label="Enter">
</dialog>
What I want to do: I want to be able to change the label attribute in my example to: label=”@string/enter” – so I can make use of the localization feature in android.
But in order to handle those @string commands, I need to know, where this is done in the original Android Source Code (on version 2.3 for instance) so I can imitate the behaviour. So far the only way I know of to obtain string resources is to use context.getString(int resID) – trouble is, how would I go about translating the String “@string/enter” to a res id? I assume I can’t, which is why i am curious about how android handles this.
I’d be really grateful if someone could point me in the right direction and I hope my explanation wasn’t too confusing 🙂 .
You can use
Resources.getIdentifier()for that.Small sample that gets the res id of
@string/enter:Given that the format for resources is always the same, you can parse all three arguments out of this string and fill them into
getIdentifier():from Accessing Resources