Friday, 9 February 2018

Login Example with basic validation

Screenshots:






string.xml:

<resources>
    <string name="app_name">Login</string>

</resources>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:background="@color/colorPrimary"
    tools:context="com.krishanandroid.login.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email Id"
        android:inputType="textEmailAddress"
        android:layout_marginTop="112px"
        android:ems="10"
        android:id="@+id/editEmailText"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/editPasswordText"
        />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/buttonLogin"
        android:onClick="Login"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Status: "
        android:textColor="#ffffff"
        android:id="@+id/tvStatusText"/>
</LinearLayout>

activity_welcome_screen.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_welcome_screen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.krishanandroid.login.WelcomeScreen">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Krishan"
        android:textColor="@color/colorPrimary"
        android:id="@+id/tvUserText"/>

</LinearLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.krishanandroid.login">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".WelcomeScreen"></activity>
    </application>


</manifest>

WelcomeScreen.java:

package com.krishanandroid.login;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class WelcomeScreen extends AppCompatActivity {
    TextView        vUserName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome_screen);

        vUserName = (TextView) findViewById(R.id.tvUserText);

        Intent intent = getIntent();

        Bundle bundle = intent.getExtras();

        String vName = bundle.getString("vName");

        vUserName.setText("Welcome: "+vName);
    }
}

MainActivity.java:

package com.krishanandroid.login;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText vUserEmail;
    EditText vUserPwd;
    TextView vStatus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vUserEmail = (EditText) findViewById(R.id.editEmailText);
        vUserPwd = (EditText) findViewById(R.id.editPasswordText);

        vStatus = (TextView) findViewById(R.id.tvStatusText);

    }

    public void Login(View view) {

        // Step 4: Logic

        String vStr = "info@catokg.com";
        String vPwd = "techkrishan";

        int counter = 1;

        String vGetEmailId, vGetPassword;

        vGetEmailId = vUserEmail.getText().toString();  // Read email entered by user
        vGetPassword = vUserPwd.getText().toString(); // Read password entered by user

        if ((vGetEmailId.length() == 0) || (vGetEmailId.length() == 0)) {
            Toast.makeText(this, "Please email or pwd", Toast.LENGTH_SHORT).show();
        }

        if ((vGetEmailId.equals(vStr)) && (vGetPassword.equals(vPwd)))
        {
            Intent intent = new Intent(MainActivity.this, WelcomeScreen.class);
            intent.putExtra("vName", vGetEmailId);
            startActivity(intent);
            counter = 1;
            //Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
        }else
        {
            if (counter == 4)
            {
                Toast.makeText(this, "Max Exceed", Toast.LENGTH_SHORT).show();
                finish(); // Closing current screen
                return;
            }

            counter++;

            if ((vGetEmailId.equals(vStr) == false) && (vGetPassword.equals(vPwd) == false))
            {
                vStatus.setText("Both are wrong: " +counter);
                vUserEmail.setText(" "); // clear edit email text
                vUserPwd.setText(" "); // clear edit password text
                vUserEmail.requestFocus(); // Place cursor on email text box

            }else if (vGetEmailId.equals(vStr) == false)
            {
                vStatus.setText("Email is wrong: " +counter);
                vUserEmail.setText(" "); // clear edit email text
                vUserEmail.requestFocus(); // Place cursor on email text box

            }else if ((vGetEmailId.equals(vPwd) == false))
            {
                vStatus.setText("Password is wrong: " +counter);
                vUserPwd.setText(" "); // clear edit password text
                vUserPwd.requestFocus(); // Place cursor on password text box
            }
            // Toast.makeText(this, "Failure", Toast.LENGTH_SHORT).show();
        }
    }
}

No comments:

Post a Comment