In this tutorial, we’ll demonstrate how to create your own open source application and use it as a dependency. You must have come across many open source libraries. Definitely, you would have used a few in your projects. Now it’s your turn to create one. We’ll be creating a library for vertical scrolling ViewPagers. Library name would aptly be ViewPagerVertical.
JitPack
JitPack is a package repository for JVM and Android Projects. It contains the aar and dependency jars of the libraries.
It’s a good practice to create a sample app along with the library.
Creating our Android Library
Let’s get started to create our Android Library by starting a new Android Studio project.
Choose the Tabbed Activity template, since it gives a default code for ViewPager in our activity.
Goto File | New Module | Android Library.
Enter the name of the module in the following screen. Let’s see how our new module looks in the project structure.
Android Library Project Structure
Let’s create a new java class in our viewpagervertical
module.
The code from the VerticalViewPager.java
class is given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
package com.journaldev.viewpagervertical; import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class VerticalViewPager extends ViewPager { public VerticalViewPager(Context context) { super(context); init(); } public VerticalViewPager(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { setPageTransformer(true, new VerticalPageTransformerAnimate()); setOverScrollMode(OVER_SCROLL_NEVER); } /** * Swaps the X and Y coordinates of your touch event. */ private MotionEvent swapXY(MotionEvent ev) { float width = getWidth(); float height = getHeight(); float newX = (ev.getY() / height) * width; float newY = (ev.getX() / width) * height; ev.setLocation(newX, newY); return ev; } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { boolean intercepted = super.onInterceptTouchEvent(swapXY(ev)); swapXY(ev); // return touch coordinates to original reference frame for any child views return intercepted; } private class VerticalPageTransformerAnimate implements PageTransformer { private static final float MIN_SCALE = 0.75f; @Override public void transformPage(View view, float position) { int pageWidth = view.getWidth(); int pageHeight = view.getHeight(); float alpha = 0; if (0 <= position && position <= 1) { alpha = 1 - position; } else if (-1 < position && position < 0) { float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position)); float verticalMargin = pageHeight * (1 - scaleFactor) / 2; float horizontalMargin = pageWidth * (1 - scaleFactor) / 2; if (position < 0) { view.setTranslationX(horizontalMargin - verticalMargin / 2); } else { view.setTranslationX(-horizontalMargin + verticalMargin / 2); } view.setScaleX(scaleFactor); view.setScaleY(scaleFactor); alpha = position + 1; } view.setAlpha(alpha); view.setTranslationX(view.getWidth() * -position); float yPosition = position * view.getHeight(); view.setTranslationY(yPosition); } } @Override public boolean onTouchEvent(MotionEvent ev) { return super.onTouchEvent(swapXY(ev)); } } |
Note: We’ve covered VerticalViewPager in the Nested ViewPagers Tutorial.
Add the Module dependency in our app module
To add the library module in our app module, go to File | Project Structure | Click the dependency Tab| Add New Module Dependency.
The following dependency should be added in your build.gradle
of the app
module.
1 2 3 |
implementation project(':viewpagervertical') |
Now in our MainActivity.java
class and activity_main.xml
of the app module, replace the ViewPager instances with VerticalViewPager
.
That’s it, our application is ready. Let’s check the output once before creating the library.
Publishing Android Library
To get our library production ready, we need to do the following changes in the Gradle.
Add the following dependency to the project level root build.gradle file.
1 2 3 |
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' |
In the library module build.gradle
file, add the following two lines after the top line apply plugin: 'com.android.library'
.
1 2 3 4 |
apply plugin: 'com.github.dcendents.android-maven' group='com.github.anupamchugh' |
In the group field replace the last string with your github username when you create your library.
Sync Changes!
Go Ahead and push your project to GitHub.
You can go to VCS | Import into version control | Share Project on GitHub.
Once it is done, create a release TAG for your repo.
Enter the following lines in the command line console present in Android Studio.
1 2 3 4 |
git tag -a 1.0 -m "v1.0" git push origin 1.0 |
Each time you do changes in the library, you need to update the version numbers in the above lines.
Typically version numbers are of form 1.0.x for minor bugs. 1.x.x for serious bugs/new features, 2.x.x for major changes.
Now the final stage!
Open JitPack, sign-in and search your repository by the url.
In our case it looks like this:
Clicking on Get would show you the dependency url. Follow the instructions below.
Typically you’ll need to add the following in the root build.gradle
.
1 2 3 4 5 6 7 8 |
allprojects { repositories { ... maven { url 'https://jitpack.io' } } } |
Replace implementation ‘module_name’ in your app’s build.gradle
with the following dependency.
1 2 3 4 5 |
dependencies { implementation 'com.github.anupamchugh:VerticalViewPager:1.0' } |
That’s it. We’ve created our first Android Library.
You can download the Android VerticalViewPager Project from the link below. Try modifying the VerticalViewPager and publish it as your library following the above set of instructions.