How create resource id without a resource - android

I need to associate several tags to a view so I use
view.setTag(id, tag_object)
Unfortunately Android requires to have the id as defined in a resource. However R file is auto generated of resource ids appearing in different resource files, so I do not know how to create an id detached from any resource. As work around I just use id of some resource but it isn't robust, because if I decide to remove the resource, the id can disappear. It is also reduces readability of the code having some weird id for addressing a tag. Perhaps I missed very simple trick as ids resource file.

There is a resource type "id" that lets you define arbitrary resource IDs:
http://developer.android.com/guide/topics/resources/more-resources.html#Id
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="foo"/>
<item type="id" name="bar"/>
</resources>
Will generate R.id.foo and R.id.bar.

You can simply declare Strings in your strings.xml file and use those id's. For readability purpose give them some good names. And don't use these strings somewhere else in your code or resources.

Related

When to use attrs.xml, when dimens.xml?

I like to integrate ads with AppLovin into my Android App.
In their documentation they say:
Declare the base banner height of 50dp in res/values/attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="banner_height">50dp</dimen>
</resources>
I also have a dimens.xml present with other <dimen... value's for my app. They work from there as well.
What is the difference between attrs.xml and dimens.xml? What to use in a situation like this?
attrs.xml is a file that allows you to define custom attributes for your views in XML layout files. These attributes can be used to customize the appearance and behavior of your views, and can be accessed programmatically in your Java code.
dimens.xml on the other hand, is a file that allows you to define dimension values for use in your app. These values can be used to set the size and layout of views in your XML layout files, and can also be accessed programmatically in your Java code.
In this situation, you should use dimens.xml to define the banner_height dimension, because you will use this value to set the height of the banner ad view in your layout.
You could also use attrs.xml to define the banner_height attribute, but since you are defining a dimension value, not an attribute, it would be better to use dimens.xml instead.
It doesn't actually matter - <attr> and <dimen> are both value resources, and you can put them in any file in the res/values folder. They'll all be combined into the same set of resources, so the actual file you use is up to you! So you can do this if you want:
# res/values/whatever.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="someDimension">48dp</dimen>
<attr name="someColour" format="color"></attr>
</resources>
and it'll work exactly the same as if you used attrs.xml and dimens.xml. It's just convention to put attrs and declare-styleable things in a file called attrs.xml, all your dimension resources in dimens.xml, all your strings in strings.xml...
But you don't have to do that, it's up to you! For example, you might want certain resource strings to be stored in a different file, for organisation (maybe they shouldn't be translated). Or maybe some values are common to all configs and you want those in a single file, and you want a separate file for the qualified stuff (that goes in folders based on API level, night theme etc)

Which approach is better for creating an Android alias resource?

When studying about Android alias resources, I encountered different approaches for creating an alias and determining its destination type.
In the first approach, as described here, we determine the destination type via a type attribute inside an <item> tag. For instance, here we declare that the alias is pointing to a layout resource:
<resources>
<item name="main" type="layout">#layout/main_twopanes</item>
</resources>
In the second approach, as described here, we determine the destination via a specific tag (instead of <item>). For instance, here we declare that the alias is pointing to a drawable resource:
<resources>
<drawable name="icon">#drawable/icon_ca</drawable>
</resources>
Or, where things get more complicated, here we set the destination to be a layout:
<merge>
<include layout="#layout/main_ltr"/>
</merge>
What is the difference between these approaches?
Are they identical, or differ in a way that I'm missing?
When should I prefer one over the other?

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.)

Android .xml files: Why do predefined colors not work for me?

When I look at misc. Android tutorials and examples when it comes to specifying colors I very often see constants like #color/red or #color/black etc. being used. For some strange reason that NEVER works for me! I always need to specify colors using the "#RGB", #ARGB, ..., #AARRGGBB notation.
As soon, as I try to use any of those mnemonic constants like e.g. "#color/red" I am getting error messages like these:
[...] C:\Users\mmo\Test\res\drawable\edit_text.xml:5: error: Error: No resource found that matches the given name (at 'drawable' with value '#color/orange').
[...] C:\Users\mmo\Test\res\drawable\myDrawable.xml:3: error: Error: No resource found that matches the given name (at 'drawable' with value '#color/black').
[...] C:\Users\mmo\Test\res\drawable\myDrawable.xml:4: error: Error: No resource found that matches the given name (at 'drawable' with value '#color/black').
[...] C:\Users\mmo\Test\res\drawable\myDrawable.xml:5: error: Error: No resource found that matches the given name (at 'drawable' with value '#color/green').
[...] C:\Users\mmo\Test\res\drawable\myDrawable.xml:6: error: Error: No resource found that matches the given name (at 'drawable' with value '#color/black').
Why is that so? Why can't I use these predefined constants? Do I need to prefix them with some package name (I tried #android:color/red but that only caused a different error)?
Do I need to specify these colors myself? If so: how and where? Any ideas or suggestions?
Michael
If you want to use the colors pre-defined in the Android platform, the syntax is #android:color/white. The android: at the beginning indicates that the resource is not part of your application.
Is "colors.xml" added to your res/values folder where these color constants are defined?
Color XML file is within the values folder where it must contain color values.within resources tag.
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<color name="green">#00ff00</color>
Make sure your color XML file is within the values folder, not a colors folder.
So you should have...
values/colors.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<color name="red">#FF0000</color>
</resources>
and NOT this...
color/colors.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<color name="red">#FF0000</color>
</selector>
Note that the tag is resources, not selector.
strangely Android does not provide a decent list of colors. And I say strangely because during my 30 years career this is the first language I met that does not do that. And that despite that is built on Java which defines colors in all it's basic libraries.
The ones that are defined are prefixed so you will not find them :)
To find them (if using eclipse ) go to the xml doc where you need the color
type android:background="#android:color/ and do a Ctrl Space.
On my version (current as we speaking) I get more than a dozen.
for instance: holo_orange_dark
So, use that or complain so Google fixes this issue. And I call it issue because it makes no sense to force all developers to manually describe all colors and values.
An important part of this that no one else has mentioned is that the reference to the color has to be
#color/black
but the xml file has to be
colors.xml
(note plural in the xml file name but not plural #color)

Categories

Resources