Description: The article is about android studio login. This passage is a tutorial. The author gives a demonstration to us. We can learn how to create a wonder-looking login application in which you can easily type in your user name and password.
In this tutorial I am going to show you how you can create a wonderful-looking login application which takes in your password and your name. If it matches it lets you go to the next activity,if you type in the incorrect attempts,it also disables the button so that you can’t log it.
First you need to open it and we will name this application as login demo and we can click on next. We can go with the defaults and click on next and we can choose empty activity. We will keep the name of the main activity as main activity and then proceed,we need to wait for Android studio to build.
Once your under studio is finished building,you can go ahead to the activity and we are going to click on the design term. Under the design section,we need to edit text and add a button,we can drag the plain text to the XML layout. It has a main layout as well as the blueprint which is C. Before we add the next edit text, we need to constraint this to the layout. We can constrain it horizontally to the right and left and then also to the top and the bottom.
Now I have constrained this,we can move it slightly higher because you don’t want it at the center. You have it perfectly aligned. You don’t want this text to be named so you can change this text. You need to keep it as a blank and instead of text let’s name the hint. You have that name which will be highlighted unless the user click it. Once the user clicks on this,it disappears. That’s a better way of defining a widget.
Before we add that,we also need to set the id of textview,if you scroll to the top,we can see that we have the ID of this edit text. Let us call this as et name and click on enter. Next we need to add edit text for the password. You can drag it below name,you need to repeat the process again.
Instead of constraining this to the top,we will constrain this below. We can constrain this to that and to the bottom as well and to the left and the right. Then you can move it around. You need to make sure it’s in the center and place it below the name.We also set the hint and the ID for this edit text so you can change the hint to password and change the ID to ET password.
ET here basically means edit text for short,this is how I code it. After this is done,we can add the button quickly. We can add the button,we can drag it below the password,we need to make sure it’s in the center,we can constraint it to the password. Once you constrained it properly,you can move it around. We can quickly rename this button to login. We can name the ID for BTN which is short for button and login.
We have name the ID for the button as well. Now we also need to add a small text view. Under the button add the text view,this is the display the of incorrect items.We need to change the two numbers. We need to type in incorrect attempts of: and click enter. You can see that it has moved,you need to make sure it’s in the center. We have done this,we can also assign the ID for this,let’s call this TV info. This is the ID for this. We are not constrain this or move to the blueprint so we can constrain it to the topmost node and top of widget.
You can move on so there you will have it. This is our simple login application which I have created in the latest under studio by using the constraint layout. This is very simple. Create the layout can be difficult at first but become very simple when you know how to it. This is how you create the layout.
After we created our layout we can go to admin activity.Java. We need to create four variables for all the widgets that we created. First let’s create the variable for edit text,I will call this as name. I’ll call the other edit text as password. We have a textview so I am going to call this as info,next we have a button so let us call this as login.
Now we have added all our variables,we need to assign these variables to the ID defined in XML layout. For doing that we need to do a find view by ID,we need to take the variables,we need to do a quick typecast to the widget,we need to do a find view by ID. It assigns this variable to the respective ID in XML layout. Next we need to assign the password. For doing that we need to do a quick edit text type cast and find view by ID.
Once this is done,we can quickly do it for the other term and skip forward. Now we have assigned our variables with the respective XML layout ID,we need to write a function for validating if our password is right. Let’s write a function called as private void validate.
It will have two parameters,one is the user entered name and the user entered password. This is how you can write a simple function.Void is because it’s not returning anything. It has to take the string and password and then compare it with string. If we have read a simple if condition and our username is equal to 0,you can write your name or whatever you want the username and password to be. You can write admin. You need to make sure that the user password is also equal to something.
User password is equal to one two three four. You need to make sure you put all of these in separate curly bracket. What inside this happens is that if the username is equal to admin and if the user password is equal to one two three four then you have to go to the next activity. It has to let the user enter into our application.
Once the string matches,it has to let the user into the next activity. For doing that we need the intent. It is equal to new intent. We need to go from main activity. This is another activity,let’s call this second activity.class. Now this is showing an error because you have not created a second activity,let’s create a second activity. You can click at the top left corner,you have it under Java,you can see your package name,you can click this. You can create your new activity,you can go to activity and create empty activity.
We want to call this for second activity. For the sake of simplicity you can click on finish. That will create your second activity. Once the second activity has loaded you can see that it has a Java file as well as the layout,but if you don’t find this,you can always go to the package and find activity. If you go to the sources layout,you can find the layout here,you can get all this.
We’ll add a simple text. This is the second activity. You can double click on this and in the text tool this is second activity. Once you have done that,you can click on enter and make sure it’s in the center. You can drag it. It’s in the center. You can also change the text size. You can type in 32 independent pixels which is exiting SP.
You can see that it’s quite large now and more visible. This is our second activity. Let’s close this,we don’t need this,we are not going to do anything much in the second activity. Our error has gone now. An intent is used to move from one activity to another,it uses these two parameters the first activity and the activity which you want to go to,it sounds more like a source and the destination. From main activity,you need to add this.
To start the next activity,we need to do start activity function. Start activity and the intent are very fast. That’s how simple it gets. This is how you can move to the next activity in this two lines. This happens only when the username and the user password entered is equal to admin and one two three four.
Now if it is not,we need to do something when the password is incorrect. We need a counter which decreases each time. You need to create a counter called int counter. Every time the wrong password is entered,the counter will decrease.
We are going to initialize this counter to five. After finding the correct items,the button should be disabled,so the counter decreases. If the counter is equal to zero then you need to disable the bottom. Our button is the login. This will disable the button. When should this function work? This function should work only when the button login is click.
We need to click we need to set the onclick listener for this. We can type in login.setonclicklistener. You need to make sure that you are typing in what I’m typing because I am not typing everything. ‘m typing a part of it. I’m typing for on click and you can see that under pseudo is completing it for me. Once you click enter,it shows another page. If you type new view,you’re not going to get this.
It should validate,you are going to see a dysfunction and we also need to pass two parameters which are username and user password. What is the username? Username is got from the edit text. We need to type in name.gettext. Our name is the edit text.
This is about how we log in. What it does? It firstly validate that our username and password is equal to admin and one two three four. If it is correct then it moves to the next activity. If it’s not then it decreases our counter. After that it keeps decreasing,if you keep plugging in incorrect passwords and when the counter becomes zero,the login will appear false.
You have completed this. We have another variable thing called info. It is the text view which we need to use to display the number of incorrect attempt. To display the incorrect attempts what we need to do is that we need to make sure you type it correctly. It displays the number of attempts remaining and the counter value which keeps decreasing each time we show them. Since this is an INT,we need to do a quick login.
What this function does? It converts into a string. This is how you can set the info of number of items. You can recollect that. We have MR as number of incorrect items. I change it to number of items. You need to make sure that you do this change. If you run this application,you can see that it shows number of items.
Whenever the button is clicked,the validate function is called and the number of items. This is how you can create the login application. It’s simple. You can quickly go ahead and run this application onto your phone. Initially we have a number of attempts which is equal to five,now the next thing we need to do is that we need to enter the user name and password. I am going to do the correct version first.
If you type in admin and then type one two three four and click on login,you should be directed to the second activity. What will happen if we click that? What will happen if you type something wrong? I’m going to type something wrong and I’m going to click on login. You can see here the number of attempts remaining is equal to four now.
If you keep clicking on login,it decreases and after it creates a zero,you can see that the button gets disabled. The button is now no longer available to be clicked. If you try to click it you can see that nothing happens,this is a simple login application which you can use.
This is solely for a single person who knows the user name and password. This is a simple demonstration of the login application. This tutorial is for all those Android beginners who want to learn Android development,by doing these simple projects,it helps you to learn and write better and faster.