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.
Give a name to the project and click "Next".
Select the "Phone and Tablet" option and click "Next".
Select an empty activity and click "Next".
At last, give the activity name and click on "Finish".
Step 2
Next, locate app>>res>>drawable folder and Create new vector assets.
Then, choose the lock open icon from the list:
Step 3
Repeat Step 2 to choose the closed lock icon from the list.
Check whether it is done correctly.
Step 4
Locate the Gradle Scripts>>Build. Gradle.
And, type the following dependency in your app's build.gradle.
The code copy is here.
- compile 'com.ebanx:swipe-button:0.4.0'
Step 5
Next, go to app >> res >> layout >> activity_main.xml. Select activity page.
Just type the code as following.
The code copy is here.
- <com.ebanx.swipebtn.SwipeButton
- android:id="@+id/swipe"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginStart="20dp"
- android:layout_marginEnd="20dp"
- android:layout_centerInParent="true"
- app:inner_text="SWIPE TO UNLOCK"
- app:inner_text_color="@android:color/white"
- app:inner_text_size="16sp"
- app:inner_text_bottom_padding="18dp"
- app:inner_text_background="@drawable/shape_rounded"
- app:button_image_disabled="@drawable/ic_lock_open_black_24dp"
- app:button_image_enabled="@drawable/ic_lock_outline_black_24dp"
- app:button_left_padding="20dp"
- app:button_right_padding="20dp"
- app:button_background="@drawable/shape_button"
- />
Step 6
Next, go to app >> java>>Mainactivity.java. Select Mainactivity page.
Just type the code as follows
The code copy is here.
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- SwipeButton swipeButton = (SwipeButton)findViewById(R.id.swipe);
- swipeButton.setOnStateChangeListener(new OnStateChangeListener() {
- @Override
- public void onStateChange(boolean active) {
- Toast.makeText(MainActivity.this,"Active :"+active, Toast.LENGTH_SHORT).show();
- }
- });
Step 7
Verify the preview.
After the code is applied, the preview will appear like this.
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.
Run the application in your desired emulator (Shift + F10).
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*