Wednesday, 11 March 2015

Android Login Screen Tutorial





DBAdapter.java:-
package com.my.members;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.Cursor;
public class DBAdapter
import android.database.sqlite.SQLiteDatabase; {
public static final String KEY_ROW_ID = "_id";
private static final String DATABASE_TABLE = "members";
public static final String KEY_PASSWORD = "password";
public static final String KEY_USERNAME = "username"; SQLiteDatabase mDb; Context mCtx;
public DBAdapter open() throws SQLException
DBHelper mDbHelper; public DBAdapter(Context context) { this.mCtx = context; } {
{
mDbHelper = new DBHelper(mCtx); mDb = mDbHelper.getWritableDatabase(); return this; } public void close()
ContentValues initialValues = new ContentValues();
mDbHelper.close(); } public long register(String user,String pw) { initialValues.put(KEY_USERNAME, user);
public boolean Login(String username, String password) throws SQLException
initialValues.put(KEY_PASSWORD, pw); return mDb.insert(DATABASE_TABLE, null, initialValues); } {
if (mCursor != null) {
Cursor mCursor = mDb.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE username=? AND password=?", new String[]{username,password}); if(mCursor.getCount() > 0) { return true; } }
}
return false;
}
DBHelper.java:-
package com.my.members;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
private static final String DATABASE_NAME = "membersdb";
public class DBHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "CREATE TABLE (_id integer primary key autoincrement,username text not null,password text not null);";
public DBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE); } @Override
}
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { db.execSQL("DROP TABLE IF EXISTS members"); onCreate(db); }
MemberActivity.java:-
package com.my.members;
import android.app.Activity;
import android.content.Context;
import android.database.SQLException;
import android.os.Bundle;
import android.view.View;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.Button; import android.widget.EditText;
EditText txtUserName;
public class MembersActivity extends Activity { DBAdapter dbAdapter; EditText txtPassword; Button btnLogin;
super.onCreate(savedInstanceState);
Button btnRegister; @Override public void onCreate(Bundle savedInstanceState) { setContentView(R.layout.main);
btnLogin = (Button) findViewById(R.id.btn_login);
txtUserName = (EditText) findViewById(R.id.et_user); txtPassword = (EditText) findViewById(R.id.et_pw); btnRegister = (Button) findViewById(R.id.btn_reg);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
dbAdapter = new DBAdapter(this); dbAdapter.open(); btnLogin.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) {
String password = txtPassword.getText().toString();
imm.hideSoftInputFromWindow(txtUserName.getWindowToken(), 0); imm.hideSoftInputFromWindow(txtPassword.getWindowToken(), 0); String username = txtUserName.getText().toString();
.show();
if (username.length() > 0 && password.length() > 0) { try { if (dbAdapter.Login(username, password)) { Toast.makeText(MembersActivity.this, "Successfully Logged In", Toast.LENGTH_LONG) } else {
Toast.LENGTH_LONG).show();
Toast.makeText(MembersActivity.this, "Invalid username or password", Toast.LENGTH_LONG).show(); } } catch (Exception e) { Toast.makeText(MembersActivity.this, "Some problem occurred", } } else {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
Toast.makeText(MembersActivity.this, "Username or Password is empty", Toast.LENGTH_LONG).show(); } } }); btnRegister.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) {
long i = dbAdapter.register(username, password);
imm.hideSoftInputFromWindow(txtUserName.getWindowToken(), 0); imm.hideSoftInputFromWindow(txtPassword.getWindowToken(), 0); try { String username = txtUserName.getText().toString(); String password = txtPassword.getText().toString(); if(i != -1)
}
Toast.makeText(MembersActivity.this, "You have successfully registered",Toast.LENGTH_LONG).show(); } catch (SQLException e) { Toast.makeText(MembersActivity.this, "Some problem occurred", Toast.LENGTH_LONG).show(); } } });
}
main.xml:-
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textSize="40dp" android:layout_gravity="right" android:layout_marginBottom="10dp" android:layout_marginLeft="30dp"/> <TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <EditText android:id="@+id/et_user" android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="250dp" android:layout_marginLeft="30dp" android:layout_marginBottom="10dp" android:hint="@string/u"> <requestFocus /> </EditText> </TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content" > <EditText android:id="@+id/et_pw" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" android:layout_marginLeft="30dp" android:layout_marginBottom="10dp" android:hint="@string/p"/> </TableRow> <TableRow android:id="@+id/tableRow4" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/l" android:layout_marginLeft="30dp" android:layout_marginBottom="10dp" android:textSize="20dp"/> </TableRow> <TableRow android:id="@+id/tableRow5" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/btn_reg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/r" android:layout_marginLeft="30dp" android:layout_marginBottom="10dp" android:textSize="20dp"/> </TableRow> </TableLayout> </LinearLayout>

Sunday, 8 March 2015

What is Android ???


Android is a mobile operating system (OS) based on the Linux kernel and currently developed by Google. 

Platform usage in Android


Charts in this section provide breakdowns of Android versions, based on the devices accessing the Play Store in a seven-day period ending on March 2, 2015. Therefore, these statistics exclude Android forks that do not access Google Play, such as Amazon's Fire tablets.

VersionCode nameRelease dateAPI levelDistribution
2.2FroyoMay 20, 201080.4%
2.3.3–2.3.7GingerbreadFebruary 9, 2011106.9%
4.0.3–4.0.4Ice Cream SandwichDecember 16, 2011155.9%
4.1.xJelly BeanJuly 9, 20121617.3%
4.2.xJelly BeanNovember 13, 20121719.4%
4.3Jelly BeanJuly 24, 2013185.9%
4.4KitKatOctober 31, 20131940.9%
5.0LollipopNovember 3, 2014213.3%
VersionCode nameRelease dateAPI levelDistribution
5.0LollipopNovember 3, 2014213.3%
4.4KitKatOctober 31, 20131940.9%
4.3Jelly BeanJuly 24, 2013185.9%
4.2.xNovember 13, 20121719.4%
4.1.xJuly 9, 20121617.3%
4.0.3–4.0.4Ice Cream SandwichDecember 16, 2011155.9%
2.3.3–2.3.7GingerbreadFebruary 9, 2011106.9%
2.2FroyoMay 20, 201080.4%