Swipe Lock In Android Using Android Studio

Introduction

 
In this article, we are going to learn how to add Swipe Lock functionality to an Android application using Android Studio. It is a type of library that locks the screen. This kind of lock is used in all smartphones.
 
Step 1
 
Create a new project in Android Studio.
 
Android
 
Give a name to the project and click "Next".
 
Android
 
Select the "Phone and Tablet" option and click "Next".
 
Android
 
Select an empty activity and click "Next".
 
Android
 
At last, give the activity name and click on "Finish".
 
Android
 
Step 2
 
Next, locate app>>res>>drawable folder and Create new vector assets.
 
Android
 
Then, choose the lock open icon from the list:
 
Android
 
Step 3
 
Repeat Step 2 to choose the closed lock icon from the list.
 
Android
 
Check whether it is done correctly.
 
Android
 
Step 4
 
Locate the Gradle Scripts>>Build. Gradle.
 
Android
 
And, type the following dependency in your app's build.gradle.
 
Android
 
The code copy is here.
  1. compile 'com.ebanx:swipe-button:0.4.0'
Step 5
 
Next, go to app >> res >> layout >> activity_main.xml. Select activity page.
 
Android
 
Just type the code as following.
 
Android
 
The code copy is here.
  1. <com.ebanx.swipebtn.SwipeButton  
  2.         android:id="@+id/swipe"  
  3.         android:layout_width="match_parent"  
  4.         android:layout_height="wrap_content"  
  5.         android:layout_marginStart="20dp"  
  6.         android:layout_marginEnd="20dp"  
  7.         android:layout_centerInParent="true"  
  8.         app:inner_text="SWIPE TO UNLOCK"  
  9.         app:inner_text_color="@android:color/white"  
  10.         app:inner_text_size="16sp"  
  11.         app:inner_text_bottom_padding="18dp"  
  12.         app:inner_text_background="@drawable/shape_rounded"  
  13.         app:button_image_disabled="@drawable/ic_lock_open_black_24dp"  
  14.         app:button_image_enabled="@drawable/ic_lock_outline_black_24dp"  
  15.         app:button_left_padding="20dp"  
  16.         app:button_right_padding="20dp"  
  17.         app:button_background="@drawable/shape_button"  
  18.         />  
Step 6
 
Next, go to app >> java>>Mainactivity.java. Select Mainactivity page.
 
Android
 
Just type the code as follows
 
Android
 
The code copy is here.
  1. @Override  
  2.     protected void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         setContentView(R.layout.activity_main);  
  5.   
  6.         SwipeButton swipeButton = (SwipeButton)findViewById(R.id.swipe);  
  7.         swipeButton.setOnStateChangeListener(new OnStateChangeListener() {  
  8.             @Override  
  9.             public void onStateChange(boolean active) {  
  10.                 Toast.makeText(MainActivity.this,"Active :"+active, Toast.LENGTH_SHORT).show();  
  11.             }  
  12.         });  
Step 7
 
Verify the preview.
 
After the code is applied, the preview will appear like this.
 
Android
 
Step 8
 
Next, go to Android Studio and deploy the application. Select an Emulator or your mobile with USB debugging enabled. Give it a few seconds to make installations and set permissions.
 
Android
 
Run the application in your desired emulator (Shift + F10).
 
Android
 
Explanation of source code
 
The source code used in the activitymain.xml file will implement a lock in the app and render the Swipe Animation to the Lock.
 

Summary

 
We have just created the app to see how the Swipe lock works and learned where to use it.
 
*Support and Share, Thank you*


Similar Articles