user6313669 user6313669 - 2 months ago 13
Android Question

SIgn IN Sign Up with Sqlite in android .Unable to solve

I am creating sign-in sign up with Sqlite But unable to sign in. While sign up is working. Please help me where is the problem?
Signup is working fine but unable to click Sign in.

MainActivity

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
EditText editTextUserName, editTextPassword;
Button btnSignIn, btnSignUp;
// String userName,password;
LoginDataBaseAdapter loginDataBaseAdapter;

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

// create a instance of SQLite Database
loginDataBaseAdapter = new LoginDataBaseAdapter(this);
loginDataBaseAdapter = loginDataBaseAdapter.open();
btnSignIn = (Button) findViewById(R.id.buttonSignIn);
btnSignUp = (Button) findViewById(R.id.buttonSignUp);
// Get The Refference Of Buttons

// Set OnClick Listener on SignUp button
btnSignUp.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub

/// Create Intent for SignUpActivity abd Start The Activity
Intent intentSignUP = new Intent(getApplicationContext(), SignupActivity.class);
startActivity(intentSignUP);
}
});

}

// Methos to handleClick Event of Sign In Button
public void SignIn(View V)
{
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.signin);
dialog.setTitle("Login");

// get the Refferences of views
final EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
final EditText editTextPassword=(EditText)dialog.findViewById(R.id.editTextPasswordToLogin);

Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIn);

// Set On ClickListener
btnSignIn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// get The User name and Password
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();

// fetch the Password form database for respective user name
String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);

// check if the Stored password matches with Password entered by user
if(password.equals(storedPassword))
{
Toast.makeText(MainActivity.this, "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
else
{
Toast.makeText(MainActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
}
}
});

dialog.show();
}

@Override
protected void onDestroy() {
super.onDestroy();
// Close The Database
loginDataBaseAdapter.close();
}
}


activity_main

<?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:id="@+id/Mobileno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_gravity="center"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="@string/Registeresd_Mobile_Number"/>

<EditText
android:id="@+id/editTextUserName"
android:hint="User Name"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textSize="15dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>

<View
android:layout_width="300dp"
android:layout_height="2dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_gravity="center"
android:background="#c0c0c0"></View>

<TextView
android:id="@+id/editTextPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="@string/password"/>

<EditText
android:id="@+id/editTextPasswordToLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_gravity="center"
android:maxLength="20"
android:inputType="textPassword"
android:hint="Password" />

<View
android:layout_width="300dp"
android:layout_height="2dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:background="#c0c0c0"></View>

<Button
android:id="@+id/buttonSignIn"
android:layout_marginTop="20dp"
android:color="#7EC0EE"
android:layout_width="fill_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_height="wrap_content"
android:background="@drawable/rounded_edittext"
android:clickable="true"
android:text="Sign In" />

<TextView
android:id="@+id/Signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textSize="15dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:clickable="true"
android:text="@string/signup"/>

<Button
android:id="@+id/buttonSignUp"
android:layout_marginTop="20dp"
android:color="#7EC0EE"
android:layout_width="fill_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_height="wrap_content"
android:background="@drawable/rounded_edittext"
android:text="Sign Up" />

</LinearLayout>


Signup

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:fillViewport="false"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical" >

<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="@string/username"/>

<EditText
android:id="@+id/editTextUserName"
android:hint="User Name"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textSize="15dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>

<View
android:layout_width="300dp"
android:layout_height="2dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:background="#c0c0c0"></View>
<TextView
android:id="@+id/Email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="@string/Email"/>

<EditText
android:id="@+id/emailid"
android:hint="Emailid"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textSize="15dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>

<View
android:layout_width="300dp"
android:layout_height="2dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:background="#c0c0c0"></View>

<TextView
android:id="@+id/Contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="@string/contact"/>

<EditText
android:id="@+id/ContactNumber"
android:hint="Contact Number"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textSize="15dp"
android:maxLength="10"
android:inputType="number"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>

<View
android:layout_width="300dp"
android:layout_height="2dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:background="#c0c0c0"></View>

<TextView
android:id="@+id/Password1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="@string/password1"/>

<EditText
android:id="@+id/editTextPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textSize="15dp"
android:maxLength="20"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="Password"
android:inputType="textPassword" />

<View
android:layout_width="300dp"
android:layout_height="2dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:background="#c0c0c0"></View>
<TextView
android:id="@+id/confirmPassword1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="@string/confirmpassword1"/>

<EditText
android:id="@+id/editTextConfirmPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:hint="Confirm Password"
android:maxLength="20"
android:inputType="textPassword" />

<Button
android:id="@+id/buttonCreateAccount"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Create Account"
android:layout_marginBottom="60dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/rounded_edittext"/>

</LinearLayout>
</ScrollView>


Signup

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SignupActivity extends Activity
{
EditText editTextUserName,editTextPassword,editTextConfirmPassword,emailid,ContactNumber;
Button btnCreateAccount;

LoginDataBaseAdapter loginDataBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);

// get Instance of Database Adapter
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();

// Get Refferences of Views
editTextUserName=(EditText)findViewById(R.id.editTextUserName);
editTextPassword=(EditText)findViewById(R.id.editTextPassword);
editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);
emailid=(EditText)findViewById(R.id.emailid);
ContactNumber=(EditText)findViewById(R.id.ContactNumber);

btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount);
btnCreateAccount.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
String confirmPassword=editTextConfirmPassword.getText().toString();
String id=emailid.getText().toString();
String number= ContactNumber.getText().toString();

// check if any of the fields are vaccant
if(userName.equals("")||password.equals("")||confirmPassword.equals(""))
{
Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
return;
}
// check if both password matches
if(!password.equals(confirmPassword))
{
Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
return;
}



/* if(!emailid(isValidEmail(charSequence target)))
{



}*/
else
{
// Save the Data in Database
loginDataBaseAdapter.insertEntry(number, password);
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
}

}
});
}

public final static boolean isValidEmail(CharSequence target) {
if (target == null) {
return false;
} else {
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();

loginDataBaseAdapter.close();
}
}


logindatabaseadapter

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class LoginDataBaseAdapter
{
static final String DATABASE_NAME = "login.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
static final String DATABASE_CREATE = "create table "+"LOGIN"+
"( " +"ID"+" integer primary key autoincrement,"+ "USERNAME text,PASSWORD text); ";
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public LoginDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public LoginDataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}

public SQLiteDatabase getDatabaseInstance()
{
return db;
}

public void insertEntry(String userName,String password)
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("USERNAME", userName);
newValues.put("PASSWORD",password);

// Insert the row into your table
db.insert("LOGIN", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
public int deleteEntry(String UserName)
{
//String id=String.valueOf(ID);
String where="USERNAME=?";
int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ;
// Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
return numberOFEntriesDeleted;
}
public String getSinlgeEntry(String userName)
{
Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
cursor.close();
return password;
}
public void updateEntry(String userName,String password)
{
// Define the updated row content.
ContentValues updatedValues = new ContentValues();
// Assign values for each row.
updatedValues.put("USERNAME", userName);
updatedValues.put("PASSWORD",password);

String where="USERNAME = ?";
db.update("LOGIN",updatedValues, where, new String[]{userName});
}
}


databasehelper

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

/**
* Created by Neha Thakur on 5/23/2016.
*/
public class DataBaseHelper extends SQLiteOpenHelper
{
public DataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
{
super(context, name, factory, version);
}
// Called when no database exists in disk and the helper class needs
// to create a new one.
@Override
public void onCreate(SQLiteDatabase _db)
{
_db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE);

}
// Called when there is a database version mismatch meaning that the version
// of the database on disk needs to be upgraded to the current version.
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
{
// Log the version upgrade.
Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data");

// Upgrade the existing database to conform to the new version. Multiple
// previous versions can be handled by comparing _oldVersion and _newVersion
// values.
// The simplest case is to drop the old table and create a new one.
_db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
// Create a new one.
onCreate(_db);
}

}


Signin

<?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:id="@+id/Mobileno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_gravity="center"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="@string/Registeresd_Mobile_Number"/>

<EditText
android:id="@+id/editTextUserNameToLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_gravity="center"
android:inputType="number"
android:maxLength="10"
android:hint="Mobile Number">
</EditText>

<View
android:layout_width="300dp"
android:layout_height="2dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_gravity="center"
android:background="#c0c0c0"></View>

<TextView
android:id="@+id/editTextPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="@string/password"/>

<EditText
android:id="@+id/editTextPasswordToLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_gravity="center"
android:maxLength="20"
android:inputType="textPassword"
android:hint="Password" />

<View
android:layout_width="300dp"
android:layout_height="2dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:background="#c0c0c0"></View>

<Button
android:id="@+id/buttonSignIn"
android:layout_marginTop="20dp"
android:color="#7EC0EE"
android:layout_width="fill_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_height="wrap_content"
android:background="@drawable/rounded_edittext"
android:text="Sign In" />
</LinearLayout>

Answer

Add onClick method also to your signIn button.

<Button

        android:id="@+id/buttonSignIn"
        android:layout_marginTop="20dp"
        android:color="#7EC0EE"
        android:layout_width="fill_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_edittext"
        <!-- Here -->
        android:onClick="SignIn"
        android:clickable="true"
        android:text="Sign In" />