Search code examples
javaandroidandroid-viewpagerviewpagerindicator

View pager indicator doesn't appear - Android


I'm using this library to create an indicator for a viewPager (in Android). I simply want to show to the user that he can change the view. For some strange reason, even when I create the view and set it up, it doesn't show. Here's the code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    viewPager = (ViewPager) findViewById(R.id.instructionViewPager);
    adapter = new InstructionPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(adapter);

    ViewPager.OnPageChangeListener listener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    viewPager.addOnPageChangeListener(listener);

    ViewPagerIndicator viewPagerIndicator = (ViewPagerIndicator) 
    findViewById(R.id.view_pager_indicator);
    viewPagerIndicator.setupWithViewPager(viewPager);
    viewPagerIndicator.addOnPageChangeListener(listener);

And I have it in the .xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical">

    <TextView
        android:id="@+id/biometricTextLabel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="84dp"
        android:layout_marginRight="84dp"
        android:layout_marginTop="38dp"
        android:text="Biometric"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:textSize="18sp" />


    <android.support.v4.view.ViewPager

        android:id="@+id/instructionViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/view_pager_indicator"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/biometricTextLabel"
        android:layout_marginBottom="0dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="10dp" />

    <Button
        android:id="@+id/instructionbContinue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/instructionbBack"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="12dp"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp"
        android:text="Continue"
        android:textColor="@android:color/background_light" />

    <com.github.vivchar.viewpagerindicator.ViewPagerIndicator
        android:id="@+id/view_pager_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:delimiterSize="8dp"
        app:itemSize="8dp"
        android:layout_above="@id/instructionbContinue"
        android:layout_marginBottom="30dp"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/instructionbBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:background="@android:color/transparent"
        android:text="Go back"
        android:textColor="@android:color/white" />

</RelativeLayout>

What am I doing wrong?

EDIT: I've added all the xml of the layout.

EDIT 2: I add the pageAdapter, it's simple:

public class InstructionPagerAdapter extends FragmentPagerAdapter {

List<Fragment> fragments;

public InstructionPagerAdapter(FragmentManager fm) {
    super(fm);
    this.fragments = new ArrayList<Fragment>();
}

public void addFragment(Fragment fragment) {
    this.fragments.add(fragment);
}

@Override
public Fragment getItem(int arg0) {
    return this.fragments.get(arg0);
}

@Override
public int getItemPosition(Object object) {
    return POSITION_UNCHANGED;
}

@Override
public int getCount() {
    return this.fragments.size();
}

}


Solution

  • It seems you have not added any fragment to your adapter yet. If you are adding the fragment to you adapter after assigning adapter to your viewpager, then try to notify your adapter.