‫Hello, my friends, and welcome back.
‫In this section we're going to learn about Viewpager in Android.
‫Viewpager combined with fragments is a powerful UI pattern in Android that allows you to create swipeable
‫screens or tabs where each screen or tab contains a separate fragment.
‫This is commonly used for building apps with multiple views or sections that the users can navigate
‫through swiping horizontally, similar to a gallery or series of pages exactly like this application.
‫It's also used to guide the users through the app.
‫When the user launches the app for the first time up in Android studio, create a new project, select
‫Empty Views activity, and I'll name it as Viewpager app Click finish.
‫The first step is to create the Viewpager widget and the Viewpager is a UI widget that allows you to
‫horizontally scroll through a series of views or fragments.
‫For that, I'll start with the main activity dot XML file.
‫I remove this text view and I'll add the Viewpager here.
‫View pager and select Viewpager two.
‫This is the latest library of the Viewpager Click enter and you can see it's from Android Viewpager
‫two dot widget package Matchparent Matchparent.
‫And you can see it.
‫This is the Viewpager widget In further constraints.
‫Just click on this button and here I'll give it an ID Viewpager two.
‫So this is how we create the Viewpager widget in Android and for better implementation without any problem
‫for the Viewpager two library, we need to add the following Android dependency to the your project's
‫Build.gradle file Select Kotlin and copy this line.
‫This implementation go to Gradle and here in the dependencies scope, I'll paste the implementation
‫of the Viewpager two library Click sync.
‫Now the second step is to create the fragments.
‫So the fragments represents modular and reusable parts of your app's UI or functionality.
‫Each fragment can contain its own user interface, and behavior fragments are especially useful for
‫creating Swipeable sections within a viewpager.
‫And in this tutorial I'll create three fragments, three blank fragments to the project.
‫I'll create new blank fragment and I'll name it as fragment one click finish.
‫Remove all of these codes and keep the constructor and the Oncreate view method.
‫This is the default constructor and the Oncreate view is a method that it is responsible for inflating
‫or parsing or displaying their respective XML file which is the fragment underscore one.
‫And this is the layout.
‫And this method returns the view which is added to the Viewpager adapter.
‫So it's essential for inflating and displaying this layout.
‫I'll edit the fragment, underscore one layout and you can see this is the text view and I'll name it
‫as fragment number one.
‫And I'll add a background color for this layout and I'll add this color.
‫This is fragment number one.
‫I'll create another fragment.
‫Called to.
‫New fragment.
‫Blank.
‫And I'll name it.
‫Again.
‫Remove unnecessary codes, keep the constructor and the Oncreate view method.
‫Edit the fragment underscore to layout.
‫And I'll add this color again.
‫And the last fragment.
‫Create a new fragment.
‫Blank three, and I'll remove these unnecessary codes.
‫Till now.
‫We're good.
‫We created our fragments, layouts.