R.id.pager? Where is this defined? - android

The example for viewpager, here, contains the lines:
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
I don't understand how R.id.pager is defined. Am I supposed to create a viewpager in xml somewhere? But that wouldn't make sense because it's instantiating a viewpager in the previous line. If someone could clear this up for me I would be most grateful!!
Thank you!!
EDIT
Apparently changing the line to:
mViewPager.setId(1);
Makes it work :) :)

You can define the id directly in a resource file in the res/values directory. The name of the file is irrelevant, but it could look something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item
type="id"
name="pager" />
</resources>
IDs can also spring up automatically in layouts when you set an id for a layout element using an attribute like this:
android:id="#+id/pager"
The + in the attribute says to add the indicated id to R.id if it isn't already there.
It's better to use an XML-defined ID rather than hard-coding a value into your code, for the same reason that it is better to use symbolic constants (e.g., final static int FOO = 1;) rather than sprinkling integer literals everywhere.
See the docs on ID resources for more info.

It is defined in R.txt file under bin folder.
int id pager 0x7f05003c

Related

How can we set id as a number for UI Elements?

I am quite interested and confused both. Sometimes ago I was working on a project who has already implemented by some one. And after worked on that i found that he was using a unique number for find ID for its UI elements. Just like if we need to find id for TextView then we write,
TextView textview = (TextView)findViewById(R.id.text);
but in that code it was like,
TextView textview = (TextView)findViewById(012345678);
something like this.I have searched a lot in project also tried out to find that number but i couldn't. In R.java file i have also checked but there was also not. Its unique id its generated automatically as usual but i couldn't find a number in whole project.
So my question how can we use like this way? Is this possible??
TextView textview = (TextView)findViewById(012345678);
If this is possible then where we can define that id number and use it for UI elements.
Help will be appreciated!!
from folder gen you can find R.java there is a class named id like in image.
for example
public static final int action_bar=0x7f06001c;
so instead of R.id.action_bar i can use 0x7f06001c.
That is not directly possible as you might think. You can define your own custom ids which are not used in any xml files. I think that is what you want. To do this create a new xml files in res/values/ids.xml with this content:
<resources>
<item name="your_custom_id" type="id"/>
</resources>
Than you can use it as R.id.your_custom_id I think that is what you want.

Assign R.id to dynamically created Edit Text

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.

When should you use `#+id` instead of `#id`?

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

Android: Possible to get a custom R.id

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

Difference between android:id, android:name and name tags in Android XML files

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.

Categories

Resources