info@thistimebd.com

Saturday 23rd of November 02:21:27pm

SQLite Login Log up android source code

MainActivity.xml

===================

package com.sam.web.loginpage;


import android.content.Intent;

import android.database.sqlite.SQLiteDatabase;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button signInButton, signUpHereButton;

    private EditText usernameEditText,passwordEditText;

    DatabaseHelper databaseHelper;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        signInButton=(Button) findViewById(R.id.signInButtonId);

        signUpHereButton=(Button)findViewById(R.id.signUpButtonId);

        usernameEditText=(EditText) findViewById(R.id.signInusernameEditTextId);

        passwordEditText=(EditText) findViewById(R.id.signInpasswordEditTextId);


        databaseHelper=new DatabaseHelper(this);

        SQLiteDatabase sqLiteDatabase=databaseHelper.getWritableDatabase();


        signInButton.setOnClickListener(this);

        signUpHereButton.setOnClickListener(this);

    }


    @Override

    public void onClick(View view) {

        String username=usernameEditText.getText().toString();

        String password=passwordEditText.getText().toString();


        if (view.getId()==R.id.signInButtonId)

        {

            Boolean result=databaseHelper.findPassword(username,password);


            if(result==true){


                Intent intent=new Intent(MainActivity.this,Main2Activity.class);

                startActivity(intent);


            }

            else {


                Toast.makeText(getApplicationContext(),"username and password didnt match",Toast.LENGTH_LONG).show();

            }


        } else if (view.getId()==R.id.signUpButtonId)

        {

            Intent intent=new Intent(MainActivity.this,SignUpActivity.class);

            startActivity(intent);



        }


    }

}

===================================================================

activity_main.xml

===================

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:background="#22c8ff"

    android:padding="10dp"

    android:gravity="center"

    tools:context="com.sam.web.loginpage.MainActivity">


    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content">


        <TextView

            android:textSize="20sp"

            android:text="Username"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content" />


        <EditText

            android:id="@+id/signInusernameEditTextId"

            android:textSize="20sp"

            android:layout_width="match_parent"

            android:layout_height="wrap_content" />



    </LinearLayout>

    

    <LinearLayout

        android:layout_marginTop="20dp"

        android:layout_width="match_parent"

        android:layout_height="wrap_content">


        <TextView

        android:textSize="20sp"

        android:text="Password"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />


        <EditText

            android:inputType="textPassword"

            android:id="@+id/signInpasswordEditTextId"

            android:textSize="20sp"

            android:layout_width="match_parent"

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



    <Button

        android:layout_marginTop="20dp"

        android:background="#0e8eb9"


        android:text="Sign in"

        android:id="@+id/signInButtonId"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />

    <Button

        android:layout_marginTop="20dp"

        android:background="#0e8eb9"


        android:text="Sign up here"

        android:id="@+id/signUpButtonId"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />



</LinearLayout>

========================================================================

Main2Activity.java

=====================


package com.sam.web.loginpage;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;


public class Main2Activity extends AppCompatActivity {


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main2);

    }

}

===========================================================================

activity_main2.xml

=====================


<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="com.sam.web.loginpage.Main2Activity">


    <TextView

        android:textSize="27sp"

        android:text="Welcome"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />


</android.support.constraint.ConstraintLayout>

=======================================================================================

signUpActivity.java

===================

package com.sam.web.loginpage;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;


public class SignUpActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText nameEditText,emailEditText,usernameEditText,passwordEditText;

    private Button signUpButton;

    UserDetails userDetails;

    DatabaseHelper databaseHelper;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_sign_up);


        nameEditText=(EditText) findViewById(R.id.nameEditTextId);

        emailEditText=(EditText) findViewById(R.id.signUpEmailEditTextId);

        usernameEditText=(EditText) findViewById(R.id.signUpUsernameEditTextId);

        passwordEditText=(EditText) findViewById(R.id.signUpPasswordEditTextId);



        signUpButton=(Button) findViewById(R.id.signUpButtonId);

        databaseHelper=new DatabaseHelper(this);


        userDetails=new UserDetails();

        signUpButton.setOnClickListener(this);



    }


    @Override

    public void onClick(View v) {

        String name = nameEditText.getText().toString();

        String email = emailEditText.getText().toString();

        String username = usernameEditText.getText().toString();

        String password = passwordEditText.getText().toString();


        userDetails.setName(name);

        userDetails.setEmail(email);

        userDetails.setUsername(username);

        userDetails.setPassword(password);


        long rowId = databaseHelper.insertData(userDetails);


        if (rowId > 0)

        {

            Toast.makeText(getApplicationContext(),"Row "+rowId+" is successfully inserted",Toast.LENGTH_LONG).show();


        }

        else {


            Toast.makeText(getApplicationContext(),"Row "+rowId+" Data is not inserted",Toast.LENGTH_LONG).show();




        }



    }

}


======================================================================================

activity_signup.xml

=====================


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:background="#22c8ff"

    android:padding="10dp"


    tools:context="com.sam.web.loginpage.SignUpActivity">


    <EditText

        android:id="@+id/nameEditTextId"

        android:textSize="20sp"

        android:hint="name"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />

    <EditText

        android:id="@+id/signUpEmailEditTextId"

        android:textSize="20sp"

        android:hint="Email"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />

    <EditText

        android:id="@+id/signUpUsernameEditTextId"

        android:textSize="20sp"

        android:hint="Username"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />

    <EditText

        android:inputType="textPassword"


        android:id="@+id/signUpPasswordEditTextId"

        android:textSize="20sp"

        android:hint="Password"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />


    <Button

        android:id="@+id/signUpButtonId"

        android:layout_gravity="center"

        android:background="#0e8eb9"

        android:text="Sign Up"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />


</LinearLayout>

============================================================================

DatabaseHelper.java

==================

package com.sam.web.loginpage;


import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

import android.widget.Toast;

import android.content.ContentValues;



/**

 * Created by maudud on 2/5/18.

 */


public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME="userdetails1.db";

    private static final String TABLE_NAME="user_details1";

    private static final String ID="Id";

    private static final String NAME="Name";

    private static final String EMAIL="Email";

    private static final String USERNAME="Username";

    private static final String PASSWORD="Password";

    private static final int VERSION_NUMBER=4;

    private Context context;


    private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+"("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME+" VARCHAR(255) NOT NULL,"+EMAIL+" TEXT NOT NULL,"+USERNAME+" TEXT NOT NULL,"+PASSWORD+" TEXT NOT NULL);";

    private static final String DROP_TABLE=" DROP TABLE IF EXISTS "+TABLE_NAME;




    public DatabaseHelper(Context context) {

        super(context, DATABASE_NAME, null, VERSION_NUMBER);

        this.context=context;

    }


    @Override

    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        try {

            sqLiteDatabase.execSQL(CREATE_TABLE);

            Toast.makeText(context,"onCreate is called",Toast.LENGTH_LONG).show();


        }catch (Exception e){

            Toast.makeText(context,"Exception :" +e,Toast.LENGTH_LONG).show();




        }


    }


    @Override

    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {


        try {

            Toast.makeText(context,"onUpgrade is called",Toast.LENGTH_LONG).show();


            sqLiteDatabase.execSQL(DROP_TABLE);

            onCreate(sqLiteDatabase);


        }catch (Exception e){

            Toast.makeText(context,"Exception :" +e,Toast.LENGTH_LONG).show();




        }


    }

    public long insertData(UserDetails userDetails){


        SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();

        ContentValues contentValues=new ContentValues();

        contentValues.put(NAME,userDetails.getName());

        contentValues.put(EMAIL,userDetails.getEmail());

        contentValues.put(USERNAME,userDetails.getUsername());

        contentValues.put(PASSWORD,userDetails.getPassword());


        long rowId=sqLiteDatabase.insert(TABLE_NAME,null,contentValues);

        return rowId;

    }

    public Boolean findPassword(String uname,String pass){


        SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();

        Cursor cursor=sqLiteDatabase.rawQuery("SELECT * FROM "+TABLE_NAME,null);

        Boolean result= false;


        if (cursor.getCount()==0)

        {


            Toast.makeText(context,"NO data is found",Toast.LENGTH_LONG).show();


        }

        else {



            while (cursor.moveToNext())

            {


                String username=cursor.getString(3);

                String password=cursor.getString(4);


                if(username.equals(uname) && password.equals(pass)){



                    result=true;

                    break;

                }

            }

        }

        return result;

    }

}

=======================================================================================

UserDetail.java

====================

package com.sam.web.loginpage;


/**

 * Created by maudud on 2/5/18.

 */


public class UserDetails {

    private String name,email, username,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 getUsername() {

        return username;

    }


    public void setUsername(String username) {

        this.username = username;

    }


    public String getPassword() {

        return password;

    }


    public void setPassword(String password) {

        this.password = password;

    }

}