Friday, 26 August 2016

Login and Signup in Android Studio By using SQLite Database

1. main_activity.xml:-
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="androidhubb.myproject.MainActivity"
    android:orientation="vertical"
    android:gravity="center">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@mipmap/logo1"
        android:id="@+id/imgLogo"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Copywrite@androidhubb\nVersion 1.1.0"
        android:gravity="center"/>
</LinearLayout>

2. MainActivity.java
package androidhubb.myproject;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    Animation animation;
    ImageView imgLogo;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Imageview...        imgLogo=(ImageView)findViewById(R.id.imgLogo);
        //animation on image..        animation= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade);
        imgLogo.startAnimation(animation);
        //spalsh screen....        Thread t=new Thread(){
            public void run(){
                try{
                   sleep(5000);
                    startActivity(new Intent(MainActivity.this,SignUp.class));
                    finish();
                }
                catch (Exception e){}
            }
        };
        t.start();
    }
    protected void onDestroy(){
        super.onDestroy();
    }
}

3. signup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_margin="16dp">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Email Id"
                android:layout_marginTop="16dp"
                android:inputType="textWebEmailAddress"
                android:id="@+id/txt_email"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Phone No"
                android:layout_marginTop="16dp"
                android:inputType="number"
                android:id="@+id/txt_phone"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Gender"
                android:textSize="20sp"
                android:layout_marginTop="16dp"/>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginTop="10dp">
                <RadioGroup
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/rdgroup"
                    android:orientation="horizontal">
                    <RadioButton
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="wrap_content"
                        android:text="Male"
                        android:id="@+id/radio_male"/>
                    <RadioButton
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="wrap_content"
                        android:text="FeMale"
                        android:id="@+id/radio_female"/>
                </RadioGroup>
            </LinearLayout>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Birthday(dd/mm//yyyy)"
                android:layout_marginTop="16dp"
                android:id="@+id/edtdate"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="password"
                android:layout_marginTop="16dp"
                android:id="@+id/edtpassword"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Signup"
                android:layout_marginTop="20dp"
                android:id="@+id/btnSignup"
                android:background="@drawable/round_button"
                android:textColor="#ffffff"
                android:textSize="20sp" />
            <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Login"
            android:textSize="25sp"
            android:textAlignment="center"
            android:layout_marginTop="16dp"
            android:textColor="#f05f5f"
            android:id="@+id/txtLogin"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="View User Details"
                android:textSize="20sp"
                android:textAlignment="center"
                android:layout_marginTop="16dp"
                android:textColor="#000000"
                android:id="@+id/txtview"/>
        </LinearLayout>
    </ScrollView>
</LinearLayout>


4. SignUp.java

package androidhubb.myproject;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

/** * Created by Supriya on 8/19/2016. */public class SignUp extends AppCompatActivity {

    EditText edtEmail,edtPhone,edtDate,edtpass;
    RadioButton rMale,rFemale;
    RadioGroup rdGroup;
    TextView tLogin,tvview;
    SQLiteDatabase db;
    Button btnSignup;
    String gender="";

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

        //database        db=openOrCreateDatabase("MyDatabase1", Context.MODE_PRIVATE,null);
        db.execSQL("create table if not exists MyTable1(ID integer primary key autoincrement not null,EMAIL varchar,PHONE varchar,GENDER varchar,BIRTH varchar,PASSWORD varchar)");

        //find id..        btnSignup=(Button)findViewById(R.id.btnSignup);
        edtEmail=(EditText)findViewById(R.id.txt_email);
        edtPhone=(EditText)findViewById(R.id.txt_phone);
        edtDate=(EditText)findViewById(R.id.edtdate);
        edtpass=(EditText)findViewById(R.id.edtpassword);

        rdGroup=(RadioGroup)findViewById(R.id.rdgroup);
        rMale=(RadioButton)findViewById(R.id.radio_male);
        rFemale=(RadioButton)findViewById(R.id.radio_female);
        tLogin=(TextView)findViewById(R.id.txtLogin);
        tvview=(TextView)findViewById(R.id.txtview);

        //signup        btnSignup.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View view) {
                ContentValues cv=new ContentValues();
                cv.put("EMAIL",edtEmail.getText().toString().trim());
                cv.put("PHONE",edtPhone.getText().toString().trim());
                cv.put("GENDER",gender.toString().trim());
                cv.put("BIRTH",edtDate.getText().toString().trim());
                cv.put("PASSWORD",edtpass.getText().toString().trim());
                Long i=db.insert("MyTable1",null,cv);
                if(i>0){
                    Toast.makeText(SignUp.this,"Data Inserted",Toast.LENGTH_LONG).show();
                    edtDate.setText("");
                    edtEmail.setText("");
                    edtPhone.setText("");
                    edtpass.setText("");
                    rdGroup.clearCheck();
                }
            }
        });

   rdGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
       @Override       public void onCheckedChanged(RadioGroup radioGroup, int i) {
           if(i==R.id.radio_male){
               gender="Male";
           }
           else {
               gender="Female";
           }
       }
   });

        //login nevigation....        
tLogin.setOnClickListener(new View.OnClickListener() {
            @Override            
public void onClick(View view) {
                startActivity(new Intent(SignUp.this,Login.class));
            }
        });
        //view nevigation....        
tvview.setOnClickListener(new View.OnClickListener() {
            @Override            
public void onClick(View view) {
                startActivity(new Intent(SignUp.this,UserList.class));
            }
        });
        //listner...        edtDate.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View view) {

                DialogFragment dFragment=new DatePickerDialogClass();
                dFragment.show(getSupportFragmentManager(),"Select date");
            }
        });

    }

    public static class DatePickerDialogClass extends DialogFragment implements DatePickerDialog.OnDateSetListener{
        @NonNull        @Override        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Calendar c=Calendar.getInstance();
           final int year=c.get(Calendar.YEAR);
            int month=c.get(Calendar.MONTH);
            int date=c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog datePickerDialog=new DatePickerDialog(getActivity(), android.app.AlertDialog.THEME_DEVICE_DEFAULT_DARK,this,year,month,date);

            return datePickerDialog;
        }

        @Override        
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
        EditText edate=(EditText)getActivity().findViewById(R.id.edtdate);
            edate.setText(i2+"/"+(i1+1)+"/"+i);

        }
    }

}
5.userlist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list1"></ListView>

</LinearLayout>

6. UserList.java
package androidhubb.myproject;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import java.util.ArrayList;

/** * Created by Supriya on 8/31/2016. */public class UserList extends AppCompatActivity {
    ListView listView;
    SQLiteDatabase db;
    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_list);
        listView=(ListView)findViewById(R.id.list1);
        db=openOrCreateDatabase("MyDatabase1", Context.MODE_PRIVATE,null);
        onUserListView();
    }
    public void onUserListView(){
        ArrayList<String> email_list=new ArrayList<String>();
        ArrayList<String> phone_list=new ArrayList<String>();
        ArrayList<String> gender_list=new ArrayList<String>();
        ArrayList<String> birth_list=new ArrayList<String>();
        ArrayList<String> pass_list=new ArrayList<String>();
        email_list.clear();
        phone_list.clear();
        gender_list.clear();
        birth_list.clear();
        pass_list.clear();
        Cursor c=db.rawQuery("select * from MyTable1",null);
        while (c.moveToNext()){
            email_list.add(c.getString(1));
            phone_list.add(c.getString(2));
            gender_list.add(c.getString(3));
            birth_list.add(c.getString(4));
            pass_list.add(c.getString(5));
        }
        listView.setAdapter(new Adapter(UserList.this,email_list,phone_list,gender_list,birth_list,pass_list));

    }
}

7. adapter.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="email"
        android:textSize="25sp"
        android:id="@+id/tvEmail"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="email"
        android:textSize="25sp"
        android:id="@+id/tvPhone"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="email"
        android:textSize="25sp"
        android:id="@+id/tvGender"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Birth"
        android:textSize="25sp"
        android:id="@+id/Birth"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password"
        android:textSize="25sp"
        android:id="@+id/tvPass"/>

</LinearLayout>
8.login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:gravity="center">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Email Id"
                android:layout_marginTop="16dp"
                android:inputType="textWebEmailAddress"
                android:id="@+id/txt_email"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Password"
                android:layout_marginTop="16dp"
                android:inputType="textPassword"
                android:id="@+id/txt_pass"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Login"
                android:layout_marginTop="20dp"
                android:id="@+id/btnlogin"
                android:background="@drawable/round_button"
                android:textColor="#ffffff"
                android:textSize="20sp"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="SignUp"
                android:textSize="25sp"
                android:textAlignment="center"
                android:layout_marginTop="16dp"
                android:textColor="#f05f5f"
                android:id="@+id/txtsignup"/>

</LinearLayout>
Step 8:Login.java
package androidhubb.myproject;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/** * Created by Supriya on 8/19/2016. */public class Login extends AppCompatActivity {
    TextView tvSign;
    EditText edtEmail,edtpass;
    Button btnLogin;
    SQLiteDatabase db;

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

        edtEmail=(EditText)findViewById(R.id.txt_email);
        edtpass=(EditText)findViewById(R.id.txt_pass);
        btnLogin=(Button)findViewById(R.id.btnlogin);
        tvSign=(TextView)findViewById(R.id.txtsignup);

        db=openOrCreateDatabase("MyDatabase1", Context.MODE_PRIVATE,null);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View view) {

                String email=edtEmail.getText().toString().trim();
                String pass=edtpass.getText().toString().trim();
                Cursor c=db.rawQuery("select * from MyTable1 where EMAIL=? AND PASSWORD=?",new String[]{email,pass});
                if(c.moveToFirst()){
                    Toast.makeText(Login.this,"Success",Toast.LENGTH_LONG).show();
                    startActivity(new Intent(Login.this,WelcomeClass.class));
                    edtpass.setText("");
                    edtEmail.setText("");
                }
                else{
                    Toast.makeText(Login.this,"invalid details",Toast.LENGTH_LONG).show();
                }

            }
        });

        //login nevigation....        tvSign.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View view) {
                startActivity(new Intent(Login.this,SignUp.class));
            }
        });
    }
}

No comments:

Post a Comment