2

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

Adam
  • 959
  • 10
  • 23
  • have you tried `android.R.id.pager`? if that doesn't work, then try this approach. http://stackoverflow.com/questions/7723964/replace-fragment-inside-a-viewpager. If you want to know why, I can post an explanation. – edthethird Jul 20 '12 at 01:47

2 Answers2

9

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.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

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

int id pager 0x7f05003c
Salih Erikci
  • 5,076
  • 12
  • 39
  • 69