Kotlin : Android’s future is here – Part 6

Overview

View Pager

This blog is about implementing view pager in Android using Kotlin.
Most of the implementation of view pager in Kotlin is just same as that of in Java.

View Pager without fragments

Including view pager in xml

Row/Page file of View Pager

Here, we have a text view in center of page which will display the page number as we swipe the view pager

Implementing Pager Adapter

The name of this adapter is CustomAdapter. It works exactly in same way as that written in Java. There is just a change of language.

Assigning adapter to View Pager in MainActivity

Note:- internal keyword behaves like default in java. It is a good practice to assign internal to functions requiring no access modifier as by default they will be final.

View Pager with fragments

Including view pager in xml

Create the fragments that you want to display inside the view pager

Here, we will display different colored background for each fragment in view pager.
I’ll show one fragment here, for instance.

XML file:-

Kotlin file:-

Implementing FragmentStatePager Adapter

Assigning adapter to View Pager in MainActivity

Conclusion

View Pagers are necessary for the apps where tab layouts are used. They simplify the interface making it user friendly. Coding view pagers in Kotlin is not much different than in Java.

mm

Sumeet Rukeja Android Developer

I am an Android Developer at Yudiz Solutions Pvt. Ltd. - a leading Mobile App Development Company. I am passionate about developing android apps and to learn more and more about the deep sea of Android.

Comments are closed.

Top