Print Shortlink

Dive into Android development – III

Now that we are off the starting trouble in Android development, let’s develop a simple login page. We will create username/password fields with a login button. If the username is “Admin” and password is “Admin123” we will display a valid message, else we will display an error message. 
An unformatted UI is shown below:
Let’s create a new project “DiveIntoAndroid3” and complete the basic rituals discussed in the earlier post. To construct the UI you will use the main.xml file present in the res/layout folder. The UI design of Android applications are present in an XML file similar to Flex, Sliverlight etc. Opening the main.xml file will provide you a template, where you can drag and drop the UI components from the palette. You can switch between the design view and the code  by clicking the  tabs ‘Graphical Layout’ and ‘main.xml’ below.
Let’s drag and drop TextView and EditText components and a button. You can change the id of each component from the main.xml. We will play with the layout later. The final UI looks like below.


The ‘id’ atrributes of the components have been changed.
Now, let’s jump into some code in our Acitivity class. We can get reference to the components by using findViewById method. We can register a OnClickListener for the button and have the logic written as shown below.

public class DiveIntoAndroid3Activity extends Activity {
private EditText userNameTxt;
private EditText passwordTxt;
private TextView message;
private Button loginBtn;
    public void onCreate(Bundle savedInstanceState) {

        userNameTxt = (EditText)findViewById(;
        passwordTxt = (EditText)findViewById(;
        message = (TextView)findViewById(;
        loginBtn = (Button)findViewById(;
        loginBtn.setOnClickListener(new OnClickListener() {
              public void onClick(View view) {
 if(“Admin”.equals(userNameTxt.getText().toString())&&     “Admin123”.equals(passwordTxt.getText().toString()))
                   message.setText(“Credentials are valid”);
                 message.setText(“Credentials are invalid “);

Running this application will give you the simple login functionality that you are looking for.

Leave a Reply