Thursday, April 28, 2016

Registration & Login Simple One

" Jai Saraswati Maa" 
Hi ... Dear All ..Today I am uploading a superb example of Creating Registration Form with FirstName LastName and Password field. 
FirstName i am taken as userName for login Purpose.Please check.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.

A. Output Will be like 
a.        


b.       
            


B. XML files

a.  activity_main.xml   is as 

<?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:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.exam.ravi.customlistwithbutton.MainActivity">

    <EditText        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/logUser"

        android:layout_alignParentTop="true"

        android:layout_alignParentStart="true"

        android:layout_marginTop="54dp"        android:hint="UserName"

        android:layout_alignParentEnd="true" />

    <EditText        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/logPass"

        android:layout_below="@+id/logUser"

        android:layout_alignParentStart="true"

        android:layout_marginTop="40dp"

        android:hint="Password"        android:inputType="textPassword"

        android:layout_alignEnd="@+id/logUser" />

    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"        android:text="Login"

        android:id="@+id/logBut"

        android:onClick="loginUser"

        android:layout_centerVertical="true"

        android:layout_alignParentStart="true" />

    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"        android:text="New User"

        android:id="@+id/newuserBut"

        android:onClick="registerUser"

        android:layout_centerVertical="true"

        android:layout_alignParentEnd="true" />
</RelativeLayout>

b.  reguser.xml is as 

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

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

    android:orientation="vertical" android:layout_width="match_parent"

    android:layout_height="match_parent">

    <EditText        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/fName"

        android:layout_alignParentTop="true"

        android:layout_alignParentStart="true"

        android:layout_marginTop="58dp"

        android:hint="First Name"

        android:layout_alignParentEnd="true" />

    <EditText        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/lName"        android:layout_below="@+id/fName"

        android:layout_alignParentStart="true"

        android:layout_marginTop="42dp"

        android:hint="Last Name"

        android:layout_alignParentEnd="true" />

    <EditText        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:inputType="textPassword"

        android:ems="10"

        android:id="@+id/userPass"

        android:layout_centerVertical="true"

        android:hint=" Password "

        android:layout_alignParentStart="true"

        android:layout_alignParentEnd="true" />

    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Register"

        android:id="@+id/regist"

        android:layout_below="@+id/userPass"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="50dp"

        android:onClick="regMe"/>
</RelativeLayout>

C. Java Files
a.   MainActivity.java ias as

package com.exam.ravi.customlistwithbutton;

import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText EdLogUser,EdLogPass;
    String user,pass;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EdLogUser = (EditText) findViewById(R.id.logUser);
        EdLogPass = (EditText) findViewById(R.id.logPass);
    }
    public void loginUser(View view)
    {
        user = EdLogUser.getText().toString();
        pass = EdLogPass.getText().toString();
        MyHelp myHelp = new MyHelp(this);
        Cursor crob = myHelp.getInfo(myHelp);
        crob.moveToFirst();
        boolean logstatus = false;
        String NAME = "";
        do {
         if((user.equals(crob.getString(0))) && (pass.equals(crob.getString(1)))) {
                logstatus = true;
                NAME = crob.getString(0);
            }
        } while (crob.moveToNext());
        if (logstatus) {
            Toast.makeText(getBaseContext(), "Login Success .. . \n Welcome " + NAME, 
                                Toast.LENGTH_LONG).show();
           EdLogUser.setText("");
           EdLogPass.setText("");
        } else {
            Toast.makeText(getBaseContext(), "Login Failed..Please Check your detail",
                                Toast.LENGTH_LONG).show();
            EdLogUser.setText("");
            EdLogPass.setText("");
        }
    }


    public void  registerUser(View view)
      {
        startActivity(new Intent(this,RegUser.class));
      }
}

b. RegUser.java is as 

package com.exam.ravi.customlistwithbutton;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

/** * Created by Ravi on 4/28/2016. */public class RegUser extends AppCompatActivity
{
   EditText EdFname,EdLname,EdPass;
    String sFname,sLname,sPass;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reguser);
        EdFname = (EditText) findViewById(R.id.fName);
        EdLname = (EditText) findViewById(R.id.lName);
        EdPass = (EditText) findViewById(R.id.userPass);
    }
    public void regMe(View view)
    {
        sFname = EdFname.getText().toString();
        sLname = EdLname.getText().toString();
        sPass = EdPass.getText().toString();
        MyHelp myob = new MyHelp(this);
        myob.putInfo(myob, sFname, sLname,sPass);
        Toast.makeText(RegUser.this, "Registered Successfully...."
                                              Toast.LENGTH_SHORT).show();
        finish();

    }
}

c. MyHelp.java is as 
package com.exam.ravi.customlistwithbutton;

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;

/** * Created by Ravi on 4/28/2016. */public class MyHelp  extends SQLiteOpenHelper {
    public static final String KEY_ID="_id";
    private static final String DB_NAME="cont.db";
    private static final String TAB_NAME="info";
    private static final int DB_VER=1;
    private static final  String CREATE_QUERY = "create table " + TAB_NAME 
            " ( " + KEY_ID + " integer primary key autoincrement, " +
            UserInfo.F_NAME + " text not null, " + UserInfo.L_NAME + " text not null, 
            " + UserInfo.PASS + " text not null);";

    public MyHelp(Context context)
    {
        super(context,DB_NAME,null,DB_VER);
    }
    @Override    public void onCreate(SQLiteDatabase sdb) {

        sdb.execSQL(CREATE_QUERY);
        Log.d("rrrr", "oncreate");
    }

    @Override

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TAB_NAME);
        Log.d("rrrr", "onupgrade");
        onCreate(db);

    }
    public  void putInfo(MyHelp mob,String fname,String lname,String pass)
    {
        SQLiteDatabase SQ= mob.getWritableDatabase();
        ContentValues CV=new ContentValues();
        CV.put(UserInfo.F_NAME, fname);
        CV.put(UserInfo.L_NAME, lname);
        CV.put(UserInfo.PASS, pass);
        SQ.insert(TAB_NAME, null, CV);
    }
    public Cursor getInfo(MyHelp mob)
    {
        SQLiteDatabase SQ= mob.getWritableDatabase();
        String cols[]={UserInfo.F_NAME,UserInfo.PASS};
        Cursor CR= SQ.query(TAB_NAME,cols,null,null,null,null,null); 
        return  CR;
    }
}

d. UserInfo.java is as

package com.exam.ravi.customlistwithbutton;


public class UserInfo {
    public static final String F_NAME="f_name";
    public static final String L_NAME="l_name";
    public static final String PASS="pass";

}

No comments:

Post a Comment