Monday, 11 February 2013

Log-in while Username and Passwords are saved in String array ?



1)xml file:
 
<?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:background="#ffffff"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Enter UserName"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

    <EditText
        android:id="@+id/edtUserName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Enter Password"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

    <EditText
        android:id="@+id/edtPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btnClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:text="Click Me" />

</LinearLayout>

2)Activity:



package venky.loginthroughstringarray;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.venkool.gvrproject.R;

public class LoginthroughStringArray extends Activity {
      /** Called when the activity is first created. */

      EditText edtUserName, edtPassword;
      Button btnClick;

      boolean status = false;

String[] MyUserName = { "android", "android1", "android2", "android3",
                  "android4", "android5", "Android" };

String[] MyPassword = { "password", "password1", "password2", "password3",
                  "password4", "password5", "Password" };

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login_test);

            edtUserName = (EditText) findViewById(R.id.edtUserName);
            edtPassword = (EditText) findViewById(R.id.edtPassword);
            btnClick = (Button) findViewById(R.id.btnClick);

            btnClick.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

         String UserName = edtUserName.getText().toString();
         String Password = edtPassword.getText().toString();

                        for (int i = 0; i < MyUserName.length; i++) {

                              if (MyUserName[i].equals(UserName)
&& MyPassword[i].equals(Password)) {

                                    status = true;
                                    Log.e("status", status + "");

                              }

                        }

                        if (status) {
                              Log.e("status", status + "");
                              Toast.makeText(
                                        LoginthroughStringArray.this,
"Successfully Login", Toast.LENGTH_LONG).show();

                        } else {
                              Log.e("status", status + "");
                              Toast.makeText(getApplicationContext(), "Login failed",
                                          Toast.LENGTH_LONG).show();
                        }
                        status = false;
                  }

            });
      }
}

No comments:

Post a Comment