Create Swipeable Videos like Tiktok

Licio Lentimo
DevCNairobi
Published in
2 min readJul 11, 2020

Recently I’ve been experimenting with ViewPager2 which is an improved version of the earlier ViewPager library for Android. ViewPager2 is built on top of RecyclerView and supports vertical orientation as well as right-to-left enabled automatically. It allows us to create beautiful user interfaces when we swipe between lists for example. In this post, we will create swipeable vertical videos like the one used in Tiktok. Let’s get started!

Create a project and add dependencies

Create a new project with an Empty Activity and give it a name. We will add the Material Library dependency to the build.gradle file.

implementation 'com.google.android.material:material:1.1.0'

Change theme

Next, we will need to change the default theme to support fullscreen and get rid of the ActionBar.

We will then add this theme to our AndroidManifest.xml as the default for our activity. In addition, add the INTERNET permission as well.

Creating the layout

Head over to your activity_main.xml and add ViewPager2 layout to your layout.

Next, create a new layout resource file and give it a name. This will host the layout we will use for our app.

Create the model class

We are now done with the layout design. We now need to create the logic of our application by creating a model class that will hold values of the video title, description and url that we gave ids to in the layout. Create a new Java class and give it a name. Add the following code:

Add an Adapter class

We now need to add an adapter class to bind our model class to the layout since our videos are in list format. If you are familiar with RecyclerView this should be nothing new.

MainActivity

Now let us write code for our MainActivity. Here we will hold our videos in an Arraylist and populate them manually as opposed to fetching them from an API.

The outcome should be similar to this:

Outcome of the app

I will use videos from Facebook Developer Cirles:Nairobi for my examples. And that’s it! You can now create Tiktok-style swipeable videos and add them in your Android apps. You can check out the repository on Github here.

Happy coding!

--

--

Licio Lentimo
DevCNairobi

I write content on Android and Web technologies. Currently focusing on Cybersecurity. Find me on liciolentimo.com