Share this post on:

Here we will design the Login activity with validation in Android studio. We will use several widgets which is required for designing the Layout in Android.

 Login activity with validation in Android studio

Step 1: Design Layout in activity_login

<?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"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/white"
android:orientation="vertical"
tools:context=".Login">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">



<TextView
android:id="@+id/welcome_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="40dp"
android:layout_marginRight="40dp"
android:text="Sign in"
android:textColor="@color/black"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:text="Happy Earth Happy Us"
android:textColor="@color/lightblack"
android:textSize="@dimen/size16"
android:textStyle="normal" />
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_marginTop="5dp"
android:background="@color/dark_green"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp">
</View>


<ImageView
android:id="@+id/login_image_tv"
android:layout_width="300dp"
android:layout_height="250dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:src="@drawable/saveco" />
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardCornerRadius="1dp"
app:cardElevation="32dp"

android:layout_marginBottom="100dp"
android:layout_marginStart="18dp"
android:layout_marginEnd="18dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textinput_mobilenumber"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:background="@android:color/transparent"
android:drawableLeft="@drawable/user_vector"
android:drawablePadding="20dp"
android:drawableTint="@color/dark_green"
android:ellipsize="end"
android:hint="Id"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:paddingLeft="20dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:inputType="textEmailAddress"
android:maxLength="10"
android:textColor="@color/black"
android:textColorHint="#CBCDCD"
android:textSize="16sp" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textinput_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true"
app:hintEnabled="false"
app:passwordToggleTint="@color/dark_green">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:background="@android:color/transparent"
android:drawableLeft="@drawable/lock_vector"
android:drawablePadding="20dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:drawableTint="@color/dark_green"
android:ellipsize="end"
android:hint="Password"
android:paddingLeft="20dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:inputType="text"
android:maxLength="10"
android:textColor="@color/black"
android:textColorHint="#CBCDCD"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>

<Button
android:id="@+id/join"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="30dp"
android:layout_marginRight="40dp"
android:gravity="center"
android:minHeight="50dp"
android:text="Log in"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="18sp"
android:theme="@style/ThemeOverlay.Button.Darkblue" />


<LinearLayout
android:layout_width="match_parent"
android:gravity="center"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:layout_height="wrap_content">

<TextView
android:layout_width="0dp"
android:layout_weight="6"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:gravity="right|center"
android:text="Don’t have account? "
android:textColor="#4E4E4E"
android:textSize="16sp"
android:textStyle="normal" />


<TextView
android:id="@+id/donthave"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="match_parent"
android:gravity="center|left"
android:text="Join Now"
android:textColor="@color/text_color_gold"
android:textSize="16sp"
android:textStyle="normal" />

</LinearLayout>
</LinearLayout>

</androidx.cardview.widget.CardView>






</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>

Step 2: Java File to initialize all Variable in Login

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.raiyanshahid.saveecooragnicmember.conn.WebApi;
import com.raiyanshahid.saveecooragnicmember.utils.MasterFunction;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class Login extends AppCompatActivity {

    private Context mContext = Login.this;
    private MasterFunction masterFunction;
    ProgressDialog pd;
    private SharedPreferences sharedPreferences;
    private static final String PREFER_NAME = "Reg";

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

        SharedPreferences settings = getSharedPreferences(PREFER_NAME, 0);
        String membercode = settings.getString("membercode", null);
        if (membercode != null) {
            Intent intent = new Intent(mContext, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            setContentView(R.layout.activity_login);

            final EditText emailbtn = findViewById(R.id.email);
            final EditText passwordbtn = findViewById(R.id.password);
            Button login = findViewById(R.id.join);
            TextView donthave = findViewById(R.id.donthave);

            donthave.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                      startActivity(new Intent(getApplicationContext(), JoinNow.class));
                }
            });

            login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String username = emailbtn.getText().toString().trim();
                    String password = passwordbtn.getText().toString().trim();
                    if (TextUtils.isEmpty(username)) {
                        emailbtn.setError("Email can't be Empty");
                        emailbtn.setFocusable(true);
                    }

                    if (TextUtils.isEmpty(password)) {
                        passwordbtn.setError("Password Can't be empty");
                        passwordbtn.setFocusable(true);
                    }
                    if (passwordbtn.length() < 3) {
                        passwordbtn.setError("Password Should not be less than 6 character");
                        passwordbtn.setFocusable(true);
                    } else {
                        Toast.makeText(Login.this, "Login Successfully", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }
}
Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

Leave a Comment

Your email address will not be published. Required fields are marked *