Kotlin ViewPager & TabLayout

In this tutorial you will learn about the Kotlin ViewPager & TabLayout and its application with practical example.

Kotlin ViewPager & TabLayout

ViewPager: This is a layout manager which allows users to swipe left and right through pages of data. In a sense, it is nice way to show the user multiple tabs. We can also add and remove pages(tabs) dynamically. Let’s get more understand about ViewPager.

Table Of Contents

Class Declaration : The ViewPager class is a subclass of ViewGroup. Below is the class declaration.

XML Declaration: We have to declare ViewPager in the XML then we can access this.

Constant Summary: There are some frequently used constant are listed below.

1.int SCROLL_STATE_DRAGGING: This constant indicates that the pager is being dragged by user.

2.int SCROLL_STATE_IDLE: This constant of ViewPager indicates that the pager is in idle state.

3.int SCROLL_STATE_SETTLING: This constant of ViewPager indicates that the state of ViewPager is in process of settling to a final position.

 

Method Summary: There are some commonly used methods are listed below.

1.PagerAdapter getAdapter(): This method is used to retrieve current adapter so that user can supply pages.

  1. void setAdapter(PagerAdapter adapter): This method is used to set a Pager Adapter that will supply views for this pager as needed.
  2. void setCurrentItem(int item): This method is used to set the currently selected pages.
  3. void setPagerMargin(int marginPixel): This method is used to set margin between pages.
  4. void setoffscreenPageLimit(int limit): This method is used to set number of pages.

 

TabLayout: The TabLayout provides a horizontal layout to display tabs. We can display more screens on a single screen using tabs. We can create sliding as well as non sliding tabs using TabLayout. Let’s get more understand about TabLayout.

Class Declaration:

Constant Summary: Some frequently used Constant are listed below.

1.int GRAVITY_CENTER: This constant is used to keep the tabs in the center of the TabLayout.

2.int GRAVITY_FILL: This constant is used to fill the TabLayout as much as possible.

3.int MODE_FIXED: This constant is used to fixed the tabs concurrently.

4.int MODE_SCROLLABLE: This constant is used to Scrollable tabs display a subset of tabs at any given moment and used to contain longer tabs labels and larger number of tabs.
Constructor Summary: Some public constructor are used in TabLayout which are listed below.

1.TabLayout(Context context).
2.TabLayout(Context context, AttributeSet attrs).
3.TabLayout(Context context, AttributeSet attrs, int defStyleAttr).

Method Summary: The most commonly methods are listed below, that will let you get more awareness about TabLayout.

1.void addTab(TabLayout.Tab tab, boolean setSelected): This method is used to add a tab to this Layout with the property of selection.

  1. void addTab(TabLayout.Tab tab, int position): This method also used to add a tab to this layout but at given position.
  2. void addTab(TabLayout.Tab tab): This method simply add a tab to this layout.
  3. void addTab(TabLayout.Tab tab, int position, boolean setSelected): This method is used to add a tab to this layout with given position and with selection status.

    5.void addOnTabSelectedListener(TabLayout.OnTabSelectedListener listener): This method is used to invoke TabLayout.OnTabSelectedListener when tab selection changed.

  4. void clearOnTabSelectedListener(): This method simply removes all TabLayot.OnTabSelectedListeners which were previously added.
  5. TabLayout.Tab getTabAt(int index): This method returns the tab at the specified position.
  6. int getTabCount(): This method is used to returns number of tabs which are currently registered with action bar.

9.TabLayout.Tab newTab(): This method is used to create and return a new tab in TabLayout.Tab.

  1. void removeAllTabs(): This method is used to remove all tabs from the layout.
  2. void removeTabAt(int position): This method is used to remove a tab from the layout at specified position.
  3. void setTabGravity(int gravity): This method is used to set the gravity to use when laying out the tabs.
  4. void setTabMode(int mode): This method is used to set the behavior mode for the Tabs in this layout.
  5. void setTabTextColors(int normalColor, int selectedColor): This method is used to set the color of Tab’s Text when normal and when selected.
  6. void setupWithViewPager(ViewPager pager): This method is used to set TabLayout with ViewPager.

 

 

So this was about ViewPager and TabLayout, Click on Next for Example of TabLayout with ViewPager.

In this tutorial we have learn about the Kotlin ViewPager & TabLayout and its application with practical example. I hope you will like this tutorial.