1).This is the main class.
LoginApp.java
package com.ex;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class LoginApp extends Activity implements OnClickListener
{
private SharedPreferences sp;
Intent i;
Button b1,b2;
EditText ed1,ed2,ed3,ed4;
String user,pass,cpass,eid,chk;
CheckBox ck;
String stat="a";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1=(EditText)findViewById(R.id.edit1);
ed2=(EditText)findViewById(R.id.edit2);
ed3=(EditText)findViewById(R.id.edit3);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
ck=(CheckBox)findViewById(R.id.check1);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
sp=this.getSharedPreferences("Register", MODE_WORLD_READABLE);
chk=sp.getString("USERNAME", "");
if(chk.length()!=0){
sp=getSharedPreferences("Register",MODE_WORLD_WRITEABLE);
Editor myEditor=sp.edit();
myEditor.putString("Status", stat);
myEditor.commit();
i=new Intent(this,Register.class);
startActivity(i);
}
}
public void onClick(View arg0) {
user=ed1.getText().toString();
pass=ed2.getText().toString();
cpass=ed3.getText().toString();
if(arg0==b1){
if(ck.isChecked()){
if((user.length()!=0))
{
if((pass.length()!=0))
{
if(pass.compareTo(cpass)==0)
{
sp=getSharedPreferences("Register",MODE_WORLD_WRITEABLE);
Editor myEditor=sp.edit();
myEditor.putString("USERNAME", user);
myEditor.putString("PASSWORD", pass);
myEditor.commit();
Toast.makeText(this, "Registration is successfull",10000).show();
i=new Intent(this,Register.class);
startActivity(i);
}
else
{
Toast.makeText(this, "Password Mismatch", 10000).show();
}
}
else
{
Toast.makeText(this, "Please Enter password", 10000).show();
}
}
else{
Toast.makeText(this,"Please Enter Username",10000).show();
}
}
else{
Toast.makeText(this, "Please Accept all our terms and conditions", 10000).show();
}
}
else if(arg0==b2){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Exit");
builder.setMessage("Do you want to exit");
builder.setCancelable(false);
builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
arg0.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
}
}
2).The next Page is for Registration page
Register.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.ex;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView.BufferType;
import android.widget.Toast;
/**
*
* @author sonu
*/
public class Register extends Activity implements OnClickListener {
private SharedPreferences sp;
String user,pass;
Button b1,b2,b3,b4;
EditText ed1,ed2;
Intent i;
int flag=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.reg);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b4=(Button)findViewById(R.id.button4);
ed1=(EditText)findViewById(R.id.edit1);
ed2=(EditText)findViewById(R.id.edit2);
chk=(CheckBox)findViewById(R.id.check1);
b1.setOnClickListener(this);
b4.setOnClickListener(this);
b3.setOnClickListener(this);
b2.setOnClickListener(this);
}
public void onClick(View arg0) {
sp=this.getSharedPreferences("Register", MODE_WORLD_READABLE);
user=sp.getString("USERNAME", "");
pass=sp.getString("PASSWORD","");
if(b1==arg0){
if((ed1.getText().toString().compareTo(user)==0)&& (ed2.getText().toString().compareTo(pass)==0))
{
Toast.makeText(this, "You are Logged In", 20000).show();
flag=1;
}
else
{
Toast.makeText(this, "Wrong Username or Password",20000).show();
flag=0;
}
}
else if ((b2==arg0 )&&(flag==1)){
Intent intent;
intent=new Intent(this,details.class);
startActivity(intent);
}
else if((b2==arg0)&&(flag==0)){
Toast.makeText(this, "You are not logged in", 10000).show();
}
else if(b3==arg0){
Intent in;
in=new Intent(this,Change.class);
startActivity(in);
}
else if(b4==arg0){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Exit");
builder.setMessage("Do you want to exit");
builder.setCancelable(false);
builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
arg0.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
}
}
3).The next code is for the my profile activity where you can see your profile
details.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.ex;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView.BufferType;
/**
*
* @author sonu
*/
public class details extends Activity implements OnClickListener {
private SharedPreferences sp;
String us,ps;
EditText ed1,ed2;
Button b;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
sp=this.getSharedPreferences("Register", MODE_WORLD_READABLE);
us=sp.getString("USERNAME", "");
ps=sp.getString("PASSWORD", "");
ed1=(EditText)findViewById(R.id.edit1);
ed2=(EditText)findViewById(R.id.edit2);
ed1.setText(us);
ed2.setText(ps);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(this);
}
public void onClick(View arg0) {
Intent i;
i=new Intent(this,Register.class);
startActivity(i);
}
}
4).The next activity is for changing the password. Here i have enable the view of the old password. You can change it .
Change.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.ex;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
/**
*
* @author sonu
*/
public class Change extends Activity implements OnClickListener {
EditText ed1,ed2,ed3;
Button b1,b2;
private SharedPreferences sp;
String pass;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.change);
ed1=(EditText)findViewById(R.id.edit1);
ed2=(EditText)findViewById(R.id.edit2);
ed3=(EditText)findViewById(R.id.edit3);
sp=this.getSharedPreferences("Register", MODE_WORLD_READABLE);
pass=sp.getString("PASSWORD", "");
ed1.setText(pass);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
public void onClick(View arg0) {
String p=ed2.getText().toString();
String c=ed3.getText().toString();
sp=getSharedPreferences("Register", MODE_WORLD_WRITEABLE);
Editor myeditor=sp.edit();
if(arg0==b1){
if(p.compareTo(c)==0){
//myeditor.remove("PASSWORD");
myeditor.putString("PASSWORD", p);
myeditor.commit();
Toast.makeText(this, "Password Changed", 20000).show();
Intent i=new Intent(this,Register.class);
startActivity(i);
}
else
{
Toast.makeText(this, "Password Mismatch", 20000).show();
}
}
else{
Intent i=new Intent(this,Register.class);
startActivity(i);
}
}
}
The layout for first activity "LoginApp.java"
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget31"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/widget44"
android:layout_width="240px"
android:layout_height="23px"
android:text="Welcome To Registration Page"
>
</TextView>
<LinearLayout
android:id="@+id/widget45"
android:layout_width="238px"
android:layout_height="42px"
>
<TextView
android:id="@+id/widget56"
android:layout_width="80px"
android:layout_height="wrap_content"
android:text="Username"
>
</TextView>
<EditText
android:id="@+id/edit1"
android:layout_width="154px"
android:layout_height="wrap_content"
android:textSize="18sp"
>
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/widget47"
android:layout_width="238px"
android:layout_height="44px"
>
<TextView
android:id="@+id/widget60"
android:layout_width="80px"
android:layout_height="wrap_content"
android:text="Password"
>
</TextView>
<EditText
android:id="@+id/edit2"
android:layout_width="155px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:password="true"
>
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/widget62"
android:layout_width="237px"
android:layout_height="47px"
>
<TextView
android:id="@+id/widget63"
android:layout_height="wrap_content"
android:text="C.Password" android:layout_width="80px">
</TextView>
<EditText
android:id="@+id/edit3"
android:layout_width="152px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:password="true"
>
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/widget65"
android:layout_width="237px"
android:layout_height="49px"
android:orientation="vertical"
>
<CheckBox
android:id="@+id/check1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I Accept all the terms and Conditions"
>
</CheckBox>
</LinearLayout>
<LinearLayout
android:id="@+id/widget67"
android:layout_width="235px"
android:layout_height="47px"
>
<Button
android:id="@+id/button1"
android:layout_width="114px"
android:layout_height="wrap_content"
android:text="Sign Up"
>
</Button>
<Button
android:id="@+id/button2"
android:layout_width="120px"
android:layout_height="wrap_content"
android:text="Exit"
>
</Button>
</LinearLayout>
</LinearLayout>
The layout for second activity Register.java
reg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget31"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:id="@+id/widget70"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/widget71"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Login Page"
>
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/widget72"
android:layout_width="240px"
android:layout_height="46px"
>
<TextView
android:id="@+id/widget73"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
>
</TextView>
<EditText
android:id="@+id/edit1"
android:layout_width="170px"
android:layout_height="wrap_content"
android:textSize="18sp"
>
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/widget75"
android:layout_width="239px"
android:layout_height="54px"
>
<TextView
android:id="@+id/widget76"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
>
</TextView>
<EditText
android:id="@+id/edit2"
android:layout_width="170px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:password="true"
>
</EditText>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="113px"
android:layout_height="wrap_content"
android:text="Login"
>
</Button>
<Button
android:id="@+id/button2"
android:layout_width="119px"
android:layout_height="wrap_content"
android:text="My Profile"
>
</Button>
</LinearLayout>
<LinearLayout
android:id="@+id/widget82"
android:layout_width="238px"
android:layout_height="47px"
>
<Button
android:id="@+id/button3"
android:layout_width="115px"
android:layout_height="wrap_content"
android:text="Forgot Password"
>
</Button>
<Button
android:id="@+id/button4"
android:layout_width="115px"
android:layout_height="wrap_content"
android:text="Exit"
>
</Button>
</LinearLayout>
</LinearLayout>
For Third activity details.java
details.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget31"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:id="@+id/widget32"
android:layout_width="320px"
android:layout_height="45px"
>
<TextView
android:id="@+id/widget33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
>
</TextView>
<EditText
android:id="@+id/edit1"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_width="190px">
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/widget35"
android:layout_width="320px"
android:layout_height="52px"
>
<TextView
android:id="@+id/widget36"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
>
</TextView>
<EditText
android:id="@+id/edit2"
android:layout_height="wrap_content"
android:textSize="18sp" android:layout_width="190px">
</EditText>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_height="wrap_content"
android:text="Back"
android:layout_width="240px">
</Button>
</LinearLayout>
For fourth activity "Change.java"
change.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget31"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:id="@+id/widget32"
android:layout_width="320px"
android:layout_height="50px"
>
<TextView
android:id="@+id/widget33"
android:layout_width="117px"
android:layout_height="wrap_content"
android:text="Old Password"
>
</TextView>
<EditText
android:id="@+id/edit1"
android:layout_height="wrap_content"
android:textSize="18sp" android:layout_width="120px">
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/widget35"
android:layout_width="320px"
android:layout_height="55px"
>
<TextView
android:id="@+id/widget37"
android:layout_width="119px"
android:layout_height="wrap_content"
android:text="New Password"
>
</TextView>
<EditText
android:id="@+id/edit2"
android:layout_height="wrap_content"
android:textSize="18sp"
android:password="true"
android:layout_width="120px">
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/widget36"
android:layout_width="313px"
android:layout_height="57px"
>
<TextView
android:id="@+id/widget39"
android:layout_height="wrap_content"
android:text="Confirm Password" android:layout_width="120px">
</TextView>
<EditText
android:id="@+id/edit3"
android:layout_height="wrap_content"
android:textSize="18sp"
android:password="true"
android:layout_width="120px">
</EditText>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_height="wrap_content"
android:text="Change Password" android:layout_width="240px">
</Button>
<Button
android:id="@+id/button2"
android:layout_height="wrap_content"
android:text="Back"
android:layout_width="240px">
</Button>
</LinearLayout>
For more queries email me at sonulohani@yahoo.com.
Thankyou. Please comment for improvement.