Almost all android application will have login or registration process in order to authenticate a user. In this article i will be demonstrating how to design android login and registration screen design (note that it just designing the screens – no database connection or user validation).

http://sh.st/fpEy
 The final output screenshots of this tutorial will be like below image


To achieve login/registration screen design I am merging multiple android layouts. I placed Scroll View as a parent element. This Scroll View makes your screen scroll in vertical direction to avoid hiding exceeding objects on the screen. Inside scroll view I placed Relative View. The main reason for using Relative View is to make footer always stick at the bottom. Finally I am using Linear Layouts for placing Header, Form and Footer. See the following diagram to get an idea about the layouts I used.


Designing Login Screen
   In this tutorial the main focus is to creating android login, registration screens and navigating/switching between them.
1. Create a new project by going to File ⇒ New Android Project. Fill all the details and name your activity as LoginActivity.
2. Once the project is created, create a new activity class in your project src directory and name it as RegisterActivity.java ( Right Click on src/package ⇒ New ⇒ Class)
3. Now we need to create a layout for login screen. Under res/layouts create a new xml file and name it as login.xml
( Right Click on res/layout ⇒ New ⇒ Android XML File)
4. In login.xml type following code( Here i am giving code for basic layout)


  
 
        
        
 
        
                   
        
 
        
        
 
  



⇒Designing Header ( with Logo & Gradient Background )

In login screen we have header with a logo and gradient background color. Design your logo with different dimensions for high-density( 72px height), medium density ( 48 px height) and low density (36px height).


5. Create a new xml file under res/layout and name it as header_gradient.xml and type the following code

header_gradient.xml


    
    


6. In login.xml add the following code between the header comments.

login.xml


        
        
        



⇒Designing Footer ( with background repeat image )
In login screen footer has a background repeat image. When you flip the device in horizontal direction you can see the footer with background repeated image.


7. Create a new xml file under res/layout and name it as footer_repeat.xml and type the following code.

 
footer_repeat.xml



8. In login.xml type the following code between footer comments

login.xml





⇒Designing Login Form

Login form contains two textfields with labels and a login button. In your login.xml file add the following code next to footer section.


        
          
          
          
          
          
          
          
          

The Final Code for login.xml

login.xml


  
 
        
        
                
                
                
        
        
        
        
        
        
 
        
        
          
          
          
          
          
          
          
          

Designing the registration form

The registration form also had the same header and footer, but the form changes. The registration form contains fields like full name, email, password (if needed retype password). The following is the code for registration screen.

9. In your project under res/layout folder create new xml file register.xml and type the following code

register.xml


  
 
        
        
                
                
                
        
        
        
        
        
        
 
        
        
          
          
          
          
          
          
          
          
          
          
          

Switching from Login screen to Registration screen

10. Now screen designs are ready. Open your LoginActivity.java and modify your code to following. In the following code we are setting layout to login.xml. Second on clicking on Registration page link we are switching screen from LoginActivity to RegisterActivity

LoginActivity.java
package com.androidhive.loginandregister;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
 
public class LoginActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setting default screen to login.xml
        setContentView(R.layout.login);
 
        TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
 
        // Listening to register new account link
        registerScreen.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View v) {
                // Switching to Register screen
                Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(i);
            }
        });
    }
}

Switching from Registration screen to Login screen

11. Open your RegisterActivity.java and modify your code to following. To switch back to login screen just call finish(). It will close the registration screen, so login screen will be shown

RegisterActivity.java
package com.androidhive.loginandregister;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
 
public class RegisterActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set View to register.xml
        setContentView(R.layout.register);
 
        TextView loginScreen = (TextView) findViewById(R.id.link_to_login);
 
        // Listening to Login Screen link
        loginScreen.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View arg0) {
                                // Closing registration screen
                // Switching to Login Screen/closing register screen
                finish();
            }
        });
    }
}

Adding Activity entry in AndroidManifest.xml

12. Finally add entry for the newly created RegisterActivty.class in your AndroidManifest.xml file.

AndroidManifest.xml


    
 
    
        
            
                
                
            
        
 
        
        
 
    


Run your project by right clicking on your project folder ⇒ Run As ⇒ 1 Android Application. You will see an awesome output.
26 Dec 2013

0 comments :

Post a Comment

:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.

 
Top