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
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.
Источник
How to Create User Interface Login & Register with Android Studio
ok, this is my first article in Medium. In this section, I want to share with you about the User Interface on Android and we will create a Login page and a Register page. Some components that I will use:
1. Viewpager
2. Fragment
3. Edittext
4. Button
5. Textview
6. Imageview
What about the results? let’s coding (follow step by step)
- Of course we must already have an Android Studio. if not, you can download it first on the official Android Studio website. If you already have one, please open your Android studio.
2. We create a new project by clicking “Start a new Android Studio project”. Fill in the application name column with “LoginApp”, then click next.
3. Select the minimum SDK you need or want, then click next.
4. Select “Empty Activity” and click next. After that, the “Activity Name” and “Layout Name” columns will appear, in this section just leave it like that, then click finish.
After you click finish, Android Studio will make you an Application with the name “LoginApp”.
After the application is successfully built, you can simply make adjustments to the following sections:
Open the colors.xml file in app/res/values/colors.xml, and change it like this:
Create a folder with the name “font” in the res folder, by right clicking on the res directory, select new / directory and name it “font”. After that, copy this font into font directory. (download the font 1 and font 2).
Create some Drawable Resource File in the drawable directory, by right-clicking on the drawable directory, select new / Drawable Resource File.
After that open file styles.xml and change like this :
Add the theme property in the Main Activity in the manifest, in the app / manifests / AndroidManifest.xml folder
So the AndroidManifest.xml file will be as follows:
After all the steps above are done, then make 2 fragments with the name fragment_login and fragment_register in the layout directory, by right-clicking on the layout directory, New/Fragment/Fragment (Blank)
Uncheck:
include fragment factory methods? and include interface methods?
Adjustment of several layouts :
change the activity_main.xml layout, fragment_login.xml and fragment_register.xml so that it will be as below
Open the strings.xml file in the res / values / strings.xml directory and change it to something like the following :
So that the overall structure of the project will be as follows :
Connect fragments with viewpager
Create an inner class in the MainActivity.java class with the name AuthenticationPapterAdapter. This class is a derivative of the FragmentPagerAdapter class and serves to connect Fragments with ViewPager,
Then call the class in the onCreate() method in MainActivity.java and connect with viewpager
So the MainActivity.java class will be like this
The display of the final application will be like this
Источник
Login Screen Application Example in Android
A Login screen is the screen where the user is asked to fill his credential like email, name etc to login into any particular application.
How to Create a Simple Login Application
This tutorial explains how to create a simple login application using the Android Studio. In the first part we will learn how to design our login application and set up all the resources and in the second part, we will write the backend code for this application.
Designing Login Application
Please follow the steps below for designing a Login Application:
Step 1) First create a new android studio project and give a name to your new application. For example, simple login app.
Step 2) Now click next and we are going to make this project for phone and tablets and therefore make sure that you choose the version with minimum SDK which covers hundred percent of devices.
Step 3) Now choose the blank activity here, click next. And give the activity name for example ‘login’.
Step 4) So now our project is set up and now this is the first activity we can see here which is ‘login activity’.
Step 5) Now create one more activity so that we can redirect the user if he provides valid login and password so that it can be a users activity so that user can access some screen or some activity after login. So in your app, just go to this ‘resources’ folder or res folder and inside this, you will be able to see this folder called ‘layout’ and create a new blank activity and name the activity for example as «user» and click finish
Step 6) So now we have two activities: — one is ‘login’ activity (activity_login.xml) which will be used to create our login page and the other is ‘user’activity (activity_login.xml) which will open after authentication of the user.
Step 7) Now in the app folder there will be a folder called ‘manifests’ go to your manifest folder. Inside this folder, there will be an xml file called ‘android manifest’ file (androidmanifest.xml file) click this and you will see both activity, one is our main activity or our launcher activity and the other is our user activity.
Step 8) Now we have to set up the intent filter for the second activity (main activity). In our first activity, which is our login activity, this intent filter is already there, But in the user activity there is only name and label no intent filters available. So we are just going to copy this intent filter tag from our login activity and paste it in the user activity.
Step 9) Now there can only be one launcher activity for your app, therefore, the second activity will be a default activity. Launcher activity means that the first activity which will appear is launcher activity which is our login activity. Now the user activity is our default activity which will open whenever we are going to authenticate the user.
Step 10) The name of the default activity will be the content name of this second activity. So go to your activity_user.xml file and copy the path and paste androidmanifest.xml in the
Now as we have successfully set up our second activity, therefore we are now going to design our first login activity here:
Step 1) So first drag and drop some text views and added views so that, you know, the user can enter his login credentials.
Step 2) After completing the design part now transfer all our string constant to the string.xml file. So in your app folder, there will be a folder called ‘values’ here in the resources folder so go to resources or res folder and there will be a folder called ‘values’ and inside this values, there will be a folder called strings.xml file and in here you can define all your string constant.
Step 3) So what we are going to do is once you are done designing your app, you can just go to this content tree and in here on every, you know, text view and other button you can see a small little warning yellow light and when you click this, you will be able to see a small little button or bulb here and just click this bulb and this bulb says «hardcoded string login page should use the string resource». So android studio also gives us the option to, define all the string constant in our string xml.
Step 4) So just click this message here and then it will open your extra resources file and file name here is string.xml and in here you can name your resource. So this text page is our text, right? So you can just name it as ‘login_text’ so it is just ID for your string OK and once you are done with that, you can just click OK.
Step 5) Do the same for every text and added text and button here so that all these resources are transferred to my string.xml file. You can see we have created login_text and the text on this login_text is login page. In the same way, we have defined one more tag for a username so I will do the same for other text boxes or text views and buttons.
Step 6) So now I have successfully defined all my strings to the string in the string.xml file. So now I have defined all my string constants, all my strings as constant and I have transferred all my strings as constant in my strings.xml file. You can see all my strings like username, password, attempt, login have some you know, tag here or constant name here so that I can reuse them in some different place using the tags or tag Ids right? Now our designing is done. You can even go to the second activity which is activity_user.xml file and go to the design and in here I can just drag and drop large text and I can change the text.
So now our design part is almost complete or complete.
Code for Login application.
Step 1) Now once we have completed our design, now we will go to the java folder here and in here, we will go to the login.java folder and there we will declare some variables inside our login class and then we will add a button action perform a method to our OnCreate method.
Step 2) So now first declare the variables. We will declare two added text boxes for username and password, one text view for the attempt and one for the button. We will declare one extra variable here which is called int attempt counter. So we want to provide minimum attempt or maximum attempt to the user so that, just, for example, we want to provide five attempts to the user to try login and password and if these attempts are gone then we want to do some other option.
Step 3) Now we are ready to create our button click event. So just create a method and inside the method and cast all the variables which we have declared. So we will cast username, password and the attempt and the button variables.
Step 4) Now after casting all the four variables which we declared now we will add a button click event to our login button and on the basis of this button click event, we are going to assign them some action. So let’s take a login_btn variable and create a button click event count, create a new OnClickListener and inside this OnClick method, we are going to write our code.
Step 5) The above condition applies when the username and password is correct. If the username and password is wrong, then define the else condition for this. So else, what we are going to do here is we are going to reduce the number of count right because we have defined the maximum number of count is equal to five. So we can, you know, reduce the number counts if the login is failed. So what we are going to do is, first of all, we will show some message to the user such as «username and password is not correct» and then reduce the number of attempts, so we can just reduce this counter by one and if this counter is equal to 0 then we are going to disable this login button so that user cannot login anymore.
Step 6) Now we are going to call this method in our OnCreate method. Therefore, just copy the LoginButton () method and paste it in OnCreate method.
Step 7) Now we will add the code to open the new activity when username and password is correct. So if this username and password is correct below our message that username and password is correct, we are going to create an instance of intent, ‘i’ or whatever name you want to give is equal to new intent and as an argument, we will pass the path to our second activity for the user.
Step 8) Now we have to set the count to attempt text box take the attempts from inside the LoginButton() method and just below the casting of these variable, we will setText is equal to whatever is the counter or current counter and convert this counter to text.
Step 9) So now once we have printed the number of attempts, just copy setText code as we also want to reduce the number of the count and set this text to the attempt text box in the else condition. So once if the username and password are correct or not correct, we will reduce the counter and set the text to our attempt text box here So just paste below else condition.
So this attempt or set the text is pasted two times:– once at the starting after the casting of all the variables here and second after reduction of this counter by 1 in the else condition.
Step 10 )Now we are going to run our program. So now our app is running and now we are going to enter username and password. Remember your username should be ‘user’ and password should be ‘pass’ in order to, you know, a successful login, right? So we will enter first of all successful username and password or correct username and password and if the username and password is correctly entered, when we press this login button, you see it shows this message – username and password is correct and we are redirected to the next page.
Step 11) Now let’s try to enter some random username and password and let’s see the response OK so once we enter the, you know, random username and password, and we click this login button, it reduces the attempts and it shows you the name and password is not correct. And last time when we attempt login, once the attempts become 0, then this login button is no more active OK so it’s disabled.
Conclusion
So following the above steps a user can create a new simple login application design in Android,
Источник