This tutorial is moderated from http://www.edumobile.org/android/android-development/login-request-example-in-android/ .
1) Run Eclipse.
2) Create New Android Application. Type name "LoginRequestTest".
3) Accept Default Project Configuration. Click Next.
4) Accept Default Launcher Configuration. Click Next.
5) Select New Blank Activity. Click Next.
6) Accept Default Activity and Layout name. Click Finish.
7) Edit the codes.
7.1) MainActivity.Java
ppackage com.example.loginrequesttest; import android.app.Activity; 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 { EditText txtUserName; EditText txtPassword; Button btnLogin; Button btnCancel; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtUserName=(EditText)this.findViewById(R.id.txtUname); txtPassword=(EditText)this.findViewById(R.id.txtPwd); btnLogin=(Button)this.findViewById(R.id.btnLogin); btnLogin=(Button)this.findViewById(R.id.btnLogin); btnLogin.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if((txtUserName.getText().toString()).equals(txtPassword.getText().toString())){ Toast.makeText(MainActivity.this, "Login Successful",Toast.LENGTH_LONG).show(); } else{ Toast.makeText(MainActivity.this, "Invalid Login",Toast.LENGTH_LONG).show(); } } }); } }
7.2) activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1"> <TableRow> <TextView android:text="User Name: " android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </TextView> <EditText android:text="" android:id="@+id/txtUname" android:layout_width="fill_parent" android:layout_height="wrap_content"> </EditText> </TableRow> <TableRow> <TextView android:text="Password: " android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content"> </TextView> <EditText android:text="" android:id="@+id/txtPwd" android:layout_width="fill_parent" android:layout_height="wrap_content" android:password="true"> </EditText> </TableRow> <TableRow> <Button android:text="Cancel" android:id="@+id/btnCancel" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> <Button android:text="Login" android:id="@+id/btnLogin" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> </TableRow> </TableLayout> </RelativeLayout>
8) Run
8.1) Enter Username/password as "user/user"
No comments:
Post a Comment