Android TypedArray of raw ids - android

Is there a way to add raw resource ids as items in a TypedArray. For example, if there is a resource res/raw/Foo.ext for a certain binary Foo.ext, is it possible to define a file res/values/arrays.xml that looks like:
<resources>
<array name="Foo">
<item name="id">#raw/Foo</item>
</array>
</resources>
Android Studio does not appear to show autocompletion for #raw/ as it does for #drawable/ for example. If possible, how could the item be retrieved programmatically?

Related

Dynamically add values to resource string-array in Android

Is there a way to dynamically add values to an Android resource string-array?
E.g.:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="titles">
</string-array>
</resources>
Is there a way to dynamically add values to an Android resource string-array?
No, because resources are read-only at runtime.
Yes! you can create a static array of strings in Android but dynamically adding values to an Android resource is not yet possible.
It turns out that it’s easy to create and use a static array of strings in Android. Of course, you can do this in Java code, as I describe in my Java string array tutorial, but for Android, I’m talking about doing this in XML.
In short, this is how you define a static string array in an Android XML file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="my_books">
<item>Scala Cookbook</item>
<item>Play Framework Recipes</item>
<item>How I Sold My Business: A Personal Diary</item>
<item>A Survival Guide for New Consultants</item>
</string-array>
</resources>
Then inside an Activity, Fragment, or other Java class, you can create a string array in Java from that XML like this:
Resources res = getResources();
String[] myBooks = res.getStringArray(R.array.my_books);

JavaDoc for Android XML (string) resources

Is there a way how to add comment/description/documentation to android resource reference? The only way I know is to use standart XML comment. Which is not ideal obviously.
Something like special attribute or special javadoc pre-element
<resources>
<string documentation="Some useful information what does this resource means..." name="KEY" translatable="false">value</string>
</resources>
<resources>
<documentation forName="KEY">Some useful information what does this resource means...</documentation>
<string name="KEY" translatable="false">value</string>
</resources>
Not ideal but currently functional way:
<resources>
<!-- Some useful information about what this resource means... -->
<string name="KEY" translatable="false">value</string>
</resources>
The following format allows for a file header (using XML documentation comments), as well as as an individual comment (a documentation attribute) that goes along with each resource entry. I haven't had the compiler complain that a specific namespace (other than xmlns:tools) that contains documentation is required.
<?xml version="1.0" encoding="utf-8"?>
<!--=============================================================================================
My File description here
==============================================================================================-->
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="Application_Name" documentation="Name of the application. Used as Activity Name as the title, and appears in About box">FantasticApp</string>
</resources>

Implementing a word list for use in an android application

I am developing an android application and would like to be able to create an array consisting of words from an xml file, but am unsure of how that would look. Any help is appreciated.
You can do this if you just need a preset list
In your res/values folder there should be an arrays.xml file if there isnt create it.
Then withiin the file add something like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="myarray">
<item>first item</item>
<item>second item</item>
<item>third item</item>
<item>foruth item</item>
</array>
</resources>
then do access it do this
String[] text = getResources().getStringArray(R.array.myarray);

android - How to create xml's id?

I'm developing a dynamic keyboard application using soft keyboard sample. My application changes layout. For example I have a keyboard(has one key) then I set up the app and I can use keyboard(has one key).
I want to create an XML file after compilation and use this file in the application (reading XML file with xmlpullparser or xmlresourceparser). However, keyboard class needs XML's id. How do I create an XML id?
It can be defined in ids.xml or any custom file like constants.xml inside your res\values folder.
Examples:
<resources>
<item type="id" name="keyboard" />
</resources>
and can be accessed in the code as following:
R.id.keyboard
You can define ids using an ids.xml file in your res/values directory. Here's an example:
<resources>
<item type="id" name="my_keyboard" />
</resources>
In code, you would set the id like so:
keyboardView.setId( R.id.my_keyboard );
XML files are compiled (binary XML) and thus you don't create them at runtime.
If you want to change your keyboard layout dynamically, you'll do that programmatically rather than in XML.

What is "android:" prefix mean in android framework-res module

I copy the this code from the styles.xml file in framework-res module
<style name="Theme">
<item name="colorForeground">#android:color/bright_foreground_dark</item>
<item name="colorForegroundInverse">#android:color/bright_foreground_dark_inverse</item>
<item name="colorBackground">#android:color/background_dark</item>
.
<style name="Theme.Black">
<item name="android:windowBackground">#android:color/black</item>
<item name="android:colorBackground">#android:color/black</item>
</style>
As you see, they all have a attribute name which's value is windowBackground. But the formar has a android: and the latter doesn't. Is it really necessary to write a android: prefix in android framework?
Found this to be an interesting question and tried exploring to find the answer.. This is what I found..
from: http://developer.android.com/guide/topics/resources/style-resource.html
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style
name="style_name"
parent="#[package:]style/style_to_inherit">
<item
name="[package:]style_property_name"
>style_value</item>
</style>
</resources>
item - Defines a single property for the style. Must be a child of a element.
attributes:
name
Attribute resource. Required. The name of the style property to be defined, with a package prefix if necessary (for example android:textColor).
from: http://developer.android.com/guide/topics/manifest/manifest-intro.html
Resource values
Some attributes have values that can be displayed to users — for example, a label and an icon for an activity. The values of these attributes should be localized and therefore set from a resource or theme. Resource values are expressed in the following format,
#[package:]type:name
where the package name can be omitted if the resource is in the same package as the application, type is a type of resource — such as "string" or "drawable" — and name is the name that identifies the specific resource. For example:
Values from a theme are expressed in a similar manner, but with an initial '?' rather than '#':
?[package:]type:name
And finally, I tried giving the attributes without android:, and it threw an exception, though it compiled successfully.
Accessing Platform Resources
Android contains a number of standard resources, such as styles, themes, and layouts. To access these resource, qualify your resource reference with the android package name. For example, Android provides a layout resource you can use for list items in a ListAdapter:
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myarray));
In this example, simple_list_item_1 is a layout resource defined by the platform for items in a ListView. You can use this instead of creating your own layout for list items. (For more about using ListView, see the List View Tutorial.)

Categories

Resources