Open In App

How to add Slidr Library in Android?

Improve
Improve
Like Article
Like
Save
Share
Report

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:-

  • SlidrInterface.lock()
  • SlidrInterface.unlock();
  • Approach:

    1. 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'     
      }         

      
      

    2. Now add theme for Slidr activity. Add these attribute to the theme as it is necessary for the Slidr to work properly.

      styles.xml




      <style name="AppTheme.SlidrActivityTheme">
              <item name="android:windowIsTranslucent">true</item>
              <item name="android:windowBackground">@android:color/transparent
              </item>
      </style>

      
      

    3. 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




      <?xml version="1.0" encoding="utf-8"?>
      <androidx.constraintlayout.widget.ConstraintLayout
          xmlns:tools="http://schemas.android.com/tools"
          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>

      
      

    4. 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




      package org.geeksforgeeks.gfgslidr;
        
      import androidx.appcompat.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.View;
      import com.r0adkll.slidr.Slidr;
      import com.r0adkll.slidr.model.SlidrInterface;
        
      public class SecondActivity extends AppCompatActivity {
          SlidrInterface slidrInterface;
        
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_second);
        
              slidrInterface = Slidr.attach(this);
          }
        
          //It will lock the slidable touch interface.
          public void lockSlide(View v) {
              slidrInterface.lock();
          }
        
          //It will unlock the slidable touch interface.
          public void unlockSlide(View v) {
              slidrInterface.unlock();
          }
      }

      
      

    5. 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>

      
      

    6. 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




      <?xml version="1.0" encoding="utf-8"?>
      <androidx.constraintlayout.widget.ConstraintLayout
          xmlns:tools="http://schemas.android.com/tools"
          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>

      
      

    7. 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




      package org.geeksforgeeks.gfgslidr;
        
      import androidx.appcompat.app.AppCompatActivity;
      import android.content.Intent;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
        
      public class MainActivity extends AppCompatActivity {
        
          Button openSite;
          @Override
          protected void onCreate(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(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      Intent intent = new Intent(MainActivity.this,
                             SecondActivity.class);
                      startActivity(intent);
                  }
              });
          }
      }

      
      

    Output:



    Last Updated : 03 Aug, 2021
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
    Similar Reads