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.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/background_material_light"android:gravity="center"android:orientation="vertical"tools:context=".SecondActivity"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:textAlignment="center"android:text="GeeksForGeeks-\nA ComputerScience 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"/><Buttonandroid: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"/><Buttonandroid: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;@OverrideprotectedvoidonCreate(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<activityandroid: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.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"tools:context=".MainActivity"><TextViewandroid: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"/><Buttonandroid: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;@OverrideprotectedvoidonCreate(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() {@OverridepublicvoidonClick(View v) {Intent intent =newIntent(MainActivity.this,SecondActivity.class);startActivity(intent);}});}}
Output:



