Login screen in android

Android — Login Screen

A login application is the screen asking your credentials to login to some particular application. You 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 false attempts are made.

First you have to define two TextView asking username and password of the user. The password TextView must have inputType set to password. Its syntax is given below −

Define a button with login text and set its onClick Property. After that define the function mentioned in the onClick property in the java file.

In the java file, inside the method of onClick get the username and passwords text using getText() and toString() method and match it with the text using equals() function.

The last thing you need to do is to provide a security mechanism, so that unwanted attempts should be avoided. For this initialize a variable and on each false attempt, decrement it. And when it reaches to 0, disable the login button.

Example

Here is an example demonstrating a login application. It creates a basic application that gives you only 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 Android studio to create an Android application under a package com.example.sairamkrishna.myapplication.
3 Modify src/MainActivity.java file to add necessary code.
4 Modify the res/layout/activity_main to add respective XML components
5 Run the application and choose a running android device and install the application on it and verify the results

Following is the content of the modified main activity file src/MainActivity.java.

Following is the modified content of the xml res/layout/activity_main.xml.

In the following code abc indicates about logo of tutorialspoint.com

Following is the content of the res/values/string.xml.

Following is the content of AndroidManifest.xml file.

Let’s try to run our application we just modified. I assume you had created your AVD while doing environment setup. To run the app from Android studio, open one of your project’s activity files and click Run icon from the toolbar. Android studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window −

Читайте также:  Увеличение памяти андроид за счет карты памяти

Type anything in the username and password field, and then press the login button. I put abc in the username field and abc in the password field. I got failed attempt. This is shown below −

Do this two more time, and you will see that you have 0 login attempts left and your login button is disabled.

Now open the application again, and this time enter correct username as admin and password as admin and click on login. You will be successfully login.

If user press on cancel button, it will close an application of login screen.

Источник

Android Studio – Android Login Screen Example

In this tutorial we will learn How to create a simple Login Screen project Using Android Studio.

So lets create a project.

Step 1 – Create new Android project.

Provide Activity name as Login as shown below

Android Login Screen Example Login Activity

Step 2 – Add components in the main activity as shown in the picture below.

Android Login Screen Example Design

SimpleLoginApp\app\src\main\res\layout\activity_login.xml

Now Chenge the AndroidManifest.xml file as shown in the code below:

\SimpleLoginApp\app\src\main\AndroidManifest.xml

Now go to values\strings.xml and change it as shown n the code

\SimpleLoginApp\app\src\main\res\values\strings.xml

Step 3 Add the new activity . Right Click Project -> New -> Activity -> Blank Activity

Give the name to your activity User

create blank activity

write the following code

Now Run your app

Android Login Screen Example Output wrong user and password

Android Login Screen Example Output correct user and password

Android Login Screen Example Output correct user and password Next activity

Foer more Details watch the videos


Источник

Android Login and Registration Screen Design

To allow only registered users to our android application, we need to implement login and registration screens to ask the users to register first, then login to the application to get an access for the content in an application.

Now we will see how to implement login and registration screens using material design in android application like as shown following.

Android Login and Registration Screens Example

Following is the example of creating the login and registration screens in android application to allow only valid users based on our requirements.

Create a new android application using android studio and give names as LoginExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.

Once we create an application, open activity_main.xml file from \res\layout folder path and write the code like as shown below.

activity_main.xml

xml version= «1.0» encoding= «utf-8» ?>
LinearLayout xmlns: android = «http://schemas.android.com/apk/res/android»
android :orientation= «vertical» android :layout_width= «match_parent»
android :layout_height= «match_parent» >
TextView
android :id= «@+id/loginscrn»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginTop= «80dp»
android :text= «Login»
android :textSize= «25dp»
android :textStyle= «bold»
android :layout_gravity= «center»/>
TextView
android :id= «@+id/fstTxt»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :layout_marginTop= «20dp»
android :text= «Email»/>
EditText
android :id= «@+id/txtEmail»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :ems= «10»/>
TextView
android :id= «@+id/secTxt»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :text= «Password»
android :layout_marginLeft= «100dp»/>
EditText
android :id= «@+id/txtPwd»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :inputType= «textPassword»
android :ems= «10»/>
Button
android :id= «@+id/btnLogin»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :text= «Login»/>
TextView android :id= «@+id/lnkRegister»
android :layout_width= «match_parent»
android :layout_height= «wrap_content»
android :layout_marginTop= «40dp»
android :text= «New to Tutlane? Register here»
android :gravity= «center»
android :textSize= «20dp»
android :textColor= «#3F51B5»/>
LinearLayout >

Читайте также:  Экран всегда включен андроид

Now we will create another layout resource file registration.xml in \res\layout path to allow new users to register in our application for that right click on your layout folder à Go to New à select Layout Resource File and give name as registration.xml.

Once we create a new layout resource file registration.xml, open it and write the code like as shown below

registration.xml

xml version= «1.0» encoding= «utf-8» ?>
LinearLayout xmlns: android = «http://schemas.android.com/apk/res/android»
android :orientation= «vertical» android :layout_width= «match_parent»
android :layout_height= «match_parent» >
TextView
android :id= «@+id/loginscrn»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginTop= «80dp»
android :text= «Registration»
android :textSize= «25dp»
android :textStyle= «bold»
android :layout_gravity= «center»/>
TextView
android :id= «@+id/fstTxt»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :layout_marginTop= «20dp»
android :text= «Full Name»/>
EditText
android :id= «@+id/txtName»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :ems= «10»/>
TextView
android :id= «@+id/secTxt»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :text= «Email»
android :layout_marginLeft= «100dp»/>
EditText
android :id= «@+id/txtEmail»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :ems= «10»/>
TextView
android :id= «@+id/thirdTxt»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :text= «Password»
android :layout_marginLeft= «100dp»/>
EditText
android :id= «@+id/txtPwd»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :inputType= «textPassword»
android :ems= «10»/>
Button
android :id= «@+id/btnLogin»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :text= «Login»/>
TextView android :id= «@+id/lnkLogin»
android :layout_width= «match_parent»
android :layout_height= «wrap_content»
android :layout_marginTop= «40dp»
android :text= «Already Registered? Login here»
android :gravity= «center»
android :textSize= «20dp»
android :textColor= «#3F51B5»
android :onClick= «test»/>
LinearLayout >

Читайте также:  Прошивка андроида samsung duos

Now open your main activity file MainActivity.java from \java\com.tutlane.loginexample path and write the code like as shown below

MainActivity.java

package com.tutlane.loginexample;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity <

@Override
protected void onCreate(Bundle savedInstanceState) <
super .onCreate(savedInstanceState);
setContentView(R.layout. activity_main );
TextView register = (TextView)findViewById(R.id. lnkRegister );
register.setMovementMethod(LinkMovementMethod.getInstance());
register.setOnClickListener( new View.OnClickListener() <
@Override
public void onClick(View v) <
Intent intent = new Intent(MainActivity. this , RegistrationActivity. class );
startActivity(intent);
>
>);
>
>

If you observe the above code, whenever the user click on register link, we are redirecting the user from login screen to registration screen using “RegistrationActivity” for that create another activity file RegistrationActivity.java in \java\com.tutlane.loginexample path.

Now right click on your application folder à Go to New à select Java Class and give name as RegistrationActivity.java.

Once we create a new activity file RegistrationActivity.java, open it and write the code like as shown below

RegistrationActivity.java

package com.tutlane.loginexample;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.TextView;

/**
* Created by tutlane on 08-01-2018.
*/

public class RegistrationActivity extends AppCompatActivity <
@Override
protected void onCreate(Bundle savedInstanceState) <
super .onCreate(savedInstanceState);
setContentView(R.layout. registration );
TextView login = (TextView)findViewById(R.id. lnkLogin );
login.setMovementMethod(LinkMovementMethod.getInstance());
login.setOnClickListener( new View.OnClickListener() <
@Override
public void onClick(View v) <
Intent intent = new Intent(RegistrationActivity. this , MainActivity. class );
startActivity(intent);
>
>);
>
>

Now we need to add this newly created activity in AndroidManifest.xml file in like as shown below.

AndroidManifest.xml

xml version= «1.0» encoding= «utf-8» ?>
manifest xmlns: android = «http://schemas.android.com/apk/res/android»
package= «com.tutlane.loginexample» >
application
android :allowBackup= «true»
android :icon= «@mipmap/ic_launcher»
android :label= «@string/app_name»
android :roundIcon= «@mipmap/ic_launcher_round»
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= «.RegistrationActivity» android :label= «Registration» > activity >
application >
manifest >

If you observe above example, we created login and registration screens and added all the activities in AndroidManifest.xml file.

Output of Android Login & Registration Screens Example

When we run the above example in the android emulator we will get a result like as shown below.

If you observe above result, initially we are getting a login screen and redirecting the user to the registration screen whenever the user clicks Register Here link.

This is how we can create login and registration screens using material design in android applications to allow valid users based on our requirements.

Источник

Оцените статью