I am creating views programmatically and later want to call 'addRule' and pass their id to this function.
Is it safe to assign ids programmatically?
How do I know that I am not overriding existing ids?
Is there a resource type '0' or can I presume that any resource id I create of the format 0xNNTTNNNN where 'TT' is the type = 0 is ok?
EDIT:
Use View.setId (int id)
Quote:
Sets the identifier for this view. The identifier does not have to be
unique in this view's hierarchy. The identifier should be a positive
number.
You can define some ids in an xml file, say, ids.xml in the res/values directory and then you can assign these ids either programmaically or by referring to them in other xml files (say, in your layout files). This way you can, for example, guarantee that a certain view will have the same known id in all the layouts where you want it to appear
You define an id as follows:
<resources>
<item type="id" name="myid" />
</resources>
And refer to it as usual "#id/myid"
Related
I am new to this. wanted to know if there is a way to assign R.id to dynamically created edit text so i can move the data with in to SQLite db. tried few diff ways but fail. Any help would be appreciated. Thanks
In android id should be unique. Setting any arbitrary integer value can lead to duplicate ids. The correct way is to define id like below.
Create a new xml file named ids.xml inside res/values folder.
Add new item like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="button_group_cancel" />
</resources>
Now you can set the id to edittext as:
edittext.setId(R.id.button_group_cancel);
You can assign an id using .setId(int) before you add the EditText to the layout. However this does not place the id into the R.java file.
You can reference the EditText with findViewById(int) instead of using findViewById(R.id.editTextId), where int is the same int used in the set method.
I am not getting what Author wants to explain , so please explain this , "Use #+ on the first occurrence of a given android:id in a layout (XML) file. It might be in the definition of the view or it might be in a reference - which ever is first. In the example above, the EditText view is defined before the Button. So the #+ is used on the android:id attribute in EditText. However, if the Button was defined first, the #+ would be used on the relative layout positioning in the Button" ,
..
Read more: http://www.intertech.com/Blog/Post/Android-Layout-and-ID-Attribute.aspx#ixzz2MHHdt1wv
#+ means, that if this id doesn't exists yet, it will be created, otherwise already created id will be used. If you look at R.java - ids is some numeric constants.
If you write just # - you should be sure, that id already created.
Take a look here. It explains a lot, take a look at the ID section.
The at-symbol (#) at the beginning of the string indicates that the
XML parser should parse and expand the rest of the ID string and
identify it as an ID resource. The plus-symbol (+) means that this is
a new resource name that must be created and added to our resources
(in the R.java file). There are a number of other ID resources that
are offered by the Android framework. When referencing an Android
resource ID, you do not need the plus-symbol, but must add the android
package namespace, like so:
android:id="#android:id/empty"
I have a bunch of Views in a <merge>, and I included that <merge> into a RelativeLayout. I try to refer to the IDs of those included Views to act as anchors for my other Views, but Eclipse complains that the IDs are not resolving. I found a workaround by using #+id rather than #id when I first refer to them rather than when I actually define the objects they refer to. I've already defined the two IDs in a Style and in the included <merge> where they are declared, so it feels a bit inefficient if I keep repeating the definition of the ID.
Is this the correct way of doing it? I'm assuming it's bad cause the '+' is another initialization. My current hypothesis is that you should use #+id when you first use the ID rather than when you initialize the object that the ID is going to represent, a bit like C/C++ and how they require at least a function prototype in the lines prior to the actual code that uses the function.
Another question I have is when you use the GUI-based UI builder of Eclipse, I noticed that they always use #+id rather than #id. Is this acceptable, cause it seems inefficient to me; it's as if the application will be spending more time determining whether or not the ID has been declared in R.id.
Using #+id format tells the Android asset compiler to assign an ID to your element, it isn't actually an id itself. So if I use #+id/myNewId the asset compiler will create a new id named myNewId and provide a number for it. The actual number can be accessed from your code as R.id.myNewId.
If you use an #id, the compiler will look for R.id.id. You can define your own id's in XML files, as explained here: http://developer.android.com/guide/topics/resources/more-resources.html#Id. You could create your own file in res/values/[your_filename].xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item
type="id"
name="id_name" />
</resources>
and then refer to #id_name, for e.g.
You can also use the Id's defined in the Android namespace: #android:id/empty
This is well explained in the Android documentation: http://developer.android.com/guide/topics/ui/declaring-layout.html#id
There's also some further discussion here: android:id what is the plus sign for
Is it possible to get android to give me a custom id?
so for example if I already have defined in xml:
R.id.some_layout
R.drawable.some_drawable
is there any function like this
R.custom_id("a_custom_id")
so I could then access as
R.id.a_custom_id
You can not dynamically create new IDs. Even if R was capable of doing so, you wouldn't be able to access it using R.id.a_custom_id. Java is not dynamic language, and cannot add fields at runtime.
There is, however, compile-time solution. In your res/values/ids.xml add:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="a_custom_id"/>
</resources>
And then you can reference R.id.a_custom_id in your code and "#id/a_custom_id" in xmls. Of course its still pre-defined id (as opposed to runtime-defined id).
You can create boolean, integer, dimension, color, and other array resources.
http://developer.android.com/guide/topics/resources/more-resources.html
Can someone clarify the difference between android:id, android:name and name tags in Android's XML files. They all seem to be ways to reference things.
For example, when I have a string array in the res/values/array.xml file I access using the name field in the defined array yet the Javadoc refers to this as the "ID".
android:id seems to be just used in Views ?
Am I missing something or would it not be simpler to have one tag?
No, I don't believe you're missing anything. Although these fields are named differently, it's my understanding that they are both used to identify/reference resources. This specifically means GUI elements (views) in the case of android:id and static resources in the case of name.
To go into more depth, I believe the android:id attribute is assigned only to Views and classes that extend View. This is done so that the view can be programmatically accessed from your code using findViewById:
Button myButton = (Button) findViewById(R.id.whatever_id_assigned_to_view)
This is different from resources such as strings.xml or array.xml which are identified simply by name, such as a following example of what might be found in strings.xml:
<string name="string_name">Text Resource Here</string>
and is accessed using...
getResources().getText(R.string.string_name)
I imagine these are separated for organizational reasons. This way the generated Android resource file (R.java) contains the IDs specified for views in R.id, the string IDs contained in R.string, array IDs in R.array etc.