-
-
Save codixil/d632476dff65e903dfc17a9ff6181ba5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:gravity="center" | |
tools:context=".HomeActivity"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="מסך הבית" | |
android:textSize="30sp" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:gravity="center" | |
android:orientation="vertical" | |
android:padding="20sp" | |
tools:context=".SignInActivity"> | |
<EditText | |
android:id="@+id/email_et" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="10sp" | |
android:gravity="right" | |
android:hint="אימייל:" | |
android:inputType="textEmailAddress" /> | |
<EditText | |
android:id="@+id/password_et" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="10sp" | |
android:gravity="right" | |
android:hint=":סיסמה" | |
android:inputType="textPassword" /> | |
<Button | |
android:id="@+id/sign_in_btn" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="התחברות" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:gravity="center" | |
android:orientation="vertical" | |
android:padding="20sp" | |
tools:context=".SignUpActivity"> | |
<EditText | |
android:id="@+id/email_et" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="10sp" | |
android:gravity="right" | |
android:hint="אימייל:" | |
android:inputType="textEmailAddress" /> | |
<EditText | |
android:id="@+id/password_et" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="10sp" | |
android:gravity="right" | |
android:hint=":סיסמה" | |
android:inputType="textPassword" /> | |
<EditText | |
android:id="@+id/confirm_password_et" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="10sp" | |
android:gravity="right" | |
android:hint=":סיסמה שוב" | |
android:inputType="textPassword" /> | |
<Button | |
android:id="@+id/sign_up_btn" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="הרשמה" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codix.app; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
import androidx.annotation.Nullable; | |
public class DBHelper extends SQLiteOpenHelper { | |
public static final String USERS_DATABASE = "users.db"; | |
public static final String LOGIN_TABLE = "login"; | |
public static final String COL_ID = "id"; | |
public static final String COL_EMAIL = "email"; | |
public static final String COL_PASSWORD = "password"; | |
public DBHelper(@Nullable Context context) { | |
super(context, USERS_DATABASE, null, 1); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase sqLiteDatabase) { | |
sqLiteDatabase.execSQL("CREATE TABLE " + LOGIN_TABLE + " (id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT, password TEXT)"); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { | |
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + LOGIN_TABLE); | |
onCreate(sqLiteDatabase); | |
} | |
public boolean signUp(String email, String password) { | |
SQLiteDatabase db = this.getWritableDatabase(); | |
ContentValues cv = new ContentValues(); | |
cv.put("email", email); | |
cv.put("password", password); | |
long rowId = db.insert(LOGIN_TABLE, null, cv); | |
db.close(); | |
return rowId != -1; | |
} | |
public boolean signIn(String email, String password) { | |
SQLiteDatabase db = this.getReadableDatabase(); | |
String[] columns = {COL_ID}; | |
String selection = COL_EMAIL + "=?" + " and " + COL_PASSWORD + "=?"; | |
String[] selectionArgs = {email, password}; | |
Cursor cursor = db.query(LOGIN_TABLE, columns, selection, selectionArgs, null, null, null); | |
int count = cursor.getCount(); | |
db.close(); | |
cursor.close(); | |
return count >= 1; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codix.app; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.util.Patterns; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
public class SignInActivity extends AppCompatActivity { | |
EditText emailEt, passwordEt; | |
Button signInBtn; | |
DBHelper db; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_sign_in); | |
emailEt = findViewById(R.id.email_et); | |
passwordEt = findViewById(R.id.password_et); | |
signInBtn = findViewById(R.id.sign_in_btn); | |
db = new DBHelper(this); | |
signInBtn.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
signIn(); | |
} | |
}); | |
} | |
public void signIn() { | |
String email = emailEt.getText().toString(); | |
String password = passwordEt.getText().toString(); | |
if (isValidEmail(email)) { | |
if (password.length() >= 4) { | |
boolean isSuccessful = db.signIn(email, password); | |
if (isSuccessful) | |
startActivity(new Intent(getApplicationContext(), HomeActivity.class)); | |
else | |
Toast.makeText(getApplicationContext(), "הפרטים לא נכונים", Toast.LENGTH_SHORT).show(); | |
} else | |
Toast.makeText(getApplicationContext(), "הסיסמה קצרה מדי", Toast.LENGTH_SHORT).show(); | |
} else | |
Toast.makeText(getApplicationContext(), "האימייל לא תקין", Toast.LENGTH_SHORT).show(); | |
} | |
public boolean isValidEmail(String email) { | |
return Patterns.EMAIL_ADDRESS.matcher(email).matches(); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
menu.add("הרשמה"); | |
return super.onCreateOptionsMenu(menu); | |
} | |
@Override | |
public boolean onOptionsItemSelected(@NonNull MenuItem item) { | |
if (item.getTitle().equals("הרשמה")) | |
startActivity(new Intent(getApplicationContext(), SignUpActivity.class)); | |
return super.onOptionsItemSelected(item); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codix.app; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.util.Patterns; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
public class SignUpActivity extends AppCompatActivity { | |
EditText emailEt, passwordEt, confirmPasswordEt; | |
Button signUpBtn; | |
DBHelper db; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_sign_up); | |
emailEt = findViewById(R.id.email_et); | |
passwordEt = findViewById(R.id.password_et); | |
confirmPasswordEt = findViewById(R.id.confirm_password_et); | |
signUpBtn = findViewById(R.id.sign_up_btn); | |
db = new DBHelper(this); | |
signUpBtn.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
signUp(); | |
} | |
}); | |
} | |
public void signUp() { | |
String email = emailEt.getText().toString(); | |
String password = passwordEt.getText().toString(); | |
String confirmPassword = confirmPasswordEt.getText().toString(); | |
if (isValidEmail(email)) { | |
if (password.length() >= 4) { | |
if (password.equals(confirmPassword)) { | |
boolean isSuccessful = db.signUp(email, password); | |
if (isSuccessful) { | |
startActivity(new Intent(getApplicationContext(), HomeActivity.class)); | |
Toast.makeText(getApplicationContext(), "נרשמת בהצלחה", Toast.LENGTH_SHORT).show(); | |
} else | |
Toast.makeText(getApplicationContext(), "שגיאה בהרשמה", Toast.LENGTH_SHORT).show(); | |
} else | |
Toast.makeText(getApplicationContext(), "הסיסמאות לא תואמות", Toast.LENGTH_SHORT).show(); | |
} else | |
Toast.makeText(getApplicationContext(), "הסיסמה קצרה מדי", Toast.LENGTH_SHORT).show(); | |
} else | |
Toast.makeText(getApplicationContext(), "האימייל לא תקין", Toast.LENGTH_SHORT).show(); | |
} | |
public boolean isValidEmail(String email) { | |
return Patterns.EMAIL_ADDRESS.matcher(email).matches(); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
menu.add("התחברות"); | |
return super.onCreateOptionsMenu(menu); | |
} | |
@Override | |
public boolean onOptionsItemSelected(@NonNull MenuItem item) { | |
if (item.getTitle().equals("התחברות")) | |
startActivity(new Intent(getApplicationContext(), SignInActivity.class)); | |
return super.onOptionsItemSelected(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment