Android login with sqlite

Android Login with SQLite Database Tutorial

Overview

In this tutorial, we show you how to create Android Login Example with SQLite Database using Android Studio. This Application has two screens, first one is login screen where we can just login with our credentials. Second screen is Welcome screen after we login successfully.

Watch Tutorial

SQLite Creating Database and Table

Create Database Name: test.db
The following SQLite Script is used to create a table called user and insert data to it.

Creating the project

Enter the info below:
Application Name: LoginWithSQLiteDBExample
Company domain: jackrutorial.com
Project location: C:/android
Then click «Next».

Select the form factors and minimum SDK. In this tutorial, We selected the minimum SDK «API 15: Android 4.0.3(IceCreamSandwich)», click «Next».

Select the Empty activity and click «Next».

Leave the activity name “MainActivity”, Click Finish.

Adding test.db file into the project

Right click on app folder goto New> Folder>Assets Folder

Keep all the settings default on Dialog box. Under target source set, option main should be selected and Click Finish.

Create Activity

  • Click the app folder for your project and choose File > New > Activity > Empty Activity
  • Name the new activity «MainActivity«.
  • Click Finish.

Change the activity_main.xml layout to the following.

Change MainActivity activity code to the following.

Источник

Android Login and Register with SQLite Database Tutorial

Whether it’s a website, application, or software login and registration are very important parts for most of them. In this post, we will learn how we can achieve login and registration using the SQLite database. In this application, we will be able to log in and register a new user. This Application has two screens first one is a login screen where we can just log in with our credentials. The second screen is the Register screen where we can register new users. So let’s get started

Download source code or apk from following links

Читайте также:  Android blobs что это

Following is a video demonstration of output of this tutorial

1. Database structure is as shown below

2. Now first we will create news class which will help us to manage to create our database and to add, retrieving information from databases. To create new class package name =>SqliteHelper.java. add the following class to it

3. Create new class User which will be model of user and paste below lines in it

4. Now create new class package name=>LoginActivity.java which will be our login activity paste below lines in it

Create new layout in layout=>activity_login.xml and paste below lines in it

5. Now create new class package name=>RegisterActivity.java which will be our registration activity paste below lines in it

Create new layout in layout=>activity_register.xml and paste below lines in it

6. Inside manifest.xml paste below lines

7. Inside values=>colors.xml paste below lines

8. Inside values=>styles.xml paste below lines

9. Inside values=>strings.xml

10. Now run your application and see the results if you have got any problem in mind then ask in following comment boxes I will be happy to help you.

Источник

Android[Kotlin] Login and Register with SQLite Database Tutorial

In software applications, it is mostly required to store users and app data locally. Android SDK provides several API’s for developers to save user and app data, So SQLite is one of the ways of storing data. For many applications, SQLite is the app’s backbone whether it’s used directly or via some third-party wrapper. In this tutorial, we will write code in Kotlin and also see how to use SQLite in our app directly.

What is SQLite?

SQLite is a lightweight database which comes with android. It is an Open-Source embedded SQL database engine. This provides relational database management structure for storing user-defined records in the form of tables.

Key point to understand regarding SQLite :-

– SQLite is RDBMS (Relational Database Management System)
– SQLite is written in C programming language
– SQLite is embedded within the Android Operating System, so you don’t need anything external on Android to use SQLite
– To manipulate data (insert, update, delete) in SQLite database – we’ll use SQL (Structured Query Language)

Let’s Get it Working

In this tutorial, we are going to learn how to use SQLite. To really understand the usage of SQLite we will create an app using Kotlin. The App contains simple Login form, Registration form, and a listing of a registered user. This app shows how SQLite database operations are performed. I assume that you have created a new android project. Source Code is available on GitHub.

Читайте также:  Офлайн навигатор для андроид с картами спутника

Step 1) Update build.gradle file.

Before you can use Material Design in your projects you need to add the following compile line to your Gradle dependencies block in your build.gradle file and rebuilt the project .

Step 2) Update strings.xml.

Add the below string values to the string.xml located in res ⇒ values ⇒ strings.xml.

Step 3) Update colors.xml.

Add the below color values to the colors.xml located in res ⇒ values ⇒ colors.xml.

Step 4) Update styles.xml.

Add the below style values to the styles.xml located in res ⇒ values ⇒ styles.xml.

Step 5) Add Logo Image.

Download the below logo image and add it to the drawable folder located in res ⇒ drawable.

Android Tutorials Hub Logo

Step 6) Create User data class.

Create a new package named modal and create a Kotlin file/class named User to maintain single contact as an object.

Step 7) Create DatabaseHelper class.

Create a new package named sql and create Kotlin class named DatabaseHelper. Extend this class with SQLiteOpenHelper to manage database creation and version management. I have also written some method to manipulate data in database.

Methods and functionality

– addUser :- add user to database.
– getAllUser :- fetch users data from database.
– updateUser :- update user in database on the basis of user id.
– deleteUser :- delete user from database on the basis of user id.
– checkUser :- check whether user exists in database.

User Table Schema

Step 8) Create activity_login.xml.

Now create a layout file for the LoginActivity.kt i.e activity_login.xml and add the below code in your layout file. The code will create a simple login form containing a logo on the top, 2 input fields email and password, login button, and registration screen navigation link.

activity_login.xml would result a screen like this:

Step 8) Create InputValidation class.

Create a package named helpers and create a Kotlin class in it named InputValidation.kt and add below code in it. The code will create validation methods for input field. Validation like empty input, valid email and etc.

Step 9) Create LoginActivity class.

Now create a package named activities and create a kotlin class named LoginActivity and add below code. Here i have written the code to validate the input fields Email and Password using the InputValidation class which i described above. Also code for navigation to registration screen on the click of registration link and to user list screen after click on login button if credentials are valid.

The Screen below shows the login form with input validation display the error message if values entered in input fields are not valid.

Login Screen with Input Validation

Читайте также:  Android studio авторизация через api

The Screen below shows the login form with input validation display the snack bar with message if values entered in input fields are not validated from SQLite database.

Login Screen With SQLite Validation

Step 10) Create activity_register.xml.

Now create a layout file for the RegisterActivity.kt i.e activity_register.xml and add the below code in your layout file. The code will create a simple registration form containing a logo on the top, 4 input fields name, email, password, and confirm password, register button, and login screen navigation link.

activity_register.xml would result a screen like this:

Step 11) Create RegisterActivity class.

Now create a package named activities and create a kotlin class named RegisterActivity and add below code. Here I have written the code to validate the input fields Name, Email, Password, and Confirm Password using the InputValidation class which I described above. Also, code for navigation to the login screen on the click of the login link and shows a snack bar with a success message for registration.

The Screen below shows the register form with input validation display the error message if values entered in input fields are not valid.

Register Screen with Input Validation

The Screen below shows the filled register form with valid values.

Register Screen with filled input fields

The Screen below shows the register form display the snackbar with registration success message.

Register Screen with Registration Success Message

Step 12) Create activity_users_list.xml.

Now create a layout file for the UsersListActivity.kt i.e activity_users_list.xml and add the below code in your layout file. The code will create a simple view containing two views one view i.e LinearLayout shows the welcome message with email id of the logged-in user and other views i.e RecyclerView shows the list of registered users in the app.

activity_users_list.xml would result a screen like this:

User List Screen

Step 13) Writing the Adapter Class.

Now create a package named adapters and create a kotlin class named UsersRecyclerAdapter and add below code. Here onCreateViewHolder() method inflates item_user_recycler.xml. In onBindViewHolder() method the appropriate User data (name,email and password) set to each row.

item_user_recycler.xml

Step 14) Create UsersListActivity class.

Now create a package named activities and create a kotlin class named UsersListActivity and add below code. Here I have written the code to show the email id of the logged in user and the list of registered users.

The Screen below after user login shows the brand name, email id of logged in user and registered users list.

User List Screen After Login

Step 15) Update AndroidManifest.xml.

Please feel free to comment as well as ask questions. And, yeah! If this post helps you please do share!

Enjoy Coding and Share Knowledge

Источник

Оцените статью