Friday 8 July 2016

Android Login page


Create login page login.xml -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="#000"
    android:gravity="center"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/userName"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_margin="10dp"
        android:background="#fff"
        android:hint="UserName" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_margin="10dp"
        android:layout_marginTop="20dp"
        android:background="#fff"
        android:hint="Password"
        android:password="true" />

    <Button
        android:id="@+id/submit"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:layout_marginTop="10dp"
        android:text="Login" />

</LinearLayout>


Create a  Login.class in src/Login.class

package com.example.loginpage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends Activity {
EditText userName, password;
Button login;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = (EditText)findViewById(R.id.userName);
password = (EditText)findViewById(R.id.password);
login = (Button)findViewById(R.id.submit);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (userName.getText().toString().equals("")) {
Toast.makeText(getApplicationContext(), "Please enter username", Toast.LENGTH_SHORT).show();
}else if (password.getText().toString().equals("")) {
Toast.makeText(getApplicationContext(), "Please enter password", Toast.LENGTH_SHORT).show();
}else {
Intent in = new Intent(Login.this, LoginSuccess.class);
startActivity(in);
}
}
});
}


}


After successfully login it redirect to LoginSuccess.class find it below class and login_success.xml

login_success.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"
    android:gravity="center" >
    

    <TextView android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login Success"
        android:textColor="#000"
        android:gravity="center"/>
</LinearLayout>


src/LoginSuccess.class - 

package com.example.loginpage;

import android.app.Activity;
import android.os.Bundle;

public class LoginSuccess  extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login_success);
}

}

Register your class into AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Login"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name=".LoginSuccess"></activity>
    </application>

</manifest>

Screens
login-


Login success - 





No comments:

Post a Comment