Saturday, January 26, 2013

Customize Login Activity in Android

Android has a built-in activity for creating login page called LoginActivity. Which provide you all the basic functionality of login system you only need to put following lines into the AndroidManifest.xml file.

<intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

The complete code for AndroidMenifest.xml will be than looks like:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.login"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.login.LoginActivity"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustResize|stateVisible" >
           
            <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
    </application>

</manifest>

Now you can run your Login application.

But, if you want to make customize login activity using if-else conditions then follow these steps.

1. Make an xml file for your your project main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textEmailAddress" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/psw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/login" />

</LinearLayout>


2. Make a class Login.java

    package com.example.login;
   
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    public class Login extends Activity {

        Button loginbtn;
        String email="student",psw="123";
        EditText login_email,login_psw;
       
        /** Called when the activity is first created. */

        @Override

        public void onCreate(Bundle savedInstanceState)
        {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
           
            loginbtn=(Button)findViewById(R.id.login);
            login_email =(EditText)findViewById(R.id.email);
            login_psw =(EditText)findViewById(R.id.psw);
            loginbtn.setOnClickListener(new View.OnClickListener(){

                public void onClick(View v) {

                    // TODO Auto-generated method stub

                    if(login_email.getText().toString().equals(email) && login_psw.getText().toString().equals(psw) )

                    {

                        Toast.makeText(Login .this, "Login Successful", Toast.LENGTH_LONG).show();

                    }

                    else

                    {

                        Toast.makeText(Login .this, "Wrong Email or Password", Toast.LENGTH_LONG).show();

                    }   
                }
            });
        }
    }

3. Edit AndroidMenifest.xml file
** Make sure that in android:name="com.example.login.Login" the last name should be your class name (as "Login" in this case)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.login"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.login.Login"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustResize|stateVisible" >
           
            <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
    </application>  

</manifest>

Now Run your project. and Test your application.

1 comment:

  1. ⦁ Accept six subject Marks from user.
    1 And also find the grade of the student
    2 if Student is fail in 2 subject then result is A.T.K.T.
    3 if student is fail in More than 2 subject than Fail

    5 Grading is based on Avg Marks
    Avg >=75 and avg <100 Dist
    Avg >=60 and avg <75 First
    Avg >=50 and avg<60 Second
    Avg>=35 and avg<50 Pass
    Below 35 fail

    Please post code for this on rahulvb9527@gmail.com

    ReplyDelete