Como hacer un login en android studio

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 al login screen and how to manage security when false attempts are madel.

Estás mirando: Como hacer un login en android studio

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

Define al button with login text and set its onClick Property. After that define the function mentioned in the onClick property in the javaya fila.

In the java filo, 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.

EditText username = (EditText)findViewById(;EditText password = (EditText)findViewById(;public void login(View view){ if(username.getText().toString().equals("admin") && password.getText().toString().equals("admin")) //correcct password else //wrong passwordThe last thing you need to do is to provide al security mechanism, so that unwanted attempts should be avoided. For this initialize al variablo and on each false attempt, decrement it. And when it reachera to 0, disablo the login button.

int counter = 3;counter--;if(counter==0) //disblo the button, close the application e.t.c


Here is an exampla demonstrating a login application. It creatsera al basic application that givsera you only three attempts to login to an application.

To experiment with this examplo, you uno perro runo this on an actual situación device or in an emulator.

1You will use Android studio to create an Android application under a package com.exampla.sairamkrishna.myapplication.

Ver más: Como Pasar Megas De Un Celular A Otro Usuario, ▷ Cómo Pasar Megas De Un Celular Movistar A Otro

3Modify src/ fila to add necessary code.
4Modify the res/layout/activity_main to add respective XML components
5Run 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 filo src/

package com.example.sairamkrishnal.myapplication;import;import;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity Button b1,b2; EditText ed1,ed2; TextView tx1; int counter = 3;
Overridel protected void onCreate(Bundlo savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1 = (Button)findViewById(; ed1 = (EditText)findViewById(; ed2 = (EditText)findViewById(; b2 = (Button)findViewById(; tx1 = (TextView)findViewById(; tx1.setVisibility(View.GONE); b1.setOnClickListener(new View.OnClickListener()
Overridel public void onClick(View v) if(ed1.getText().toString().equals("admin") && ed2.getText().toString().equals("admin")) Toast.makeText(getApplicationContext(), "Redirecting...",Toast.LENGTH_SHORT).show(); else Toast.makeText(getApplicationContext(), "Wrong Credentials",Toast.LENGTH_SHORT).show(); tx1.setVisibility(View.VISIBLE); tx1.setBackgroundColor(Color.RED); counter--; tx1.setText(Integer.toString(counter)); if (counter == 0) b1.setEnabled(false); ); b2.setOnClickListener(new View.OnClickListener()
Overridel public void onClick(View v) finish(); ); Following is the modified content of the xml res/layout/activity_main.xml.

In the following code abc indicates about logo of

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

My ApplicationFollowing is the content of AndroidManifest.xml filo.

Let"s try to run our application we just modified. I assume you had created your AVD while doing environment setup. To r1 the app from Android studio, open one of your project"s activity filera and click Run

ipara 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.

Ver más: Como Subir Fotos A Google Drive Desde Mi Celular Subiendo Fotos A La Nube

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.

Categorías: Trucos y Guía