1) create login.xml (activity layout) in / res / layout In this example, there is an additional option to change the password
<?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" > <EditText android:id="@+id/username" android:hint="Username" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_marginTop="180dp" android:layout_gravity="center" android:ems="10" > <requestFocus /> </EditText> <EditText android:id="@+id/password" android:layout_width="250dp" android:hint="Password" android:layout_marginTop="20dp" android:layout_gravity="center" android:layout_height="wrap_content" android:ems="10" android:inputType="textPassword" /> <Button android:id="@+id/Loginbutton" android:layout_gravity="center" android:layout_marginTop="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login" /> <CheckedTextView android:id="@+id/changepassword" android:layout_width="wrap_content" android:layout_marginTop="75dp" android:clickable="true" android:layout_marginLeft="190dp" android:layout_height="wrap_content" android:text="Change_Login_Id" /> </LinearLayout>
2) create the Login.java class in / src //
public class Login extends Activity{ private Button login; private EditText Username; private EditText Password; private CheckedTextView changeid; public SQLiteDatabase sampleDB; public String COLUMN_ID="_id"; public String COLUMN1="username"; public String COLUMN2="password"; public String TABLE_NAME="Androdata"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); login=(Button)findViewById(R.id.Loginbutton); Username=(EditText)findViewById(R.id.username); Password=(EditText)findViewById(R.id.password); changeid=(CheckedTextView)findViewById(R.id.changepassword); sampleDB = this.openOrCreateDatabase(TABLE_NAME, MODE_PRIVATE, null); boolean x=init(TABLE_NAME); if(x==false) { createDB(); insertDB(); } changeid.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent myintent=new Intent("android.intent.action.DATABASE"); startActivity(myintent); } }); login.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int k=check(); if(k==1) { Toast.makeText(Login.this, "Login Successful ...", Toast.LENGTH_SHORT).show(); Intent myintent=new Intent("android.intent.action.CHOICE"); startActivity(myintent); } else { Username.setText(""); Password.setText(""); Toast.makeText(Login.this, "Authentication Failed...", Toast.LENGTH_SHORT).show(); } } }); } public boolean init(String tableName) { Cursor cursor = sampleDB.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+tableName+"'", null); if(cursor!=null) { if(cursor.getCount()>0) { cursor.close(); return true; } cursor.close(); } return false; } private void insertDB() { sampleDB.execSQL("INSERT INTO " + TABLE_NAME + " Values ('1','Androviewer','viewer');"); System.out.println("Inserted data successfully...."); } private void createDB() { sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME+ "(" + COLUMN_ID + " integer primary key autoincrement, " + COLUMN1 + " text not null,"+ COLUMN2 + " text not null);"); System.out.println("Database created successfully...."); } private int check() { String a=Username.getText().toString(); String b=Password.getText().toString();
source share