Question -2
a. Create sample application with login module.(Check username and password)
b. On successful login, go to next screen. And on failing login, alert user using Toast.
c. Also pass username to next screen
Step 1: Create New Project in eclipse
Step 2: Edit main activity layout xml file.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<TableRow
android:id="@+id/tableRow0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="Login Form" android:textSize="30dp"/>
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="UserName : " android:textSize="20dp"/>
<EditText android:id="@+id/etUname"/>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="PassWord : " android:textSize="20dp"/>
<EditText android:id="@+id/etPwd"/>
</TableRow>
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
</TableLayout>
Step 3: Add new layout xml file sucess.xml in res/layout folder.
Step 2: Edit main activity layout xml file.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<TableRow
android:id="@+id/tableRow0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="Login Form" android:textSize="30dp"/>
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="UserName : " android:textSize="20dp"/>
<EditText android:id="@+id/etUname"/>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="PassWord : " android:textSize="20dp"/>
<EditText android:id="@+id/etPwd"/>
</TableRow>
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
</TableLayout>
Step 3: Add new layout xml file sucess.xml in res/layout folder.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="Login Sucessfull" android:textSize="30dp"/>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="WellCome :" android:textSize="25dp"/>
<TextView android:text="" android:id="@+id/tvUser" android:textSize="25dp"/>
</TableRow>
</TableLayout>
Step 4: Edit Main Activity Java File from src/package/MainActivity.java
package myLab.Pack;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
EditText uname;
EditText pwd;
Button login;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
uname = (EditText)findViewById(R.id.etUname);
pwd = (EditText)findViewById(R.id.etPwd);
login = (Button)findViewById(R.id.btnLogin);
login.setOnClickListener(this);
}
public void onClick(View v)
{
String un = uname.getText().toString();
String pd = pwd.getText().toString();
try
{
if(un.equalsIgnoreCase("sahin") && pd.equalsIgnoreCase("raj"))
{
Intent i = new Intent(this,Sucess.class);
i.putExtra("user", un);
startActivity(i);
}
else
{
Toast.makeText(this,"Usernae & Passward Mismatch",Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Step 5: Add new Java file in src/package/sucess.java
package myLab.Pack;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Sucess extends Activity
{
TextView tv;
@Override
protected void onCreate(Bundle s)
{
// TODO Auto-generated method stub
super.onCreate(s);
setContentView(R.layout.sucess);
Intent i = getIntent();
String uname = i.getStringExtra("user");
tv = (TextView)findViewById(R.id.tvUser);
tv.setText(""+uname);
}
}
No comments:
Post a Comment