Static Login Page-Android

by Murtaza FarhatYar | 16 Jan 2019

Many applications need to have a login page or a registration page in order to log into the desired application; And use that as a user. In this article I am going to design a static login page in Android Studio, which takes the entered information and show them on a “TextView” in another activity .

Please follow the following steps.

firstly Create a layout by any name.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    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:background="@color/colorPrimaryDark"
    tools:context="com.example.zakia.myapplication.MainActivity">
     <ImageView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:src="@drawable/default_avatar"
         android:scaleType="fitStart"
         />
   <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Sign Up Here"
       android:textSize="45dp"
       android:fontFamily="serif"
       android:textAlignment="center"
       />
   <EditText
       android:id="@+id/firstname"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:ems="20"
       android:inputType="textPersonName"
       android:hint=" fitstname..."
       android:paddingTop="10sp"
       android:layout_marginLeft="20dp"
       />
   <EditText
       android:id="@+id/lastname"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:ems="20"
       android:inputType="textPersonName"
       android:hint=" lastname..."
       android:paddingTop="10sp"
       android:layout_marginLeft="20dp"
       />
   <EditText
       android:id="@+id/username"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:ems="20"
       android:inputType="textPersonName"
       android:hint=" username..."
       android:paddingTop="10sp"
       android:layout_marginLeft="20dp"
       />
   <EditText
       android:id="@+id/password"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:ems="20"
       android:inputType="textPassword"
       android:hint=" password..."
       android:paddingTop="10sp"
       android:layout_marginLeft="20dp"
       />
   <Button
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:ems="20"
       android:text="LOGIN"
       android:fontFamily="serif"
       android:onClick="StartMethod"
       />   
</LinearLayout>

Next step is find the java file of Main Activity .

MainActivity.java:

package com.example.zakia.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
   EditText firstname,lastname, username, password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void StartMethod(View view) {
     // access the components of xml file
        firstname= findViewById(R.id.firstname);
        lastname= findViewById(R.id.lastname);
        username= findViewById(R.id.username);
        password= findViewById(R.id.password);
         // get vlaues
        String fname, lname, uname, pword;
        fname= firstname.getText().toString();
        lname= lastname.getText().toString();
        uname= username.getText().toString();
        pword= password.getText().toString();
        // link the present activity to desired activity
        Intent intent= new Intent(this, HomeActivity.class);
        // send data to another activity using putExtra
        intent.putExtra("firstname",fname);
        intent.putExtra("lastname",lname);
        intent.putExtra("username",uname);
        intent.putExtra("password",pword);
        startActivity(intent);
    }
}

 

see also : Android:CustomListView

Another activity which we send the data is HomeActivity.

the xml file is as activity_home.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"
    tools:context="com.example.zakia.myapplication.HomeActivity">

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="your info"
        />
</LinearLayout>

The final step is the related java file of activity_home.xml.

Home_Activity.java:

package com.example.zakia.myapplication;

import android.app.SearchManager;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class   HomeActivity extends AppCompatActivity {
    EditText phone;
    ToggleButton blue;
    TextView  sss , fname, lname,uname,pword;
    Switch sw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

      String fname= getIntent().getExtras().getString("firstname");
      String lname= getIntent().getExtras().getString("lastname");
      String uname= getIntent().getExtras().getString("username");
      String pword= getIntent().getExtras().getString("password");
     sss = findViewById(R.id.name);
     // Toast.makeText(this,"firstname: "+fname+"lastname: "+lname+"username: "+uname+"password: "+pword,Toast.LENGTH_LONG).show();

         sss.setText(fname+"\n"+lname+"\n"+uname+"\n"+pword);
}
}

The result is as bellow:

for more articles follow us here

 

 

previous post
Android:CustomListView