DevCNairobi
Published in

DevCNairobi

Create Swipeable Videos like Tiktok

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!

--

--

--

Developer Circle: Nairobi

Recommended from Medium

Reduce AWS cost with a Graviton EKS cluster

Cache Stampede

How To List Every File in a Directory in Python

What did you do this past week?

JS Back-End Week 2 Interview Questions

A Brief on GIT

Elixir vs Ruby: Which is Faster?

Adaptive apps using Flutter

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Licio Lentimo

Licio Lentimo

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

More from Medium

Link C/C++ library dependencies to your own C/C++ code in an Android application using CMake

How to launch an Instant app to production on the Google Play Store

Google Instant app — How to publish it

Should Android App Developers Take Note Of Google Fuchsia?

Should Android App Developers Take Note Of Google Fuchsia?

How to Beautify and Convert the User’s Voice Using the Agora Android SDK