Create simple login form with validation android.





create simple android login form with validation


In this tutorial we are going to create android login window, first create activity_main.xml
1. Create MainActivity.java file

package com.example.loginform;

import android.support.v7.app.ActionBarActivity;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 ActionBarActivity {
 
 String strpassword,strusername;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
 final EditText userName=(EditText) findViewById(R.id.editText_UserName);
 
 final EditText password=(EditText) findViewById(R.id.editText_Password);
 
 Button loginButton=(Button) findViewById(R.id.button_login);
 
 loginButton.setOnClickListener(new OnClickListener() {
  
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   strusername=userName.getText().toString();
   strpassword=password.getText().toString();
   
   
   
   if(strusername.isEmpty())
   {
    Toast.makeText(getApplicationContext(), "Please enter user name", Toast.LENGTH_LONG).show();
   }
   else if(strpassword.isEmpty())
   {
    Toast.makeText(getApplicationContext(), "Please enter password", Toast.LENGTH_LONG).show();
   }
   else
   {
    
    if(strusername.equals("admin") &&strpassword.equals("admin"))
    {
     Toast.makeText(getApplicationContext(), "Login Sucessfully", Toast.LENGTH_LONG).show();
    }
    else
    {
     Toast.makeText(getApplicationContext(), "Invalid user id or password", Toast.LENGTH_LONG).show(); 
    }
    
   }
   
   
   
  }
 });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }
}

2. create activity_main.xml file




<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"
    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:background="@color/Beige"
    tools:context="com.example.loginform.MainActivity" >

    <EditText
        android:id="@+id/editText_UserName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="87dp"
        android:ems="10"
        android:hint="User Name"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText_Password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/editText_UserName"
        android:layout_below="@+id/editText_UserName"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/button_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/editText_Password"
        android:layout_below="@+id/editText_Password"
        android:layout_marginTop="46dp"
        android:text="Login" />

</RelativeLayout>


Leave a Reply

Your email address will not be published. Required fields are marked *