How to Create a login Activity

Practical - 10
Using Android, Create a login Activity. It asks “username” and “password” from user. If username and password are valid, it displays Welcome message using new activity.


Code:
MainActivity.java
package com.believe.loginmodule;

import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText musername;
    EditText mpassword;
    Button mlogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window w = getWindow(); // in Activity's onCreate() for instance
            w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }

        musername=(EditText)findViewById(R.id.et_username);
        mpassword=(EditText)findViewById(R.id.et_password);
        mlogin=(Button)findViewById(R.id.bt_login);

        musername.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    musername.setHint("");
                else
                    musername.setHint("Username");
            }
        });

        mpassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    mpassword.setHint("");
                else
                    mpassword.setHint("Password");
            }
        });

        mlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!musername.getText().toString().equals("") && !mpassword.getText().toString().equals(""))
                {
                    if(musername.getText().toString().equals("birju"))
                    {
                        if(mpassword.getText().toString().equals("1234"))
                        {
                            Intent intent=new Intent(MainActivity.this,WelcomeScreen.class);
                            intent.putExtra("user",musername.getText().toString());
                            startActivity(intent);
                        }
                        else
                        {
                            Toast.makeText(MainActivity.this,"Password is incorrect!",Toast.LENGTH_SHORT).show();
                        }
                    }
                    else
                    {
                        Toast.makeText(MainActivity.this,"Invaild username!",Toast.LENGTH_SHORT).show();
                    }
                }
                else
                {
                    Toast.makeText(MainActivity.this,"Please fill both fields!",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@drawable/bg1"
    android:layout_height="match_parent"
    tools:context="com.believe.loginmodule.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="65dp"
        android:background="@null"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@+id/button2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.278"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="24dp"
            android:background="@drawable/ic_account" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="32dp"
            android:layout_marginRight="32dp"
            android:background="@drawable/shape"
            android:orientation="vertical">

            <EditText
                android:id="@+id/et_username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="32dp"
                android:layout_marginRight="32dp"
                android:background="@android:color/transparent"
                android:hint="Username"
                android:inputType="text"
                android:padding="8dp"
                android:textAlignment="center"
                android:textColor="#FFFFFF"
                android:textColorHint="#FFFFFF"
                android:textSize="18dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"

            android:layout_marginLeft="32dp"
            android:layout_marginRight="32dp"
            android:background="@drawable/shape"
            android:orientation="vertical">

            <EditText
                android:id="@+id/et_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="32dp"
                android:layout_marginRight="32dp"
                android:background="@android:color/transparent"
                android:hint="Password"
                android:inputType="textPassword"
                android:padding="8dp"
                android:textAlignment="center"
                android:textColor="#FFFFFF"
                android:textColorHint="#FFFFFF"
                android:textSize="18dp" />
        </LinearLayout>

    </LinearLayout>

    <Button
        android:id="@+id/bt_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#25FFFFFF"
        android:text="Login"
        android:textColor="#FFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</RelativeLayout>




WelcomeScreen.java
package com.believe.loginmodule;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class WelcomeScreen extends AppCompatActivity {

    TextView musername;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome_screen);

        musername=(TextView)findViewById(R.id.tv_user);
        musername.setText(getIntent().getExtras().getString("user"));
    }
}


activity_welcome_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:gravity="center"
    tools:context="com.believe.loginmodule.WelcomeScreen">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:textSize="50sp"/>

    <TextView
        android:id="@+id/tv_user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="username"
        android:layout_marginTop="16dp"
        android:textColor="@color/colorAccent"
        android:textSize="32sp"/>

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="You are successfully logged in!"
        android:textSize="24sp"/>

</LinearLayout>



Output:







Comments

Popular posts from this blog

Study of DB Miner Tool

Study of WEKA tool

Create calculated member using arithmetic operators and member property of dimension member