![]() ![]() A unique resource name for the element, which you can use to obtain a reference to the ViewGroup from your application.” - Android Developers: Guide - Layout Resource When the app is compiled, this ID is referenced as an integer, but the ID is typically assigned in the layout XML file as a string, in the id attribute.” - Android Developers: Guide - Declaring Layout “ID - Any View object may have an integer ID associated with it, to uniquely identify the View within the tree. Let’s revisit the TextView that I talked about earlier in this article: Why do we add the prefix to the value we assign to every “android:id” XML attribute? I had coded String, Style, and Color resources before, but had never been instructed to code ID resources. It turns out that an ID is a simple resource type. You can use any simple resource in more than one layout, menu, or any other more complex resource.Īt the time of this writing, the simple resources are: Bool, Color, Dimension, ID, Integer, Integer Array, String, Style, Typed Array. I’d like to add to that definition: Simple resources are the reusable building blocks of other resources in your app. The Android Developers docs define a simple resource as a “resource that is referenced using the value provided in the name attribute (not the name of the XML file)” ( Reference). While looking through the various resource types, I noticed that the documentation classified some of these resource types as “simple resources”. Simple Resources - The building blocks of your app’s XML-based resources. ![]() Why do we add the prefix to the value we assign to every android:id XML attribute? If we’re not providing a raw string as the value, then what are we providing? Does that prefix do anything special? However, there must be a really specific reason why Android Studio and every tutorial I had come across prompted me to add that prefix every time I write code for a new View or ViewGroup directly in a layout’s XML file. I thought that perhaps the prefix tells Android somehow that the raw string immediately following was the ID itself, and that the ID couldn’t have any spaces in it (just like with HTML ID values). I figured that because I’m adding unique instances of Views to layouts, I’m also naturally adding a new string as an ID. The prefix seemed unnecessary at first glance because an ID for anything is supposed to be unique, by definition. The difference, however, is that we must add the prefix And, there are times where we don’t include the plus ( +) symbol. We type a string as the value of a given View or ViewGroup’s android:id XML attribute. Similarly, it seemed just as straightforward to assign an ID to a new View or ViewGroup element in a layout file. ![]()
0 Comments
Leave a Reply. |