Android Tutorial For Beginners

Android Tutorial For Beginners

Android Tutorial For Beginners is especially focus on beginners to the android world. In this tutorial you are going to see about developing an android login system using android intent.Lets start the android tutorial for beginners.


Android Tutorial for beginners Example – Login System Version 1

In this android tutorial for beginners, login system version 1 example, you are going to build a simple login system. This system verifies whether the user name and password is entered correct, based on that it will take the user to corresponding pages.

If the user credentials are valid then welcome page will be displayed, otherwise controller will redirect the user to error page. In this example I gave username as vimaltuts and password as vimalraj. On the next tutorial we are about to improve this system by including admin section, new user sign in page, and retrieve password based on sequrity question.

Step By Step Procedure – Login System Version 1

  • Create a new android project — New -> Android Application Project
  • Name the project such as LoginSystemV1.
Creating activity_main.xml
  • We need two android EditText to get username and password.
  • I am not going to use android TextView to display like username, password.Because it simply take some space to show the message.
  • So we are going to use the android EditText to accomplish our display TextView messages like “Enter Username” and “Enter Password”.
  • Place the below code to the activity_main.xml
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <EditText
        android:id="@+id/usrPassTxt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="170dp"
        android:inputType="text"
        android:text="@string/password"
        android:textColor="@color/gray"
        android:textSize="@dimen/big"
        android:typeface="serif"  />
    <Button
        android:id="@+id/loginBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/login"
        android:textSize="@dimen/big"
        android:textStyle="bold"
        android:typeface="serif"
        android:layout_below="@+id/usrPassTxt"
        android:layout_marginTop="60dp" />
    <EditText
        android:id="@+id/usrNameTxt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:inputType="textEmailAddress"
        android:text="@string/user_name"
        android:textColor="@color/gray"
        android:textSize="@dimen/big"
        android:typeface="serif"  />
</RelativeLayout>
Creating MainActivity.java
  • Set the onclick listener to the Login button.
  • In the onclick method we are validating the user credentials.
  • In the If block we are checking whether the user name and password is valid. Then set the Intent to WelcomePage.class
  • And put the Extra Information to the Intent, Here we want to display a welcome message containing the username.
  • myIntent.putExtra("value", name);
  • So when the intent goes to WelcomePage.java we can retrieve the Extra Information out of the Intent.
  • The rest both else if and else block will take the user to Error Page.
  • But In the else if we are checking whether the user name is correct. Then we will show a custom message to the user like User name Found, But the Password is MisMatched.
  • For that purpose we are going to Put Extra Information to the Intent.
    like (“user”,”ok”) which means the value of the User = Ok.
    Here in this else if we set the intent to ErrorPage.class, so When this intent take the controll over to ErrorPage.java we can retrieve the Extra Information of the intent, here the “user”.
  • For else part where both the username and password is wrong, we only set the intent to ErrorPage.class we are not going to put any Extra information to the intent.
  • Place the below code to MainActivity
package com.example.androidlogin;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
	private EditText uName;
	private EditText uPass;
	private Button loginBtn;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpViews();
    }
	@Override
	protected void onResume() {
		super.onResume();
		uName.setText("Enter User Name");
		uName.setTextColor(Color.parseColor("#888888"));
		uPass.setText("Enter Password");
		uPass.setInputType(InputType.TYPE_CLASS_TEXT);
		uPass.setTextColor(Color.parseColor("#888888"));
	}
	private void setUpViews() {
		uName=(EditText)findViewById(R.id.usrNameTxt);
		uPass=(EditText)findViewById(R.id.usrPassTxt);
		uName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(uName.getText().toString().equalsIgnoreCase("Enter User Name")){
					if(hasFocus){
						uName.setTextColor(Color.parseColor("#000000"));
						uName.setText("");
					}
				}
				else if(uName.getText().toString().equalsIgnoreCase("")){
					uName.setTextColor(Color.parseColor("#888888"));
					uName.setText("Enter User Name");
				}
			}
		});
        uPass.setOnFocusChangeListener(new View.OnFocusChangeListener(){
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(uPass.getText().toString().equalsIgnoreCase("Enter Password")){
					if(hasFocus){
						uPass.setInputType(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_VARIATION_PASSWORD);
						uPass.setTextColor(Color.parseColor("#000000"));
						uPass.setText("");
					}
				}
				else if(uPass.getText().toString().equalsIgnoreCase("")){
						uPass.setInputType(InputType.TYPE_CLASS_TEXT);
						uPass.setTextColor(Color.parseColor("#888888"));
						uPass.setText("Enter Password");
				}
			}
		});
		loginBtn=(Button)findViewById(R.id.loginBtn);
		loginBtn.setFocusableInTouchMode(true);
		loginBtn.requestFocus();
		loginBtn.setOnClickListener(new OnClickListener() {
			private String pass;
			private String name;
			Intent myIntent;
			@Override
			public void onClick(View v) {
				name=uName.getText().toString();
				pass=uPass.getText().toString();
				if(name.equalsIgnoreCase("vimaltuts") && pass.equalsIgnoreCase("vimalraj")){
					myIntent=new Intent(MainActivity.this,WelcomePage.class);
					System.out.println("---IF---");
			    }
				else if(name.equalsIgnoreCase("vimaltuts")){
			    	myIntent=new Intent(MainActivity.this,ErrorPage.class);
			    	myIntent.putExtra("user", "ok");
			    	System.out.println("---ELSE IF---");
			    }
			    else{
			    	myIntent=new Intent(MainActivity.this,ErrorPage.class);
			    	System.out.println("---ELSE---");
			    }
				myIntent.putExtra("value", name);
				startActivity(myIntent);
			}
		});
	}
}
Creating welcome.xml
  • If the user credentials are valid then we need to redirect the user to welcome page or home page.
  • So Lets create a welcome.xml for this.
  • This is very simple layout. Containing only one android TextView to display a welcome message to the user.
  • Static mesaage is like simply shows Welcome message regardless of user.
  • So We need the system to show dynamic message such as Including the user name. And perform task based on the user.
  • Finally place a SignOut Button.
  • Copy the below code to the welcome.xml
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:id="@+id/msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/welcome"
        android:textSize="@dimen/big"
        android:textStyle="bold"
        android:typeface="serif" />
    <Button
        android:id="@+id/backBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@string/back"
        android:textSize="@dimen/big"
        android:textStyle="bold" />
</RelativeLayout>
Creating WelcomPage.java
  • In this class we are going to display a welcome message along with the username.
  • In the MainActivity.java class we have set the Extra Information to the Intent.
  • Now we are going to retrieve the Extra Information from the Intent. And display the Welcome message with username.
Bundle myValues=getIntent().getExtras();
welcomeMsg.setText("Welcome : "+myValues.getString("value"));

Place the below code to the WelcomePage.Java

package com.example.androidlogin;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class WelcomePage extends Activity {
	Bundle myValues;
	private TextView welcomeMsg;
	private Button backButton;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        myValues=getIntent().getExtras();
        setUpViews();
    }
	private void setUpViews() {
		welcomeMsg=(TextView)findViewById(R.id.msg);
		welcomeMsg.setText("Welcome : "+myValues.getString("value"));
		backButton=(Button)findViewById(R.id.backBtn);
		backButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
}
Creating error.xml
  • If the user credentials are invalid then we need to redirect the user to error page.
  • So Lets create a error.xml for this.
  • This is very simple layout. Containing only one android TextView to display a welcome message to the user.
  • Static mesaage is like simply shows Invalid Entry.
  • But we need to validate one more thing.
  • If the user name is correct but the password is wrong. Then we need the system to show a message such as User Name Found but Password is Wrong.
  • Finally place a Back Button.
  • Copy the below code to the error.xml
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:id="@+id/errmsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/error"
        android:textSize="@dimen/big"
        android:textStyle="bold"
        android:typeface="serif" />
    <Button
        android:id="@+id/backBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@string/back"
        android:textSize="@dimen/big"
        android:textStyle="bold" />
</RelativeLayout>
Creating ErrorPage.java
  • Here we are checking whether the username is valid.
if(myValues.getString("user")!=null){
			if(myValues.getString("user").equals("ok")){
				errorMsg.setText("User Found : "+myValues.getString("value")+"\n\nBut Password is Wrong \nContact Admin to Reset");
			}
			else{
				errorMsg.setText("Invalid Entry : "+myValues.getString("value"));
			}
		}
In the above code we retrieve and checking the Extra information set in the MainActivity.java
Based on the “user” value the if or else part will be evaluated.
Then for the back button we are just finish() the activity. So the MainActivity will be shown. When once again the MainAcitivity shown, the onResume() method will be called.

Android Tutorial For Beginners – Output ScreenShot


android tutorial for beginners




login system




android login system




android login




login android system




android login system




android tutorial for beginners



Android Tutorial For Beginners – Full Source Code

activity_main.xml
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <EditText
        android:id="@+id/usrPassTxt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="170dp"
        android:inputType="text"
        android:text="@string/password"
        android:textColor="@color/gray"
        android:textSize="@dimen/big"
        android:typeface="serif"  />
    <Button
        android:id="@+id/loginBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/login"
        android:textSize="@dimen/big"
        android:textStyle="bold"
        android:typeface="serif"
        android:layout_below="@+id/usrPassTxt"
        android:layout_marginTop="60dp" />
    <EditText
        android:id="@+id/usrNameTxt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:inputType="textEmailAddress"
        android:text="@string/user_name"
        android:textColor="@color/gray"
        android:textSize="@dimen/big"
        android:typeface="serif"  />
</RelativeLayout>



welcome.xml
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:id="@+id/msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/welcome"
        android:textSize="@dimen/big"
        android:textStyle="bold"
        android:typeface="serif" />
    <Button
        android:id="@+id/backBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@string/back"
        android:textSize="@dimen/big"
        android:textStyle="bold" />
</RelativeLayout>



error.xml
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:id="@+id/errmsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/error"
        android:textSize="@dimen/big"
        android:textStyle="bold"
        android:typeface="serif" />
    <Button
        android:id="@+id/backBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@string/back"
        android:textSize="@dimen/big"
        android:textStyle="bold" />
</RelativeLayout>



AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidlogin"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="15" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
            android:name=".LoginProcess"
            android:label="@string/title_activity_main" />
         <activity
            android:name=".WelcomePage"
            android:label="@string/title_activity_main" />
         <activity
            android:name=".ErrorPage"
            android:label="@string/title_activity_main" />
    </application>
</manifest>



strings.xml
<resources>
    <string name="app_name">AndroidLoginSystem</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="user_name">Enter User Name</string>
    <string name="password">Enter Password</string>
    <string name="login">Login</string>
    <color name="gray">#888888</color>
    <string name="error">Invalid Login</string>
    <string name="welcome">Welcome</string>
    <string name="back">Back</string>
</resources>



MainActivity.java
package com.example.androidlogin;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
	private EditText uName;
	private EditText uPass;
	private Button loginBtn;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpViews();
    }
	@Override
	protected void onResume() {
		super.onResume();
		uName.setText("Enter User Name");
		uName.setTextColor(Color.parseColor("#888888"));
		uPass.setText("Enter Password");
		uPass.setInputType(InputType.TYPE_CLASS_TEXT);
		uPass.setTextColor(Color.parseColor("#888888"));
	}
	private void setUpViews() {
		uName=(EditText)findViewById(R.id.usrNameTxt);
		uPass=(EditText)findViewById(R.id.usrPassTxt);
		uName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(uName.getText().toString().equalsIgnoreCase("Enter User Name")){
					if(hasFocus){
						uName.setTextColor(Color.parseColor("#000000"));
						uName.setText("");
					}
				}
				else if(uName.getText().toString().equalsIgnoreCase("")){
					uName.setTextColor(Color.parseColor("#888888"));
					uName.setText("Enter User Name");
				}
			}
		});
        uPass.setOnFocusChangeListener(new View.OnFocusChangeListener(){
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(uPass.getText().toString().equalsIgnoreCase("Enter Password")){
					if(hasFocus){
						uPass.setInputType(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_VARIATION_PASSWORD);
						uPass.setTextColor(Color.parseColor("#000000"));
						uPass.setText("");
					}
				}
				else if(uPass.getText().toString().equalsIgnoreCase("")){
						uPass.setInputType(InputType.TYPE_CLASS_TEXT);
						uPass.setTextColor(Color.parseColor("#888888"));
						uPass.setText("Enter Password");
				}
			}
		});
		loginBtn=(Button)findViewById(R.id.loginBtn);
		loginBtn.setFocusableInTouchMode(true);
		loginBtn.requestFocus();
		loginBtn.setOnClickListener(new OnClickListener() {
			private String pass;
			private String name;
			Intent myIntent;
			@Override
			public void onClick(View v) {
				name=uName.getText().toString();
				pass=uPass.getText().toString();
				if(name.equalsIgnoreCase("vimaltuts") && pass.equalsIgnoreCase("vimalraj")){
					myIntent=new Intent(MainActivity.this,WelcomePage.class);
					System.out.println("---IF---");
			    }
				else if(name.equalsIgnoreCase("vimaltuts")){
			    	myIntent=new Intent(MainActivity.this,ErrorPage.class);
			    	myIntent.putExtra("user", "ok");
			    	System.out.println("---ELSE IF---");
			    }
			    else{
			    	myIntent=new Intent(MainActivity.this,ErrorPage.class);
			    	System.out.println("---ELSE---");
			    }
				myIntent.putExtra("value", name);
				startActivity(myIntent);
			}
		});
	}
}



Welcome.java
package com.example.androidlogin;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class WelcomePage extends Activity {
	Bundle myValues;
	private TextView welcomeMsg;
	private Button backButton;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        myValues=getIntent().getExtras();
        setUpViews();
    }
	private void setUpViews() {
		welcomeMsg=(TextView)findViewById(R.id.msg);
		welcomeMsg.setText("Welcome : "+myValues.getString("value"));
		backButton=(Button)findViewById(R.id.backBtn);
		backButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
}



ErrorPage.java
package com.example.androidlogin;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ErrorPage extends Activity {
	private Bundle myValues;
	private TextView errorMsg;
	private Button backButton;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.error);
        myValues=getIntent().getExtras();
        setUpViews();
    }
	private void setUpViews() {
		errorMsg=(TextView)findViewById(R.id.errmsg);
		if(myValues.getString("user")!=null){
			if(myValues.getString("user").equals("ok")){
				errorMsg.setText("User Found : "+myValues.getString("value")+"\n\nBut Password is Wrong \nContact Admin to Reset");
			}
		}
		else{
			errorMsg.setText("Invalid Entry : "+myValues.getString("value"));
		}
		backButton=(Button)findViewById(R.id.backBtn);
		backButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
}
In this Android Tutorial For Beginners we have developed a simple yet a complete login system. In the Login System Version 2 Tutorial we are about to improve this system by including admin section, new user sign in page, and retrieve password based on sequrity question. If you like this android tutorial for beginners then share it to your friends.

One comment

  1. Pingback: Login activity in android | Code and Programming

Leave a Reply

Scroll To Top

Foolow Me on Google Plus

VimalTuts on Twitter
61 people follow VimalTuts
sureshkurupinderubemafoominkyuuuabhijeetjatarmlMillerHealanosma