Menu
- Android Basics
- Android - User Interface
I'd like to know what is the best practice/way of programmatically register a broadcast receiver. I want to register specific receivers according to user choice. As the registration is done through the manifest file, I'm wondering if there's a proper way to achieve this in code.
- Android Advanced Concepts
- Android Useful Examples
- Android Useful Resources
- Selected Reading
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
![Boldbeast Register Code For Android Boldbeast Register Code For Android](/uploads/1/2/5/8/125848881/217691844.jpg)
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.