Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mdad.project;
- import com.example.manandhowproject.R;
- import android.app.Activity;
- import android.content.Intent;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class Login extends Activity {
- SQLiteDatabase db;
- Button btnLogin, btnSignUp;
- EditText etUsername, etPassword;
- SQLiteOpenHelper dbhelper;
- Cursor cursor;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.login);
- etUsername = (EditText)findViewById(R.id.etUsername);
- etPassword = (EditText)findViewById(R.id.etPassword);
- Button button = (Button)findViewById(R.id.btnLogin);
- button.setOnClickListener(new OnClickListener(){
- public void onClick (View V){
- if(login(etUsername.getText().toString(), etPassword.getText().toString()) == 1){
- Toast.makeText(getApplicationContext(), "Log In Success ! ", Toast.LENGTH_LONG).show();
- Intent msg1 = new Intent(Login.this, Userreg.class);
- startActivity(msg1);
- }else{
- Toast.makeText(getApplicationContext(), "Error Logging In, Please Try Again", Toast.LENGTH_LONG).show();
- }
- }
- });
- Button btnSignUp = (Button) findViewById (R.id.btnSignUp);
- btnSignUp.setOnClickListener(new OnClickListener (){
- public void onClick (View V) {
- Toast.makeText(getApplicationContext(), "Sign Up Success ! ", Toast.LENGTH_LONG).show();
- String username= etUsername.getText().toString();
- String password= etPassword.getText().toString();
- String sql = "insert into Registration (Username ,Password) values( '"+username+"','"+password+"')";
- String result = updateTable(sql);
- etUsername.setText("");
- etPassword.setText("");
- }});
- String sql="create table if not exists Registration (recld integer PRIMARY KEY autoincrement, Username text, Password text)";
- String result = createDatabase(sql, "Reg.db");
- }
- public int login(String username, String password){
- String[] selectionArgs = new String[]{username, password};
- try
- {
- int i = 0;
- Cursor c = null;
- c = db.rawQuery("select * from Registration where username=? and password=?", selectionArgs);
- c.moveToFirst();
- i = c.getCount();
- c.close();
- return i;
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- return 0;
- }
- String createDatabase(String sql, String dbName) {
- try{
- System.out.println(sql);
- db = SQLiteDatabase.openOrCreateDatabase("sdcard/" + dbName,null);
- db.beginTransaction();
- db.execSQL(sql);
- db.setTransactionSuccessful();
- db.endTransaction();
- }
- catch (Exception e){
- Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
- System.out.println(e.toString());
- return ("error open DB");
- }
- return "";
- }
- String updateTable(String sql)
- {
- try{
- System.out.println(sql);
- db.beginTransaction();
- db.execSQL(sql);
- db.setTransactionSuccessful();
- db.endTransaction();
- }catch (Exception e) { System.out.println(e.toString());
- return ("Error updating DB");
- }
- return ("DB updated");
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
Add Comment
Please, Sign In to add comment