User Login page Example in android studio using java

This article will explain about Login process of an Android app using Java code. A basic Login example will be to understand Android studio logic build and event handle all those parts. At first, create an Android application from Android Studio. By default, MainActicivity.java and activity_main.xml will be created in below below-mentioned project structure path.

app/res/layout/activity_main.xml
com/example/app/MainActivity.java

Two Text view and edit view for User name and Password and Button for the onClick event to call for the next page will be implemented in an XML file. Button Onclick event will check the Login process and it will move to the next page of apps.

XML code

<?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:layout_centerVertical="true"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:visibility="visible">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login page"
        android:textSize="20dp" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/login" />

    <LinearLayout
        android:layout_width="match_parent"
        android:id="@+id/toolbar"
        android:layout_height="match_parent"
        android:background="#3412"
        android:textAlignment="gravity"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView9"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="User Name" />

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

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

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

            <EditText
                android:id="@+id/editTextNumberPassword2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="numberPassword" />
        </LinearLayout>

        <Button
            android:id="@+id/button2"
            android:layout_width="186dp"
            android:layout_height="wrap_content"
            android:onClick="movepage"
            android:text="Login" />
    </LinearLayout>
</LinearLayout>

On the button, click the Login () method that is calling and check the login user name and page word then will give the toast message of "Login success with given input" Text and Login to the app. If wrong input is given "Wrong input" Toast message will come. Also, after login text view is saved in Bundles.

Bundles are Data structures that are used to pass data inside an app. Its key-pair value with different data types like primitive Datatype string, boolean, etc. And then it's useful to send data from one activity to another activity.

Java Code

public class MainActivity extends AppCompatActivity {
    Button clk;
    TextView txtView;
    EditText email, password;

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

        email = (EditText) findViewById(R.id.editTextTextEmailAddress3);
        password = (EditText) findViewById(R.id.editTextNumberPassword2);
        clk = (Button) findViewById(R.id.button2);
        txtView = (TextView) findViewById(R.id.textView9);
    }

    public void movepage(View view) {
        String stEmail = email.getText().toString();
        String stPwd = password.getText().toString();

        if (stEmail.equals("[email protected]") && stPwd.equals("1234")) {
            Toast.makeText(getBaseContext(), "Login success with given input", Toast.LENGTH_SHORT).show();
            Intent objIntent = new Intent(MainActivity.this, Activity2.class);
            Bundle bundle = new Bundle();
            bundle.putString("email", stEmail);
            bundle.putString("pwd", stPwd);
            objIntent.putExtras(bundle);
            startActivity(objIntent);
        } else if (stEmail.equals("") && stPwd.equals("")) {
            Toast.makeText(getBaseContext(), "Enter Email and password", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getBaseContext(), "Wrong input", Toast.LENGTH_SHORT).show();
        }
    }
}

Output

Login


Similar Articles