How to add Slidr Library in Android?

In this layout, we will learn to add Slidr Library in android. This library is used to create attractive animation when user switch from one activity to another. This library can be used with both Activity and Fragment. It is very easy to implement. When Slidr is attached to the activity its interface return us two methods:-
Approach:
- Add the support Library in build.gradle file and add dependency in the dependencies section. This will help us to directly add themes and methods to add animations.
dependencies {Â Â Â Â Â Â Â Â ÂÂ Â Â Â Â Âimplementation 'com.r0adkll:slidableactivity:2.1.0'Â Â Â Â Â}Â Â Â Â Â Â Â Â Â - Now add theme for Slidr activity. Add these attribute to the theme as it is necessary for the Slidr to work properly.
styles.xml
<stylename="AppTheme.SlidrActivityTheme">       Â<itemname="android:windowIsTranslucent">true</item>       Â<itemname="android:windowBackground">@android:color/transparent       Â</item></style> - Now create a activity_second.xml file and add the following code. It will add a textview and two button in the layout. onClick attribute is added to the buttons which will invoke the lockSlide and unlockSlide methods respectively when clicked.
activity_second.xml
<?xmlversion="1.0"encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout   Âandroid:layout_width="match_parent"   Âandroid:layout_height="match_parent"   Âandroid:background="@color/background_material_light"   Âandroid:gravity="center"   Âandroid:orientation="vertical"   Âtools:context=".SecondActivity">   Â<TextView       Âandroid:layout_width="match_parent"       Âandroid:layout_height="wrap_content"       Âandroid:textAlignment="center"       Âandroid:text="GeeksForGeeks-\nA Computer               ÂScience Portal\nFor Geeks"       Âandroid:textColor="#219806"       Âandroid:textSize="30sp"       Âandroid:textStyle="bold"       Âapp:layout_constraintBottom_toBottomOf="parent"       Âapp:layout_constraintEnd_toEndOf="parent"       Âapp:layout_constraintStart_toStartOf="parent"       Âapp:layout_constraintTop_toTopOf="parent"/>   Â<Button       Âandroid:layout_width="wrap_content"       Âandroid:layout_height="wrap_content"       Âandroid:layout_margin="20dp"       Âandroid:layout_marginEnd="16dp"       Âandroid:onClick="lockSlide"       Âandroid:text="Lock this Screen"       Âapp:layout_constraintBottom_toBottomOf="parent"       Âapp:layout_constraintEnd_toEndOf="parent"/>   Â<Button       Âandroid:layout_width="wrap_content"       Âandroid:layout_height="wrap_content"       Âandroid:layout_margin="20dp"       Âandroid:layout_marginStart="16dp"       Âandroid:onClick="unlockSlide"       Âandroid:text="Unlock this Screen"       Âapp:layout_constraintBottom_toBottomOf="parent"       Âapp:layout_constraintStart_toStartOf="parent"/></androidx.constraintlayout.widget.ConstraintLayout> - Now create SecondActivity.java and add the following code. Here we create two methods lockSlide and unlockSlide which will lock the animation and resume the animation respectively.
SecondActivity.java
packageorg.zambiatek.gfgslidr;ÂÂimportandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.View;importcom.r0adkll.slidr.Slidr;importcom.r0adkll.slidr.model.SlidrInterface;ÂÂpublicclassSecondActivityextendsAppCompatActivity {   ÂSlidrInterface slidrInterface;   Â@Override   ÂprotectedvoidonCreate(Bundle savedInstanceState) {       Âsuper.onCreate(savedInstanceState);       ÂsetContentView(R.layout.activity_second);       ÂslidrInterface = Slidr.attach(this);   Â}   Â//It will lock the slidable touch interface.   ÂpublicvoidlockSlide(View v) {       ÂslidrInterface.lock();   Â}   Â//It will unlock the slidable touch interface.   ÂpublicvoidunlockSlide(View v) {       ÂslidrInterface.unlock();   Â}} - Now add the activity and theme in Manifest file. Here we add the SecondActivity and the theme for it.
Manifest.xml
<application<activity           Âandroid:name=".SecondActivity"           Âandroid:theme="@style/AppTheme.SlidrActivityTheme"></activity>Â</application> - Now add the following code in the activity_main.xml file. Here a textview and a button is added to the layout.
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout   Âandroid:layout_width="match_parent"   Âandroid:layout_height="match_parent"   Âandroid:gravity="center"   Âtools:context=".MainActivity">   Â<TextView       Âandroid:layout_width="match_parent"       Âandroid:layout_height="wrap_content"       Âandroid:layout_marginTop="256dp"       Âandroid:textColor="#219806"       Âandroid:textSize="30sp"       Âandroid:textAlignment="center"       Âandroid:text="Want to Learn Algorithm and Data Structure?"       Âapp:layout_constraintEnd_toEndOf="parent"       Âapp:layout_constraintHorizontal_bias="0.0"       Âapp:layout_constraintStart_toStartOf="parent"       Âapp:layout_constraintTop_toTopOf="parent"/>   Â<Button       Âandroid:id="@+id/button"       Âandroid:layout_width="wrap_content"       Âandroid:layout_height="wrap_content"       Âandroid:text="Open GeeksForGeeks"       Âandroid:textAllCaps="false"       Âandroid:textColor="#219806"       Âandroid:textStyle="bold"       Âapp:layout_constraintBottom_toBottomOf="parent"       Âapp:layout_constraintEnd_toEndOf="parent"       Âapp:layout_constraintStart_toStartOf="parent"       Âapp:layout_constraintTop_toTopOf="parent"       Âapp:layout_constraintVertical_bias="0.585"/></androidx.constraintlayout.widget.ConstraintLayout> - Now add the following code in the MainActivity.java file. Here onClickListener is added to the button and it starts SecondActivity when clicked.
MainActivity.java
packageorg.zambiatek.gfgslidr;ÂÂimportandroidx.appcompat.app.AppCompatActivity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;ÂÂpublicclassMainActivityextendsAppCompatActivity {   ÂButton openSite;   Â@Override   ÂprotectedvoidonCreate(Bundle savedInstanceState) {       Âsuper.onCreate(savedInstanceState);       ÂsetContentView(R.layout.activity_main);       ÂopenSite = findViewById(R.id.button);       Â//It will help to make a transaction to the Second Activity.       ÂopenSite.setOnClickListener(newView.OnClickListener() {           Â@Override           ÂpublicvoidonClick(View v) {               ÂIntent intent =newIntent(MainActivity.this,                      ÂSecondActivity.class);               ÂstartActivity(intent);           Â}       Â});   Â}}
Output:



