What is Login

In my program i going to use the Login page which take the userid and password after button click if the userid and password is correct than it will open the new Activity otherwise it will display the message invalid userid or password.


Login:.

In this section i am going to show the login with practical example.

First open the android studio and create a project and give the name of your project like LoginDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

Drag and drop the two TextView ,one EditText , one Password and one Button from pallet. Open the activity_main.xml file and code look like this


<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"
    tools:context="com.example.niitpc.myapplication.MainActivity">


    <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Userid" />

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

    <TextView
        android:id="@+id/textView5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Password" />

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

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClickButton"
        android:text="Login" />


</LinearLayout>


     

Take a new activity for this you right click on the app folder and a pop menu display click new->Activity->Empty Activity. A dialog box will open you can change the Activity Name but directly click on finish.

This will create the Empty Activity and simple drag and drop TextView from pallet and set the property android:Text="Welcome to this page".

Open the MainActivity.java file and write code look like this.


package com.example.niitpc.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

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

        userid=(EditText)findViewById(R.id.editText);
        password=(EditText)findViewById(R.id.editText2);
    }
    public void onClickButton(View v)
    {
        String user=userid.getText().toString();
        String pwd=password.getText().toString();
        if(user.equals("Ram") && pwd.equals("kumar"))
        {
            Intent intent=new Intent(getApplicationContext(),Main2Activity.class);
            startActivity(intent);
        }
        else
        {
            Toast.makeText(MainActivity.this, "Invalid userid or password", Toast.LENGTH_SHORT).show();
        }
   }
}
 

Explanation:

EditText userid:This statement create the object userid of TextView.

EditText password:This statement create the object password of TextView.

userid=(EditText)findViewById(R.id.editText);This statement initialized the userid object.

password=(EditText)findViewById(R.id.editText2):This statement initialized the password object.

onClickButton():Here i am creating the onClickButton method.

String user=userid.getText().toString():In this line i am taking the value from userid using getText() and after converting into String assign into user variable.

String pwd=password.getText().toString();:In this line i am taking the value from password using getText() and after converting into String assign into pwd variable.

if(user.equals("Ram") && pwd.equals("kumar")):Here i am going to check the value of user variable as Ram and pwd value as kumar with equal().

Intent intent=new Intent(getApplicationContext(),Main2Activity.class):In this statement i am going to create the object of Intent and Intent constructor take two argument current context and the Activity that you want to open. In my case i want to open the Main2Activity.Do not forgot the Write like this Main2Activity.class .

startActivity(intent):The startActivity() take the Intent that you want to start as argument.

else:else execute when the if condtion is false.

Toast.makeText(MainActivity.this, "Invalid userid or password", Toast.LENGTH_SHORT).show():This statement display the message onto the screen after login fail.


Login

What is login page for video Click here

Smiley face                     Smiley face