This code snippet creates a login screen with 2 labels (Username, Password), corresponding 2 text boxes and 2 buttons (Login, Reset) using Android. All the above resources are defined in login.xml file. The events for Login and Reset button are handled and further database authentication can be enhanced from there.
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/widget0" android:layout_height="270px" xmlns:android="http://schemas.android.com/apk/res/android" android:fitsSystemWindows="true" android:paddingBottom="0px" android:paddingLeft="0px" android:paddingRight="0px" android:paddingTop="0px" android:persistentDrawingCache="scrolling" android:layout_width="480px"> <TextView android:id="@+id/widget1" android:layout_width="wrap_content" android:layout_height="20px" android:text="User Name" android:textStyle="bold" android:layout_y="43px" android:layout_x="110px" android:textColor="#FFFFFF"> </TextView> <TextView android:id="@+id/widget2" android:layout_width="wrap_content" android:layout_height="20px" android:text="Password" android:textStyle="bold" android:layout_x="110px" android:textColor="#FFFFFF" android:layout_y="87px"> </TextView> <EditText android:id="@+id/userName" android:layout_width="162px" android:layout_x="191px" android:textSize="12px" android:layout_y="33px" android:textStyle="bold" android:nextFocusDown="@+id/password" android:numeric="integer" android:layout_height="38px"> </EditText> <EditText android:id="@+id/password" android:layout_width="162px" android:password="true" android:layout_x="190px" android:textSize="12px" android:layout_height="38px" android:layout_y="77px"> </EditText> <Button android:id="@+id/submit" android:text="Login" android:textStyle="bold" android:textSize="11sp" android:layout_width="71px" android:layout_y="126px" android:layout_x="197px" android:layout_height="32px" android:ellipsize="start"> </Button> <Button android:id="@+id/cancel" android:layout_width="71px" android:text="Reset" android:textSize="12sp" android:textStyle="bold" android:layout_x="277px" android:layout_y="126px" android:layout_height="32px"> </Button> </AbsoluteLayout>
import android.app.Activity; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class Login extends Activity { //Global declaration Button submit; Button reset; EditText username; EditText password; String uname=null; String pwd=null; String pwd=null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle b) { super.onCreate(b); setContentView(R.layout.login); username = (EditText) this.findViewById(R.id.userName); password = (EditText) this.findViewById(R.id.password); submit = (Button) this.findViewById(R.id.submit); submit.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { authenticate(); } }); reset = (Button) this.findViewById(R.id.cancel); reset.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { reset(); } }); } // Function to perform authentication using the entered values private void authenticate() { uname = username.getText().toString(); pwd = password.getText().toString(); // Can perform checks with the input given from screen using uname and pwd } // Function to clear the username and password fields private void reset() { username.setText(""); password.setText(""); } }