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

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

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      dependencies {         
            implementation 'com.r0adkll:slidableactivity:2.1.0'     
      }         

      chevron_right

      
      

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

      styles.xml

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

    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



      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

    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

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      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();
          }
      }

      chevron_right

      
      

    5. Now add the activity and theme in Manifest file. Here we add the SecondActivity and the theme for it.

      Manifest.xml

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <application
      <activity
                  android:name=".SecondActivity"
                  android:theme="@style/AppTheme.SlidrActivityTheme">
      </activity
      </application>

      chevron_right

      
      

    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

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

    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

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      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);
                  }
              });
          }
      }

      chevron_right

      
      

    Output:

    Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




    My Personal Notes arrow_drop_up


    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


    Article Tags :
    Practice Tags :


    2


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.