Translate To Your Own Langauge

Login with Validation (Android Example)




Now here we move to the next step which is very important when we deal with the Login page in android app Development. Many of the time, we just forget to apply the validations on the EditText provided for username and password. This is the problem with fresher.  In this tutorial, we will learn that how to apply validation to the username and password field to make a Login page with validation so that user must have to fill all the required fields. Just follow these steps:

  1. Make two layout files inside res/layouts folder. One for Login UI and other for Redirecting user after entering the username, password and pressing login button. Use the following code in Activity_main.xml file which is your login UI.
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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/edtuser"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:hint="Enter username"
        android:inputType="text" />

    <EditText
        android:id="@+id/edtpass"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edtuser"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="44dp"
        android:ems="10"
        android:hint="Enter password"
        android:inputType="textPassword" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edtpass"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="43dp"
        android:text="Login" />

</RelativeLayout>


2.       Now your Login UI is ready. Now you have to make another file for redirecting user after login. I named it after_login.xml. You can use any other name which you feel suitable for you. Copy the following code into your after_login.xml file.
after_login.xml
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to the after login page."
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

3.       Your UI for both the files is ready. Now you have to work on the coding part which is always in java when we deals with android development. Follow these steps for mainActivity.java file.

·         Add the following code after “extends activity {”
EditText edtuser,edtpass;
Button btnLogin;

·         Add the following code to initialize the controls inside onCreate() method.

edtuser=(EditText)findViewById(R.id.edtuser);
edtpass=(EditText)findViewById(R.id.edtpass);
btnLogin=(Button)findViewById(R.id.btnLogin);

·         Now apply onclick listener to the button by copying the following code.

btnLogin.setOnClickListener(new View.OnClickListener() {
                    
                     @Override
                     public void onClick(View arg0) {
                           // TODO Auto-generated method stub
                     if(edtuser.getText().toString().trim().length()==0){
                           edtuser.setError("Username is not entered");
                           edtuser.requestFocus();
                     }
                     if(edtpass.getText().toString().trim().length()==0){
                           edtpass.setError("Password is not entered");
                           edtpass.requestFocus();
                     }
                      else{
                           Intent it=new Intent(getApplicationContext(), afterLogin.class);
                           startActivity(it);  
                     }
                     }
              });

Your final mainActivity.class file will contain the following code:
package com.at4u.loginwithvalidation;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
       EditText edtuser,edtpass;
       Button btnLogin;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              edtuser=(EditText)findViewById(R.id.edtuser);
              edtpass=(EditText)findViewById(R.id.edtpass);
              btnLogin=(Button)findViewById(R.id.btnLogin);
              btnLogin.setOnClickListener(new View.OnClickListener() {
                    
                     @Override
                     public void onClick(View arg0) {
                           // TODO Auto-generated method stub
                     if(edtuser.getText().toString().trim().length()==0){
                           edtuser.setError("Username is not entered");
                           edtuser.requestFocus();
                     }
                     if(edtpass.getText().toString().trim().length()==0){
                           edtpass.setError("Password is not entered");
                           edtpass.requestFocus();
                     }
                     else{
                           Intent it=new Intent(getApplicationContext(), afterLogin.class);
                           startActivity(it);  
                     }
                     }
              });
       }

       @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;
       }

}


4.       Now add the activity in the manifest.xml file by copying the following code:

<activity android:name="com.at4u.loginwithvalidation.afterLogin"></activity>


Download the full code.

29 Responses to “Login with Validation (Android Example)”

lokesh said...
5 April 2017 at 01:16

thanks dude.... i thought it was very difficult to write validation code u made it simple probably all using it...


Unknown said...
26 April 2017 at 03:53

This is step by step . Very helpful it's like weight to beginner , you fill all space bro


Unknown said...
16 February 2018 at 23:10

means that if we put any input , it will redirect to another page , am I right ?


AngelVila said...
6 June 2019 at 11:26

Thanks a lot.
How can I validate having the información in a PC DataBase?


Unknown said...
20 June 2019 at 04:51

how we create a Valid user name and psword using intent button


qb enterprise support number said...
26 January 2020 at 22:36

The experts at QuickBooks Technical Support Phone Number +1 (877) 282-6222 are available 24x7 hours and are always ready to help you with the best solution, every time. The experts at QuickBooks Technical Support Phone Number +1 (877) 282-6222 are available 24x7 hours and are always ready to help you with the best solution, every time.
QuickBooks Technical Support Phone Number
QuickBooks Technical Support Phone Number
QuickBooks Technical Support Phone Number


Quickbooks Phone Number said...
10 June 2020 at 22:43

Very well explained blog click here for QuickBooks Proadvisor Support Phone Number for more details dial on our 24*7 QuickBooks Enterprise support phone number 1-855-615-2111


The Qb Payroll said...
3 July 2020 at 22:46

QuickBooks POS Support Phone Number
QuickBooks Desktop Support Phone Number
QuickBooks Desktop Support Phone Number
QuickBooks For Mac Support Phone Number
QuickBooks Pro Support Phone Number
QuickBooks Enterprise Support Phone Number
QuickBooks Tech Support phone Number
QuickBooks Payroll Support Phone Number
QuickBooks For Mac Support Phone Number
QuickBooks Toll Free Phone Number


Quickbooks Phone Number said...
6 July 2020 at 22:22

Very helpful and i would like to thanks!Do you want QuickBooks Payroll Support Number Get live support 24*7 from QuickBooks expert on tool-free Number.Click here to know how to Grab QuickBooks trial for desktop Dial for any tech support:1-855-511-6911


Quickbooks Phone Number said...
6 July 2020 at 22:51

Very well explained blog click here for QuickBooks Proadvisor Support Phone Number for more details dial on our 24*7 and Dial 1-855-511-6911 QuickBooks Enterprise support Number


Anonymous said...
11 August 2020 at 18:53 This comment has been removed by the author.

Anonymous said...
11 August 2020 at 18:57

Thanx a lot


yamini said...
24 January 2022 at 03:53 This comment has been removed by the author.

yamini said...
24 January 2022 at 04:00

This is really helpful and informative.
Nice, Best data analytics institute in Lucknow


Erkam said...
28 September 2023 at 03:30

https://bayanlarsitesi.com/
Altınşehir
Karaköy
Alemdağ
Gürpınar
KWOAWC


Ergün74 said...
3 October 2023 at 18:03

elazığ
bilecik
kilis
sakarya
yozgat
FUCY


StardustGoddessXYOLG1 said...
21 October 2023 at 11:41

Mersin Lojistik
Amasya Lojistik
Kayseri Lojistik
Kırklareli Lojistik
Erzurum Lojistik
HLU


87A19MichaelA9904 said...
11 November 2023 at 07:58

9B158
Satoshi Coin Hangi Borsada
Hatay Şehirler Arası Nakliyat
Eskişehir Lojistik
Nevşehir Parça Eşya Taşıma
Malatya Parça Eşya Taşıma
Tekirdağ Cam Balkon
Silivri Çatı Ustası
Niğde Şehirler Arası Nakliyat
Bolu Şehir İçi Nakliyat


64149DonaldBF32E said...
14 November 2023 at 19:27

CF81F
Binance Referans Kodu
Çanakkale Evden Eve Nakliyat
Denizli Evden Eve Nakliyat
Samsun Evden Eve Nakliyat
Balıkesir Evden Eve Nakliyat
buy masteron
order masteron
Manisa Evden Eve Nakliyat
Mardin Evden Eve Nakliyat


E56FBEmersonE8EFB said...
18 November 2023 at 13:06

7AF94
Gümüşhane Lojistik
Kütahya Parça Eşya Taşıma
Tunceli Şehirler Arası Nakliyat
Milyon Coin Hangi Borsada
Hatay Şehir İçi Nakliyat
Siirt Evden Eve Nakliyat
Ünye Kurtarıcı
İstanbul Evden Eve Nakliyat
Kilis Şehirler Arası Nakliyat


8393AMerrick6B3C9 said...
20 December 2023 at 06:02

CE8A3
canli sohbet chat
parasız görüntülü sohbet uygulamaları
bilecik parasız sohbet siteleri
kayseri ucretsiz sohbet
van ücretsiz sohbet
kadınlarla sohbet et
bingöl sohbet muhabbet
sivas kadınlarla sohbet et
muğla görüntülü sohbet ücretsiz


F0C41ChelseaB0350 said...
24 December 2023 at 03:33

46057
sesli sohbet odası
Tokat Rastgele Canlı Sohbet
kastamonu parasız görüntülü sohbet
görüntülü sohbet ücretsiz
kadınlarla rastgele sohbet
adıyaman canli sohbet bedava
çankırı telefonda kadınlarla sohbet
agri sesli sohbet odası
sesli sohbet sitesi


44784Ronald53D05 said...
25 December 2023 at 19:03

427ED
bedava sohbet
amasya canlı sohbet
adıyaman ücretsiz sohbet uygulaması
uşak rastgele sohbet
adıyaman görüntülü sohbet uygulama
Bayburt Canlı Sohbet Siteleri Ücretsiz
Bitlis Rastgele Sohbet Uygulaması
adana yabancı canlı sohbet
kocaeli en iyi rastgele görüntülü sohbet


CED46Jimmie7DE24 said...
2 January 2024 at 02:45

2643C
bedava sohbet chat odaları
yozgat ücretsiz görüntülü sohbet
tokat kızlarla rastgele sohbet
rastgele sohbet
canli sohbet chat
Antep Ücretsiz Sohbet Odaları
kütahya sesli sohbet siteleri
karabük canlı sohbet et
Adıyaman Ücretsiz Sohbet Sitesi


98BCDGlenn51D56 said...
5 January 2024 at 02:04

B3B95
rize ücretsiz sohbet sitesi
niğde ücretsiz görüntülü sohbet
Düzce Bedava Görüntülü Sohbet
konya kadınlarla rastgele sohbet
kars ucretsiz sohbet
kırklareli sohbet odaları
şırnak mobil sohbet chat
maraş canlı sohbet odası
düzce görüntülü sohbet uygulama


A564CShay02309 said...
6 January 2024 at 07:30

1452F
Coin Nedir
Görüntülü Sohbet Parasız
Görüntülü Sohbet
Yeni Çıkacak Coin Nasıl Alınır
Tumblr Takipçi Hilesi
Vector Coin Hangi Borsada
Clubhouse Takipçi Hilesi
Meta Coin Hangi Borsada
Bitcoin Nasıl Üretilir


BE8DDTraci8E41E said...
9 January 2024 at 22:19

66E47
Bitcoin Nasıl Çıkarılır
Binance Hangi Ülkenin
Bitcoin Nasıl Para Kazanılır
Binance Borsası Güvenilir mi
Kripto Para Nasıl Çıkarılır
Likee App Takipçi Satın Al
Kripto Para Madenciliği Nasıl Yapılır
Binance Referans Kodu
Binance Referans Kodu


6C647Valerie6948F said...
18 January 2024 at 11:51

B94BF
Coin Çıkarma Siteleri
Kaspa Coin Hangi Borsada
Caw Coin Hangi Borsada
Binance Para Kazanma
Referans Kimliği Nedir
Vector Coin Hangi Borsada
Arbitrum Coin Hangi Borsada
Pinterest Takipçi Hilesi
Binance Referans Kodu


9FFE7William2CD8C said...
20 January 2024 at 02:38

03734
Binance Referans Kodu
Twitter Trend Topic Hilesi
Binance Nasıl Oynanır
Soundcloud Takipçi Satın Al
Gate io Borsası Güvenilir mi
Bitcoin Para Kazanma
Sohbet
Osmo Coin Hangi Borsada
Binance Referans Kodu


Post a Comment

Comment your feedback for the post. You can also ask your queries regarding android.

Blogger Widgets
All Rights Reserved Android Tutorials | Designed by: Deepak Anand