Login and Register Source Code With Instruction
Login and Register Source Code With Instruction
Login and Register Source Code With Instruction
//package com.demo.easylearn;
import android.app.Dialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
db = new DatabaseHelper(this);
mTextUsername = (EditText)findViewById(R.id.edittext_username);
mTextPassword = (EditText)findViewById(R.id.edittext_password);
mButtonLogin = (Button)findViewById(R.id.button_login);
mTextViewRegister = (TextView)findViewById(R.id.textview_register);
mTextViewRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent registerIntent = new
Intent(LoginActivity.this,RegisterActivity.class);
startActivity(registerIntent);
}
});
mButtonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String user = mTextUsername.getText().toString().trim();
String pwd = mTextPassword.getText().toString().trim();
Boolean res = db.checkUser(user, pwd);
if(res == true)
{
Intent HomePage = new
Intent(LoginActivity.this,HomeActivity.class);
startActivity(HomePage);
}
else
{
Toast.makeText(LoginActivity.this,"Login
Error",Toast.LENGTH_SHORT).show();
}
}
});
}
if (!isProgressShowing) {
View view=findViewById(R.id.progressBar1);
view.bringToFront();
}
}
___________________________________________________________________________________
______________________________________________________
Step 2: Add the below lines your layout file ie. activity_login.xml (under
app/res/layout/activity_login.xml)
<ImageView
android:layout_width="wrap_content"
android:layout_height="127dp"
android:layout_marginTop="50dp"
app:srcCompat="@drawable/logo"/>
<LinearLayout
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="#c2ffffff"
android:orientation="vertical">
<EditText
android:id="@+id/edittext_username"
android:layout_width="190dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:paddingLeft="2dp"
android:background="#ffffff"
android:drawableLeft="@drawable/username"
android:hint="@string/username"/>
<EditText
android:id="@+id/edittext_password"
android:layout_width="190dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#ffffff"
android:drawableLeft="@drawable/password"
android:hint="@string/password"/>
</LinearLayout>
<Button
android:id="@+id/button_login"
android:layout_width="190dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#04ea00"
android:textColor="#ffffff"
android:text="@string/login"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:textColor="#ffffff"
android:text="@string/not_registered"/>
<TextView
android:id="@+id/textview_register"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:textStyle="bold"
android:textColor="#04ea00"
android:text="@string/register"/>
</LinearLayout>
</LinearLayout>
___________________________________________________________________________________
_______________________________________________________
Step 3: Create another Activity, follow the steps- Click on App -> Click on Java ->
Right click on package ->Click on New -> Click on empty Activity -> Write a name
(ie. RegisterActivity and layout activity_register.xml). Then copy the below code
in
RegisterActivity.java file.
[NOTE: Don't forget to give your package name which we give during creating the
project. I have given package name as - com.demo.easylear ;]
//RegisterActivity.java
//package com.demo.easylearn;
import android.content.Intent;
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.TextView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
db = new DatabaseHelper(this);
mTextUsername = (EditText)findViewById(R.id.edittext_username);
mTextPassword = (EditText)findViewById(R.id.edittext_password);
mTextCnfPassword = (EditText)findViewById(R.id.edittext_cnf_password);
mButtonRegister = (Button)findViewById(R.id.button_register);
mTextViewLogin = (TextView)findViewById(R.id.textview_login);
mTextViewLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent LoginIntent = new
Intent(RegisterActivity.this,LoginActivity.class);
startActivity(LoginIntent);
}
});
mButtonRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String user = mTextUsername.getText().toString().trim();
String pwd = mTextPassword.getText().toString().trim();
String cnf_pwd = mTextCnfPassword.getText().toString().trim();
if(pwd.equals(cnf_pwd)){
long val = db.addUser(user,pwd);
if(val > 0){
Toast.makeText(RegisterActivity.this,"You have
registered",Toast.LENGTH_SHORT).show();
Intent moveToLogin = new
Intent(RegisterActivity.this,LoginActivity.class);
startActivity(moveToLogin);
}
else{
Toast.makeText(RegisterActivity.this,"Registeration
Error",Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(RegisterActivity.this,"Password is not
matching",Toast.LENGTH_SHORT).show();
}
}
});
}
}
___________________________________________________________________________________
______________________________________________________
Step 4: Copy the below lines and add it to your activity_register.xml file.
//activity_register.xml
<ImageView
android:layout_width="wrap_content"
android:layout_height="127dp"
android:layout_marginTop="50dp"
app:srcCompat="@drawable/logo"/>
<EditText
android:id="@+id/edittext_username"
android:layout_width="206dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:paddingLeft="2dp"
android:background="#ffffff"
android:drawableLeft="@drawable/username"
android:hint="@string/username"/>
<EditText
android:id="@+id/edittext_password"
android:layout_width="206dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#ffffff"
android:drawableLeft="@drawable/password"
android:hint="@string/password"/>
<EditText
android:id="@+id/edittext_cnf_password"
android:layout_width="206dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#ffffff"
android:drawableLeft="@drawable/password"
android:hint="@string/confirm_password"/>
<Button
android:id="@+id/button_register"
android:layout_width="206dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#04ea00"
android:textColor="#ffffff"
android:text="@string/register"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:textColor="#ffffff"
android:text="@string/already_registered"/>
<TextView
android:id="@+id/textview_login"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:textStyle="bold"
android:textColor="#04ea00"
android:textSize="16sp"
android:text="@string/login"/>
</LinearLayout>
</LinearLayout>
___________________________________________________________________________________
______________________________________________________
Step 5. Create a class. Click on app -> Click on java -> Right click on your
package name -> Click on new -> Click on java class -> Write name as
DatabaseHelper -> click ok.
Add the below lines in your DatabaseHelper class.
//DatabaseHelper.java
//package com.demo.easylearn;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE registeruser (ID INTEGER PRIMARY KEY
AUTOINCREMENT, username TEXT, password TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
if(count>0)
return true;
else
return false;
}
}
___________________________________________________________________________________
______________________________________________________
Step 6: This is optional. If you want to go to homepage after login add the below
lines in a new activity. Click on app -> Click on java -> Right click on package
name -> Click on new -> Click on empty activity -> Write name of the activity as
HomeActivity and layout name as activity_home.
//HomeActivity.java
//package com.demo.easylearn;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
}
___________________________________________________________________________________
______________________________________________________
Step 7: Add the below lines in activity_home.xml file.
//activity_home
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome_user"
android:textColor="#123456"
android:textSize="23sp"
tools:layout_editor_absoluteX="151dp"
tools:layout_editor_absoluteY="227dp" />
</LinearLayout>
___________________________________________________________________________________
______________________________________________________
Step 8 : To add progressbar layout. Click on app -> click on res -> Right click on
layout -> click new -> Click on layout resource file -> Write name as progressbar
-> Click ok. Now add the below line in progressbar.xml layout file.
//progressbar.xml
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:indeterminateDrawable="@drawable/my_drawable"
/>
</RelativeLayout>
___________________________________________________________________________________
________________________________________________________________________
Step 9: Add the below codes under resource tag in colors.xml file. Click on app ->
res -> values -> colors.xml
//colors.xml
<color name="color_preloader_start">#fdb6b6</color>
<color name="color_preloader_center">#fdb6b6</color>
<color name="color_preloader_end">#ffffff</color>
</resources>
___________________________________________________________________________________
________________________________________________________________________
Step 10: Add the below codes under resource tag in colors.xml file. Click on app ->
res -> values -> colors.xml
//strings.xml
<resources>
<string name="app_name">AndroidCam</string>
<string name="action_settings">Settings</string>
</resources>
___________________________________________________________________________________
________________________________________________________________________
//Step 11. Add the images. Right click on the image and copy it. Now go to Android
Studio- Click on app -> res -> Right click on drawable -> Click Paste.
Or else find your project in file explorer, find the drawable folder under res and
copy all your images here.