SQLite database with login and register demo

Sqlite database use to store the application data. sqlite is a lightweight database

Screenshot

Snow
Forest
Mountains

Resource file

value

color.xml
<?xml version="1.0" encoding="utf-8"?>
        <resources>
           <color name="colorPrimary">#3b5998</color>
           <color name="colorPrimaryDark">#3b5998</color>
           <color name="colorAccent">#3b5998</color>
           <color name="lavender">#3b5998</color>
           <color name="white">#FFFFFF</color>
           <color name="login_button">#D81B60</color>
        </resources>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
        <resources>
            <string name="app_name">Login</string>
            <string name="email">Email</string>
            <string name="login">Login</string>
            <string name="name">Name</string>
            <string name="password">Password</string>
            <string name="phone">Phone number</string>
            <string name="register">Register</string>
            <string name="user_login">User Login</string>
            <string name="please_enter_name">please enter name</string>
            <string name="please_enter_email">please enter email</string>
            <string name="please_enter_password">please enter password</string>
            <string name="please_enter_phone">please enter phone number</string>
            <string name="loading">Loading....</string>
            <string name="ok">OK</string>
            <string name="register_success">Register success fully</string>
            <string name="email_id_use">Email id already use</string>
            <string name="fail">Login Fail</string>
            <string name="update">Update</string>
            <string name="logout">Logout</string>
            <string name="update_user">Update User</string>
        </resources>
styles.xml
<resources>
            <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
                <item name="colorPrimary">@color/colorPrimary</item>
                <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
                <item name="colorAccent">@color/colorAccent</item>
            </style>
        </resources>

drawable file

edittext_bg.xml
<?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <item>
                <shape android:shape="rectangle">
                    <stroke android:width="1dp" android:color="@color/white" />
                    <corners android:radius="20dp" />
                </shape>
            </item>
        </selector>
login_button_bg.xml
<?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@color/white" />
                    <corners android:radius="40dp" />
                </shape>
            </item>
        </selector>

layout file

activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/lavender"
            android:fillViewport="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/lavender"
                android:gravity="center"
                android:orientation="vertical"
                tools:context=".Activity.Login">

                 <TextView
                     android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/login"
                    android:textColor="@color/white"
                    android:textSize="32sp"
                    android:textStyle="bold" />

                 <EditText
                    android:id="@+id/editText_email_login"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="40dp"
                    android:layout_marginEnd="20dp"
                    android:background="@drawable/edittext_bg"
                    android:hint="@string/email"
                    android:inputType="textEmailAddress"
                    android:paddingStart="10dp"
                    android:paddingEnd="10dp"
                    android:singleLine="true"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@null" />

                <EditText
                    android:id="@+id/editText_password_login"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginEnd="20dp"
                    android:background="@drawable/edittext_bg"
                    android:hint="@string/password"
                    android:inputType="textPassword"
                    android:paddingStart="10dp"
                    android:paddingEnd="10dp"
                    android:singleLine="true"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@null" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/button_login"
                        android:layout_width="120dp"
                        android:layout_height="40dp"
                        android:layout_marginTop="20dp"
                        android:background="@drawable/login_button_bg"
                        android:text="@string/login"
                        android:textColor="@color/login_button"
                        android:textStyle="bold" />

                    <Button
                        android:id="@+id/button_reg_login"
                        android:layout_width="120dp"
                        android:layout_height="40dp"
                        android:layout_marginStart="20dp"
                        android:layout_marginTop="20dp"
                        android:background="@drawable/login_button_bg"
                        android:text="@string/register"
                        android:textColor="@color/login_button"
                        android:textStyle="bold" />

                </LinearLayout>

            </LinearLayout>

        </ScrollView>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:orientation="vertical"
            tools:context=".Activity.MainActivity">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginTop="40dp"
                android:layout_marginEnd="10dp"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/name"
                        android:textSize="18sp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        android:layout_marginEnd="10dp"
                        android:text=":-"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/textView_name_main"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/name"
                        android:textSize="18sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/email"
                        android:textSize="18sp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        android:layout_marginEnd="10dp"
                        android:text=":-"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/textView_email_main"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/name"
                        android:textSize="18sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/phone"
                        android:textSize="18sp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        android:layout_marginEnd="10dp"
                        android:text=":-"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/textView_phone_main"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/name"
                        android:textSize="18sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="20dp"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/button_update_main"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/update" />

                    <Button
                        android:id="@+id/button_logout_main"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="5dp"
                        android:text="@string/logout" />

                </LinearLayout>

            </LinearLayout>

        </RelativeLayout>
activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/lavender"
            android:fillViewport="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical"
                tools:context=".Activity.Register">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/register"
                    android:textColor="@color/white"
                    android:textSize="32sp"
                    android:textStyle="bold" />

                <EditText
                    android:id="@+id/editText_name_reg"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="50dp"
                    android:layout_marginEnd="20dp"
                    android:background="@drawable/edittext_bg"
                    android:hint="@string/name"
                    android:inputType="text"
                    android:paddingStart="10dp"
                    android:paddingEnd="10dp"
                    android:singleLine="true"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@null" />

                <EditText
                    android:id="@+id/editText_email_reg"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginEnd="20dp"
                    android:background="@drawable/edittext_bg"
                    android:hint="@string/email"
                    android:inputType="textEmailAddress"
                    android:paddingStart="10dp"
                    android:paddingEnd="10dp"
                    android:singleLine="true"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@null" />

                <EditText
                    android:id="@+id/editText_password_reg"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginEnd="20dp"
                    android:background="@drawable/edittext_bg"
                    android:hint="@string/password"
                    android:inputType="textPassword"
                    android:paddingStart="10dp"
                    android:paddingEnd="10dp"
                    android:singleLine="true"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@null" />

                <EditText
                    android:id="@+id/editText_phone_reg"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginEnd="20dp"
                    android:background="@drawable/edittext_bg"
                    android:hint="@string/phone"
                    android:inputType="phone"
                    android:paddingStart="10dp"
                    android:paddingEnd="10dp"
                    android:singleLine="true"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@null" />

                <Button
                    android:id="@+id/button_reg"
                    android:layout_width="120dp"
                    android:layout_height="40dp"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:layout_marginBottom="40dp"
                    android:background="@drawable/login_button_bg"
                    android:text="@string/register"
                    android:textColor="@color/login_button"
                    android:textStyle="bold" />

            </LinearLayout>

        </ScrollView>
activity_update_user.xml
<?xml version="1.0" encoding="utf-8"?>
        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/lavender"
            android:fillViewport="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical"
                tools:context=".Activity.UpdateUser">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/update_user"
                    android:textColor="@color/white"
                    android:textSize="32sp"
                    android:textStyle="bold" />

                <EditText
                    android:id="@+id/editText_name_update"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="50dp"
                    android:layout_marginEnd="20dp"
                    android:background="@drawable/edittext_bg"
                    android:hint="@string/name"
                    android:inputType="text"
                    android:paddingStart="10dp"
                    android:paddingEnd="10dp"
                    android:singleLine="true"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@null" />

                <EditText
                    android:id="@+id/editText_email_update"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginEnd="20dp"
                    android:background="@drawable/edittext_bg"
                    android:hint="@string/email"
                    android:inputType="textEmailAddress"
                    android:paddingStart="10dp"
                    android:paddingEnd="10dp"
                    android:singleLine="true"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@null" />

                <EditText
                    android:id="@+id/editText_password_update"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginEnd="20dp"
                    android:background="@drawable/edittext_bg"
                    android:hint="@string/password"
                    android:inputType="textPassword"
                    android:paddingStart="10dp"
                    android:paddingEnd="10dp"
                    android:singleLine="true"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@null" />

                <EditText
                    android:id="@+id/editText_phone_update"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginEnd="20dp"
                    android:background="@drawable/edittext_bg"
                    android:hint="@string/phone"
                    android:inputType="phone"
                    android:paddingStart="10dp"
                    android:paddingEnd="10dp"
                    android:singleLine="true"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@null" />

                <Button
                    android:id="@+id/button_update"
                    android:layout_width="120dp"
                    android:layout_height="40dp"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:layout_marginBottom="40dp"
                    android:background="@drawable/login_button_bg"
                    android:text="@string/update"
                    android:textColor="@color/login_button"
                    android:textStyle="bold" />
           
            </LinearLayout>

        </ScrollView>

AndroidManifest file

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.app.databaseloginregister">

            <application
                android:allowBackup="true"
                android:icon="@mipmap/ic_launcher"
                android:label="@string/app_name"
                android:roundIcon="@mipmap/ic_launcher_round"
                android:supportsRtl="true"
                android:theme="@style/AppTheme">
                <activity android:name=".Activity.Login">
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />

                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>
                <activity android:name=".Activity.Register" />
                <activity android:name=".Activity.UpdateUser" />
                <activity android:name=".Activity.MainActivity" />
            </application>

        </manifest>  
        

Java Code

Login.java
package com.app.databaseloginregister.Activity;

        import android.content.Context;
        import android.content.Intent;
        import android.os.Bundle;
        import android.support.v7.app.AppCompatActivity;
        import android.view.View;
        import android.view.WindowManager;
        import android.view.inputmethod.InputMethodManager;
        import android.widget.Button;
        import android.widget.EditText;

        import com.app.databaseloginregister.DataBase.DatabaseHandler;
        import com.app.databaseloginregister.R;
        import com.app.databaseloginregister.Util.Method;

        public class Login extends AppCompatActivity {

            private Method method;
            private DatabaseHandler db;
            private String email, password;
            private EditText editText_email, editText_password;
            private Button button_reg, button_login;
            private InputMethodManager imm;





            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_login);

                method = new Method(Login.this);
                db = new DatabaseHandler(Login.this);

                imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

                if (method.pref.getBoolean(method.pref_login, false)) {
                    String id = method.pref.getString(method.profileId, null);
                    startActivity(new Intent(Login.this, MainActivity.class)
                            .putExtra("id", id));
                    finishAffinity();
                }

                editText_email = findViewById(R.id.editText_email_login);
                editText_password = findViewById(R.id.editText_password_login);
                button_login = findViewById(R.id.button_login);
                button_reg = findViewById(R.id.button_reg_login);

                button_login.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        email = editText_email.getText().toString();
                        password = editText_password.getText().toString();
                        editText_email.clearFocus();
                        editText_password.clearFocus();
                        imm.hideSoftInputFromWindow(editText_email.getWindowToken(), 0);
                        imm.hideSoftInputFromWindow(editText_password.getWindowToken(), 0);
                        login();
                    }
                });

                button_reg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(Login.this, Register.class));
                    }
                });

            }

            private void login() {
                editText_email.setError(null);
                editText_password.setError(null);

                if (!isValidMail(email) || email.isEmpty()) {
                    editText_email.requestFocus();
                    editText_email.setError(getResources().getString(R.string.please_enter_email));
                } else if (password.isEmpty()) {
                    editText_password.requestFocus();
                    editText_password.setError(getResources().getString(R.string.please_enter_password));
                } else {
                    if (!db.checkLogin(email, password)) {
                        String id = db.getUserId(email);
                        if (id != null) {
                            startActivity(new Intent(Login.this, MainActivity.class)
                                    .putExtra("id", id));
                            finishAffinity();
                            method.editor.putBoolean(method.pref_login, true);
                            method.editor.putString(method.profileId, id);
                            method.editor.commit();
                        }
                    } else {
                        method.alertBox(getResources().getString(R.string.fail));
                    }
                }
            }

            private boolean isValidMail(String email) {
                return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
            }

        }
MainActivity.java
package com.app.databaseloginregister.Activity;

        import android.content.Intent;
        import android.os.Bundle;
        import android.support.v7.app.AppCompatActivity;
        import android.view.View;
        import android.widget.Button;
        import android.widget.TextView;

        import com.app.databaseloginregister.DataBase.DatabaseHandler;
        import com.app.databaseloginregister.Item.UserList;
        import com.app.databaseloginregister.R;
        import com.app.databaseloginregister.Util.Method;

        import java.util.ArrayList;
        import java.util.List;

        public class MainActivity extends AppCompatActivity {

            private String id;
            private Method method;
            private DatabaseHandler db;
            private List userLists;
            private Button button_update, button_logout;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                id = getIntent().getStringExtra("id");
                method = new Method(MainActivity.this);

                userLists = new ArrayList<>();
                db = new DatabaseHandler(MainActivity.this);

                userLists = db.getUser(id);

                TextView textView_name = findViewById(R.id.textView_name_main);
                TextView textView_email = findViewById(R.id.textView_email_main);
                TextView textView_phoneNo = findViewById(R.id.textView_phone_main);
                button_update = findViewById(R.id.button_update_main);
                button_logout = findViewById(R.id.button_logout_main);

                if (userLists.size() != 0) {
                    textView_name.setText(userLists.get(0).getName());
                    textView_email.setText(userLists.get(0).getEmail());
                    textView_phoneNo.setText(userLists.get(0).getPhone());
                }

                button_update.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(MainActivity.this, UpdateUser.class)
                                .putExtra("id", id)
                                .putExtra("name", userLists.get(0).getName())
                                .putExtra("email", userLists.get(0).getEmail())
                                .putExtra("phone", userLists.get(0).getPhone()));
                    }
                });

                button_logout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        method.editor.putBoolean(method.pref_login, false);
                        method.editor.commit();
                        startActivity(new Intent(MainActivity.this, Login.class));
                        finishAffinity();
                    }
                });

            }
        }
Register.java
        package com.app.databaseloginregister.Activity;

        import android.content.Context;
        import android.content.Intent;
        import android.os.Bundle;
        import android.support.v7.app.AppCompatActivity;
        import android.view.View;
        import android.view.WindowManager;
        import android.view.inputmethod.InputMethodManager;
        import android.widget.Button;
        import android.widget.EditText;

        import com.app.databaseloginregister.DataBase.DatabaseHandler;
        import com.app.databaseloginregister.R;
        import com.app.databaseloginregister.Util.Method;

        public class Register extends AppCompatActivity {

            private Method method;
            private DatabaseHandler db;
            private Button button_register;
            private InputMethodManager imm;
            private String name, email, password, phoneNo;
            private EditText editText_name, editText_email, editText_password, editText_phoneNo;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_register);

                method = new Method(Register.this);
                db = new DatabaseHandler(Register.this);

                imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

                editText_name = findViewById(R.id.editText_name_reg);
                editText_email = findViewById(R.id.editText_email_reg);
                editText_password = findViewById(R.id.editText_password_reg);
                editText_phoneNo = findViewById(R.id.editText_phone_reg);
                button_register = findViewById(R.id.button_reg);

                button_register.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        name = editText_name.getText().toString();
                        email = editText_email.getText().toString();
                        password = editText_password.getText().toString();
                        phoneNo = editText_phoneNo.getText().toString();

                        editText_name.clearFocus();
                        editText_email.clearFocus();
                        editText_password.clearFocus();
                        editText_phoneNo.clearFocus();
                        imm.hideSoftInputFromWindow(editText_name.getWindowToken(), 0);
                        imm.hideSoftInputFromWindow(editText_email.getWindowToken(), 0);
                        imm.hideSoftInputFromWindow(editText_password.getWindowToken(), 0);
                        imm.hideSoftInputFromWindow(editText_phoneNo.getWindowToken(), 0);

                        form();

                    }
                });

            }

            private boolean isValidMail(String email) {
                return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
            }

            private void form() {

                editText_name.setError(null);
                editText_email.setError(null);
                editText_password.setError(null);
                editText_phoneNo.setError(null);

                if (name.equals("") || name.isEmpty()) {
                    editText_name.requestFocus();
                    editText_name.setError(getResources().getString(R.string.please_enter_name));
                } else if (!isValidMail(email) || email.isEmpty()) {
                    editText_email.requestFocus();
                    editText_email.setError(getResources().getString(R.string.please_enter_email));
                } else if (password.equals("") || password.isEmpty()) {
                    editText_password.requestFocus();
                    editText_password.setError(getResources().getString(R.string.please_enter_password));
                } else if (phoneNo.equals("") || phoneNo.isEmpty()) {
                    editText_phoneNo.requestFocus();
                    editText_phoneNo.setError(getResources().getString(R.string.please_enter_phone));
                } else {

                    if (db.checkEmail(email)) {

                        db.addUser(name, email, password, phoneNo);

                        editText_name.setText("");
                        editText_email.setText("");
                        editText_password.setText("");
                        editText_phoneNo.setText("");

                        method.alertBox(getResources().getString(R.string.register_success));

                        startActivity(new Intent(Register.this, Login.class));

                    } else {
                        method.alertBox(getResources().getString(R.string.email_id_use));
                    }

                }
            }

            @Override
            public void onBackPressed() {
                super.onBackPressed();
            }
        }
        
UpdateUser.java
        package com.app.databaseloginregister.Activity;

        import android.content.Context;
        import android.content.Intent;
        import android.os.Bundle;
        import android.support.v7.app.AppCompatActivity;
        import android.view.View;
        import android.view.WindowManager;
        import android.view.inputmethod.InputMethodManager;
        import android.widget.Button;
        import android.widget.EditText;

        import com.app.databaseloginregister.DataBase.DatabaseHandler;
        import com.app.databaseloginregister.R;
        import com.app.databaseloginregister.Util.Method;

        public class UpdateUser extends AppCompatActivity {

            private Method method;
            private DatabaseHandler db;
            private Button button_update;
            private InputMethodManager imm;
            private String id, name, email, password, phoneNo;
            private EditText editText_name, editText_email, editText_password, editText_phoneNo;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_update_user);

                method = new Method(UpdateUser.this);
                db = new DatabaseHandler(UpdateUser.this);

                id = getIntent().getStringExtra("id");
                name = getIntent().getStringExtra("name");
                email = getIntent().getStringExtra("email");
                phoneNo = getIntent().getStringExtra("phone");

                imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

                editText_name = findViewById(R.id.editText_name_update);
                editText_email = findViewById(R.id.editText_email_update);
                editText_password = findViewById(R.id.editText_password_update);
                editText_phoneNo = findViewById(R.id.editText_phone_update);
                button_update = findViewById(R.id.button_update);

                editText_name.setText(name);
                editText_email.setText(email);
                editText_phoneNo.setText(phoneNo);

                button_update.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        name = editText_name.getText().toString();
                        email = editText_email.getText().toString();
                        password = editText_password.getText().toString();
                        phoneNo = editText_phoneNo.getText().toString();

                        editText_name.clearFocus();
                        editText_email.clearFocus();
                        editText_password.clearFocus();
                        editText_phoneNo.clearFocus();
                        imm.hideSoftInputFromWindow(editText_name.getWindowToken(), 0);
                        imm.hideSoftInputFromWindow(editText_email.getWindowToken(), 0);
                        imm.hideSoftInputFromWindow(editText_password.getWindowToken(), 0);
                        imm.hideSoftInputFromWindow(editText_phoneNo.getWindowToken(), 0);

                        form();

                    }
                });

            }

            private boolean isValidMail(String email) {
                return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
            }

            private void form() {

                editText_name.setError(null);
                editText_email.setError(null);
                editText_password.setError(null);
                editText_phoneNo.setError(null);

                if (name.equals("") || name.isEmpty()) {
                    editText_name.requestFocus();
                    editText_name.setError(getResources().getString(R.string.please_enter_name));
                } else if (!isValidMail(email) || email.isEmpty()) {
                    editText_email.requestFocus();
                    editText_email.setError(getResources().getString(R.string.please_enter_email));
                } else if (phoneNo.equals("") || phoneNo.isEmpty()) {
                    editText_phoneNo.requestFocus();
                    editText_phoneNo.setError(getResources().getString(R.string.please_enter_phone));
                } else {
                    db.updateUser(id, name, email, password, phoneNo);
                    startActivity(new Intent(UpdateUser.this, MainActivity.class)
                            .putExtra("id", id));
                    finishAffinity();
                }
            }

            @Override
            public void onBackPressed() {
                super.onBackPressed();
            }

        }
        

Sqlite Database

Create inside DataBase package name DatabaseHandler class and extends SQLiteOpenHelper. This class all function perform CRUD(create, read, update and delete) onCreate() will be call once application is install and create table. onUpgrade() call to update table. addUser() method use to the new register user data add to the database. getUser() method used to the get single user information using to user id. updateUser() method used to the user update information to the database. checkEmail() method used to check the email id already exist or not in database. getUserId() method used to the get user id.
DatabaseHandler.java

        package com.app.databaseloginregister.DataBase;

        import android.annotation.SuppressLint;
        import android.content.ContentValues;
        import android.content.Context;
        import android.database.Cursor;
        import android.database.sqlite.SQLiteDatabase;
        import android.database.sqlite.SQLiteOpenHelper;
        import android.util.Log;

        import com.app.databaseloginregister.Item.UserList;

        import java.util.ArrayList;
        import java.util.List;

        public class DatabaseHandler extends SQLiteOpenHelper {

            // All Static variables
            // Database Version
            private static final int DATABASE_VERSION = 1;

            // Database Name
            private static final String DATABASE_NAME = "demo_login";

            // Table name
            private static final String TABLE_NAME = "user";

            // Table Columns names
            private static final String KEY_ID = "id";
            private static final String KEY_NAME = "name";
            private static final String KEY_EMAIL = "user_email";
            private static final String KEY_PHONE_NUMBER = "phone_number";
            private static final String KEY_PASSWORD = "password";

            public DatabaseHandler(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }

            @Override
            public void onCreate(SQLiteDatabase db) {

                //create table
                String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
                        + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT ," + KEY_NAME + " TEXT,"
                        + KEY_EMAIL + " TEXT," + KEY_PHONE_NUMBER + " TEXT,"
                        + KEY_PASSWORD + " TEXT"
                        + ")";
                db.execSQL(CREATE_TABLE);

            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            }

            //add user in database
            public void addUser(String name, String email, String password, String phone) {

                SQLiteDatabase db = this.getWritableDatabase();

                ContentValues values = new ContentValues();
                values.put(KEY_NAME, name);
                values.put(KEY_EMAIL, email);
                values.put(KEY_PHONE_NUMBER, phone);
                values.put(KEY_PASSWORD, password);

                db.insert(TABLE_NAME, null, values);
                db.close();//close database connection

            }

            //get user detail
            public List getUser(String id) {

                List userLists = new ArrayList<>();

                String selectQuery = "SELECT  * FROM " + TABLE_NAME + " WHERE " + KEY_ID + " = " + id;
                Log.d("Query_database_user", selectQuery);

                SQLiteDatabase db = this.getWritableDatabase();
                @SuppressLint("Recycle")
                Cursor cursor = db.rawQuery(selectQuery, null);

                if (cursor.moveToFirst()) {
                    do {
                        UserList list = new UserList();
                        list.setName(cursor.getString(1));
                        list.setEmail(cursor.getString(2));
                        list.setPhone(cursor.getString(3));
                        list.setPassword(cursor.getString(4));

                        userLists.add(list);
                    } while (cursor.moveToNext());
                }

                // return user list
                return userLists;

            }

            //update user detail
            public void updateUser(String id, String name, String email, String password, String phoneNumber) {

                SQLiteDatabase db = this.getWritableDatabase();

                ContentValues values = new ContentValues();
                values.put(KEY_NAME, name);
                values.put(KEY_EMAIL, email);
                if (!password.equals("")) {
                    values.put(KEY_PASSWORD, password);
                }
                if (!phoneNumber.equals("")) {
                    values.put(KEY_PHONE_NUMBER, phoneNumber);
                }
                db.update(TABLE_NAME, values, KEY_ID + "=" + id, null);

            }

            //check user login or not
            public boolean checkLogin(String userEmail, String userPassword) {
                String selectQuery = "SELECT " + KEY_ID + " FROM " + TABLE_NAME + " WHERE " + KEY_EMAIL + " =" + " " + "\'" + userEmail + "\'" + " AND " + KEY_PASSWORD + " = " + "\'" + userPassword + "\'";
                Log.d("Query_database", selectQuery);
                SQLiteDatabase db = this.getWritableDatabase();
                Cursor cursor = db.rawQuery(selectQuery, null);
                if (cursor.getCount() == 0) {
                    return true;
                } else {
                    return false;
                }
            }

            //check email id exit or not
            public boolean checkEmail(String userEmail) {
                String selectQuery = "SELECT  * FROM " + TABLE_NAME + " WHERE " + KEY_EMAIL + " =?";
                Log.d("Query_database", selectQuery);
                SQLiteDatabase db = this.getWritableDatabase();
                @SuppressLint("Recycle") Cursor cursor = db.rawQuery(selectQuery, new String[]{userEmail});
                if (cursor.getCount() == 0) {
                    return true;
                } else {
                    return false;
                }
            }

            //get current user login id
            public String getUserId(String userEmail) {

                String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE " + KEY_EMAIL + " = " + "\'" + userEmail + "\'";
                Log.d("Query_database", selectQuery);
                SQLiteDatabase db = this.getWritableDatabase();
                @SuppressLint("Recycle")
                Cursor cursor = db.rawQuery(selectQuery, null);
                if (cursor.moveToFirst()) {
                    String userId = cursor.getString(0);
                    Log.d("user_id", String.valueOf(userId));
                    return userId;
                } else {
                    return null;
                }
            }

        }

        

Item class

UserList.java

        package com.app.databaseloginregister.Item;

        import java.io.Serializable;

        public class UserList implements Serializable {

            private String name, email, phone, password;

            public UserList() {
            }

            public UserList(String name, String email, String phone, String password) {
                this.name = name;
                this.email = email;
                this.phone = phone;
                this.password = password;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getEmail() {
                return email;
            }

            public void setEmail(String email) {
                this.email = email;
            }

            public String getPhone() {
                return phone;
            }

            public void setPhone(String phone) {
                this.phone = phone;
            }

            public String getPassword() {
                return password;
            }

            public void setPassword(String password) {
                this.password = password;
            }
        }


        

Method class

Method.java

        package com.app.databaseloginregister.Util;

        import android.app.Activity;
        import android.content.DialogInterface;
        import android.content.SharedPreferences;
        import android.support.v7.app.AlertDialog;

        import com.app.databaseloginregister.R;

        public class Method {

            private Activity activity;

            public SharedPreferences pref;
            public SharedPreferences.Editor editor;
            private final String myPreference = "login";
            public String pref_login = "pref_login";
            public String profileId = "profileId";

            public Method(Activity activity) {
                this.activity = activity;
                pref = activity.getSharedPreferences(myPreference, 0); // 0 - for private mode
                editor = pref.edit();
            }


            //show dialog box
            public void alertBox(String message) {

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
                alertDialogBuilder.setCancelable(false);
                alertDialogBuilder.setMessage(message);
                alertDialogBuilder.setPositiveButton(activity.getResources().getString(R.string.ok),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {

                            }
                        });

                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();

            }

        }
        



No comments:

Post a Comment