Adam
Adam

Reputation: 957

R.id.pager? Where is this defined?

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

Upvotes: 2

Views: 3775

Answers (2)

Salih Erikci
Salih Erikci

Reputation: 5087

It is defined in R.txt file under bin folder.

int id pager 0x7f05003c

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234847

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.

Upvotes: 9

Related Questions