Wednesday 9 April 2014

Android Signup and Login Page Tutorial


Today i would like to share knowledge about Signup and Login page in Android.
some apps requires login for security purpose.

I am taking a Demo which has two portion one is SIGN UP and another is LOGIN,
While Developing you need to specify two field one is USERNAME and  another is PASSWORD. for both portion.
for Saving your credentials you require a Database. i am using here SQLiteDatabase to save the USERNAME and PASSWORD.

Below is code

Create a New Project LoginDemo.
Paste below code in class MainActivity.java

package com.example.logindemo;


import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
   
    EditText user,pass,sign_user,sign_pass;
    Button login,signup;
   
    DBHelper dbhelper;
    SQLiteDatabase mDB;
    Cursor cur;
   
    String user_name,password,signin_user,signin_pass;
   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        dbhelper = new DBHelper(this);
       
        sign_user = (EditText)findViewById(R.id.user);
        sign_pass = (EditText)findViewById(R.id.pass);
        signup = (Button)findViewById(R.id.signup);
       
        user = (EditText)findViewById(R.id.sign_user);
        pass = (EditText)findViewById(R.id.sign_pass);
        login = (Button)findViewById(R.id.singin);
       
        signup.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View arg0) {
               
                user_name = sign_user.getText().toString();
                password = sign_pass.getText().toString();
               
                if(user_name.equals("") || password.equals("")){
                    Toast.makeText(getApplicationContext(), "Plz Fill all fields", Toast.LENGTH_LONG).show();
                }else{
                    ContentValues cv = new ContentValues();
                    cv.put(DBHelper.USER_ID, user_name);
                    cv.put(DBHelper.PASS, password);
                   
                    Toast.makeText(getApplicationContext(), "Successfull", Toast.LENGTH_LONG).show();

                    mDB = dbhelper.getWritableDatabase();
                    try {
                        mDB.insert(DBHelper.LOGIN_TABLE, null, cv);
                       
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
               
               
               
               
               
            }
        });
       
        login.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View arg0) {
               
                signin_user = user.getText().toString();
                signin_pass = pass.getText().toString();
               
               
                if(signin_user.equals("")||signin_pass.equals("")){
                    Toast.makeText(getApplicationContext(), "Plz fill correctly", Toast.LENGTH_LONG).show();
                }else{
                   
                    mDB = dbhelper.getReadableDatabase();
                   
                    String query = "SELECT * FROM "+DBHelper.LOGIN_TABLE+"  WHERE "
                                +DBHelper.USER_ID+" = "+"'"+signin_user+"'"+" AND "+DBHelper.PASS+" = '"+signin_pass+"'";

                           
                            cur = mDB.rawQuery(query, null);
                           
                            Log.d("logggg ", query);
                            Log.d("count ", ""+cur.getCount());
                           
                            if(cur.getCount()>0){
                               
                                cur.moveToFirst();
                               
                                Log.d("cur va",""+cur.getString(cur.getColumnIndex(DBHelper.USER_ID)) );
                               
                                String user = cur.getString(cur.getColumnIndex(DBHelper.USER_ID));
                                String pass = cur.getString(cur.getColumnIndex(DBHelper.PASS));
                               
                                Toast.makeText(getApplicationContext(), user+" n "+pass, Toast.LENGTH_LONG).show();
                               
                                Intent intent = new Intent(MainActivity.this, Login.class);
                                startActivity(intent);
                               
                            }else{
                                Toast.makeText(getApplicationContext(), "Wrong User id or Pass", Toast.LENGTH_LONG).show();
                            }

                   
                }
               
                               
            }
        });
       
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
   
}

Post Below code in your xml file activity_main.xml

<LinearLayout 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:gravity="top"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="16dp"
                android:background="#c8c8c8"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dip"
                    android:text="Signup"
                    android:textSize="20dip" />

                <EditText
                    android:id="@+id/user"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="12dp"
                    android:layout_marginRight="12dp"
                    android:layout_marginTop="5dp"
                    android:hint="Name" />

                <EditText
                    android:id="@+id/pass"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="12dp"
                    android:layout_marginRight="12dp"
                    android:layout_marginTop="5dp"
                    android:inputType="textPassword"
                    android:hint="password" />

                <Button
                    android:id="@+id/signup"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="12dp"
                    android:layout_marginRight="12dp"
                    android:layout_marginTop="5dp"
                    android:text="Signup" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="16dp"
                android:background="#c8c8c8"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dip"
                    android:text="Login"
                    android:textSize="20dip" />

                <EditText
                    android:id="@+id/sign_user"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="12dp"
                    android:layout_marginRight="12dp"
                    android:layout_marginTop="5dp"
                    android:hint="Name" />

                <EditText
                    android:id="@+id/sign_pass"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="12dp"
                    android:layout_marginRight="12dp"
                    android:layout_marginTop="5dp"
                    android:inputType="textPassword"
                    android:hint="password" />

                <Button
                    android:id="@+id/singin"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="12dp"
                    android:layout_marginRight="12dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginBottom="16dp"
                    android:text="Login" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</LinearLayout>


Create a class DBHelper.java  and  paste below code in DBHelper.java class.

package com.example.logindemo;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper{
   
    public static int DB_VERSION =1;
    public static String DB_NAME = "login";
    public static String LOGIN_TABLE = "login_table";
    public static String LOGIN_ID = "login_id";
    public static String USER_ID = "login_user";
    public static String PASS = "login_pass";
   

    public DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
       
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

       
        String login = "CREATE TABLE "+LOGIN_TABLE+"("+LOGIN_ID+" INTEGER PRIMARY KEY,"+USER_ID+" TEXT, "+
                PASS+" TEXT )";
        db.execSQL(login);
       
    }

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





Create a class Login.java and paste below code in Login.java class.


package com.example.logindemo;



import android.app.Activity;
import android.os.Bundle;

public class Login extends Activity{
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
    }

}



create a xml file login.xml and paste below code in login.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Congrats !!"
        android:textSize="25dp"
        android:textColor="#6633cc"
        />
   

</LinearLayout>

and Run your project now dont forget to define your activity in manifest.xml.

Below is snapshot of the LoginDemo App.

SignUp

















No comments:

Post a Comment