Android Login Screen Tutorial

Published on January 2017 | Categories: Documents | Downloads: 15 | Comments: 0 | Views: 190
of 8
Download PDF   Embed   Report

Comments

Content


Advertisements
A login application is the screen asking your credentials to login to some particular a
might have seen it when logging into facebook,twitter e.t.c
This chapter explains, how to create a login screen and how to manage security when
are made.
First you have to define two TextView asking username and password of the user.
TextView must have inputType set to password. Its syntax is given below:
<EditText
andr oi d: i d="@+i d/ edi t Text 2"
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: i nput Type="t ext Passwor d" />
<EditText
andr oi d: i d="@+i d/ edi t Text 1"
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
/>
Define a button with login text and set its onClick Property. After that define the function
the onClick property in the java file.
<Button
andr oi d: i d="@+i d/ but t on1"
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: onCl i ck="l ogi n"
andr oi d: t ext ="@st r i ng/ Logi n"
/>
In the java file, inside the method of onClick get the username and passwords text using
toString() method and match it with the text using equals() function.
EditText user name = ( EditText) f i ndVi ewByI d( R. i d. edi t Text 1) ;
EditText passwor d = ( EditText) f i ndVi ewByI d( R. i d. edi t Text 2) ;
public void l ogi n( View vi ew) {
if( user name. get Text ( ) . t oSt r i ng( ) . equal s( "admi n") &&
passwor d. get Text ( ) . t oSt r i ng( ) . equal s( "admi n") ) {
//correcct password
}else{
//wrong password
Home Programmi ng Java Web Databases Academi c Management Qual i ty Tel ecom More
Previous Page Nex Androi d Tutori al
Sel ected Readi ng
Developer's Best Practices
Effective Resume Writing
Computer Glossary
Who is Who
Android Home
Android Overview
Android Environment Setup
Android Architecture
Application Components
Hello World Example
Android Resources
Android Activities
Android Services
Android Broadcast Receivers
Android Content Providers
Android Fragments
Android Intents/Filters
Androi d User Interface
Android UI Layouts
Android UI Controls
Android Event Handling
Android Styles and Themes
Android Custom Components
Androi d Advanced Concepts
Android Drag and Drop
Android Notifications
Location Based Services
Android Sending Email
g p
}
The last thing you need to do is to provide a security mechanism, so that unwanted attem
avoided. For this intializa a variable and on each false attempt, decrement it. And when
disable the login button.
int count er = 3;
count er - - ;
if( count er ==0) {
//disble the button, close the application e.t.c
}
Here is an example demonstrating a login application. It creates a basic application tha
three attempts to login to an application.
To experiment with this example , you can run this on an actual device or in an emulator.
Steps Description
1
You will use Eclipse IDE to create an Android application and name it as Login
a package com.example.loginscreen. While creating this project, make sure yo
and Compile With at the latest version of Android SDK to use higher levels of AP
3 Modify src/MainActivity.java file to add necessary code.
4 Modify the res/layout/activity_main to add respective XML components
5 Modify the res/values/string.xml to add necessary string components
6
Run the application and choose a running android device and install the applic
verify the results
Following is the content of the modifed main
src/com.example.loginscreen/MainActivity.java.
package com. exampl e. l ogi nscr een;
import andr oi d. app. Activity;
import andr oi d. gr aphi cs. Color;
import andr oi d. os. Bundle;
import andr oi d. vi ew. Menu;
import andr oi d. vi ew. View;
import andr oi d. wi dget . Button;
import andr oi d. wi dget . EditText;
import andr oi d. wi dget . TextView;
import andr oi d. wi dget . Toast;
public class MainActivity extends Activity {
private EditText user name=null;
private EditText passwor d=null;
private TextView at t empt s;
private Button l ogi n;
int count er = 3;
@Over r i de
protected void onCr eat e( Bundle savedI nst anceSt at e) {
super. onCr eat e( savedI nst anceSt at e) ;
set Cont ent Vi ew( R. l ayout . act i vi t y mai n) ;
Android Sending SMS
Android Phone Calls
Publishing Android Application
Androi d Useful Exampl es
Android Alert Dialoges
Android Animations
Android Audio Capture
Android AudioManager
Android Auto Complete
Android Best Practices
Android Bluetooth
Android Camera
Android Clipboard
Android Custom Fonts
Android Data Backup
Android Developer Tools
Android Emulator
Android Facebook Integration
Android Gestures
Android Google Maps
Android Image Effects
Android ImageSwitcher
Android Internal Storage
Android J etPlayer
Android J SON Parser
Android Linkedin Integration
Android Loading Spinner
Android Localization
Android MediaPlayer
Android Multitouch
Android Navigation
Android Network Connection
Android NFC Guide
Android PHP/MySQL
Androi d Logi n Screen
( y y_ )
user name = ( EditText) f i ndVi ewByI d( R. i d. edi t Text 1) ;
passwor d = ( EditText) f i ndVi ewByI d( R. i d. edi t Text 2) ;
at t empt s = ( TextView) f i ndVi ewByI d( R. i d. t ext Vi ew5) ;
at t empt s. set Text ( Integer. t oSt r i ng( count er ) ) ;
l ogi n = ( Button) f i ndVi ewByI d( R. i d. but t on1) ;
}
public void l ogi n( View vi ew) {
if( user name. get Text ( ) . t oSt r i ng( ) . equal s( "admi n") &&
passwor d. get Text ( ) . t oSt r i ng( ) . equal s( "admi n") ) {
Toast. makeText ( get Appl i cat i onCont ext ( ) , "Redi r ect i ng. . . ",
Toast. LENGTH_SHORT) . show( ) ;
}
else{
Toast. makeText ( get Appl i cat i onCont ext ( ) , "Wr ong Cr edent i al s",
Toast. LENGTH_SHORT) . show( ) ;
at t empt s. set Backgr oundCol or ( Color. RED) ;
count er - - ;
at t empt s. set Text ( Integer. t oSt r i ng( count er ) ) ;
if( count er ==0) {
l ogi n. set Enabl ed( false) ;
}
}
}
@Over r i de
public boolean onCr eat eOpt i onsMenu( Menu menu) {
// Inflate the menu; this adds items to the action bar if it i
get MenuI nf l at er ( ) . i nf l at e( R. menu. mai n, menu) ;
return true;
}
}
Following is the modified content of the xml res/layout/activity_main.xml.
<RelativeLayout xml ns: andr oi d="ht t p: / / schemas. andr oi d. com/ apk/ r es/ an
xml ns: t ool s="ht t p: / / schemas. andr oi d. com/ t ool s"
andr oi d: l ayout _wi dt h="mat ch_par ent "
andr oi d: l ayout _hei ght ="mat ch_par ent "
andr oi d: paddi ngBot t om="@di men/ act i vi t y_ver t i cal _mar gi n"
andr oi d: paddi ngLef t ="@di men/ act i vi t y_hor i zont al _mar gi n"
andr oi d: paddi ngRi ght ="@di men/ act i vi t y_hor i zont al _mar gi n"
andr oi d: paddi ngTop="@di men/ act i vi t y_ver t i cal _mar gi n"
t ool s: cont ext =". Mai nAct i vi t y" >
<TextView
andr oi d: i d="@+i d/ t ext Vi ew1"
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _al i gnPar ent Top="t r ue"
andr oi d: l ayout _cent er Hor i zont al ="t r ue"
andr oi d: l ayout _mar gi nTop="18dp"
andr oi d: t ext ="@st r i ng/ hel l o_wor l d"
andr oi d: t ext Appear ance="?andr oi d: at t r / t ext Appear anceLar ge" />
<TextView
andr oi d: i d="@+i d/ t ext Vi ew2"
Android PHP/MySQL
Android Progress Circle
Android ProgressBar
Android Push Notification
Android RenderScript
Android RSS Reader
Android Screen Cast
Android SDK Manager
Android Sensors
Android Session Management
Android Shared Preferences
Android SIP Protocol
Android Spelling Checker
Android SQLite Database
Android Support Library
Android Testing
Android Text to Speech
Android TextureView
Android Twitter Integration
Android UI Deign
Android UI Patterns
Android UI Testing
Android WebView Layout
Android Wi-Fi
Android Widgets
Android XML Parsers
Androi d Useful Resources
Android Quick Guide
Android Useful Resources
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _al i gnPar ent Lef t ="t r ue"
andr oi d: l ayout _bel ow="@+i d/ t ext Vi ew1"
andr oi d: l ayout _mar gi nTop="50dp"
andr oi d: t ext ="@st r i ng/ user name"
andr oi d: t ext Appear ance="?andr oi d: at t r / t ext Appear anceMedi um" />
<EditText
andr oi d: i d="@+i d/ edi t Text 1"
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _al i gnBot t om="@+i d/ t ext Vi ew2"
andr oi d: l ayout _mar gi nLef t ="32dp"
andr oi d: l ayout _t oRi ght Of ="@+i d/ t ext Vi ew2"
andr oi d: ems="10" >
<requestFocus />
</EditText>
<TextView
andr oi d: i d="@+i d/ t ext Vi ew3"
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _al i gnLef t ="@+i d/ t ext Vi ew2"
andr oi d: l ayout _bel ow="@+i d/ t ext Vi ew2"
andr oi d: l ayout _mar gi nTop="38dp"
andr oi d: t ext ="@st r i ng/ passwor d"
andr oi d: t ext Appear ance="?andr oi d: at t r / t ext Appear anceMedi um" />
<EditText
andr oi d: i d="@+i d/ edi t Text 2"
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _al i gnBot t om="@+i d/ t ext Vi ew3"
andr oi d: l ayout _al i gnLef t ="@+i d/ edi t Text 1"
andr oi d: ems="10"
andr oi d: i nput Type="t ext Passwor d" />
<Button
andr oi d: i d="@+i d/ but t on1"
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _bel ow="@+i d/ edi t Text 2"
andr oi d: l ayout _cent er Hor i zont al ="t r ue"
andr oi d: l ayout _mar gi nTop="94dp"
andr oi d: onCl i ck="l ogi n"
andr oi d: t ext ="@st r i ng/ Logi n" />
<TextView
andr oi d: i d="@+i d/ t ext Vi ew4"
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _al i gnLef t ="@+i d/ t ext Vi ew3"
andr oi d: l ayout _bel ow="@+i d/ t ext Vi ew3"
andr oi d: l ayout _mar gi nLef t ="30dp"
andr oi d: l ayout _mar gi nTop="48dp"
andr oi d: t ext ="@st r i ng/ at t empt s"
andr oi d: t ext Appear ance="?andr oi d: at t r / t ext Appear anceMedi um" />
<T tVi
<TextView
andr oi d: i d="@+i d/ t ext Vi ew5"
andr oi d: l ayout _wi dt h="wr ap_cont ent "
andr oi d: l ayout _hei ght ="wr ap_cont ent "
andr oi d: l ayout _al i gnRi ght ="@+i d/ t ext Vi ew1"
andr oi d: l ayout _al i gnTop="@+i d/ t ext Vi ew4"
andr oi d: t ext ="Text Vi ew" />
</RelativeLayout>
Following is the content of the res/values/string.xml.
<?xml ver si on="1. 0" encodi ng="ut f - 8"?>
<resources>
<string name="app_name">Logi nScr een</string>
<string name="act i on_set t i ngs">Set t i ngs</string>
<string name="hel l o_wor l d">Logi n Scr een</string>
<string name="user name">User name: </string>
<string name="passwor d">Passwor d: </string>
<string name="Logi n">Logi n: </string>
<string name="at t empt s">At t empt s Lef t : </string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml ver si on="1. 0" encodi ng="ut f - 8"?>
<manifest xml ns: andr oi d="ht t p: / / schemas. andr oi d. com/ apk/ r es/ andr oi d"
package="com. exampl e. l ogi nscr een"
andr oi d: ver si onCode="1"
andr oi d: ver si onName="1. 0" >
<uses-sdk
andr oi d: mi nSdkVer si on="8"
andr oi d: t ar get SdkVer si on="17" />
<application
andr oi d: al l owBackup="t r ue"
andr oi d: i con="@dr awabl e/ i c_l auncher "
andr oi d: l abel ="@st r i ng/ app_name"
andr oi d: t heme="@st yl e/ AppTheme" >
<activity
andr oi d: name="com. exampl e. l ogi nscr een. Mai nAct i vi t y"
andr oi d: l abel ="@st r i ng/ app_name" >
<intent-filter>
<action andr oi d: name="andr oi d. i nt ent . act i on. MAI N" />
<category andr oi d: name="andr oi d. i nt ent . cat egor y. LAUNCHER
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run our Login application we just modified. I assume you had created your A
environment setup. To run the app from Eclipse, open one of your project's activity files
icon from the toolbar. Eclipse installs the app on your AVD and starts it and if everyth
your setup and application, it will display following Emulator window:
Type anything in the username and password field except admin, and then press the lo
admin in the username field and nimda in the password field. I got failed attempt. This is
Do this two more time, and you will see that you have 0 login attempts left and your
disabled.
Now open the application again, and this time enter correct username as admin and
admin and click on login. You will be successfully login.
Advertisements
Copyright ©2014 by tutorialspoint. All Rights Reserved.
Previous Page Print Version Nex
ASP.NET | jQuer y | AJAX | ANT | JSP | Ser vlets | log4j | iBATIS | Hiber nat e | JDBC | St r uts | HTM

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close