banner



Android Studio How To Create A Login Page

The login page is very necessary for an application. Both android applications and or web-based applications. Without a login page, anyone can make changes to the data. However, not everyone is aware of the importance of the login page. In this tutorial article, I will provide steps on how to make it. This article I titled How to Make Login Page in Android Studio.

For information, when writing this article, I use Android Studio version 3.6.1.

  • Android Studio 3.6.1
  • Build #AI-192.7142.36.36.6241897, built on February 27, 2020
  • Runtime version: 1.8.0_212-release-1586-b4-5784211 amd64
  • VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o

Steps to Make Login Page in Android Studio

  1. Create a new project.
  2. Select Empty Activity in the Select a Project Template window, click Next.
  3. In the Configure Your Project window give the name of your project Login Page. Then click Finish.
  4. After you click the Finish button, 2 files will be created automatically. Those two files are activity_main.xml and MainActivity.java. The code for these two files can be seen in the code list below.
  5. After that, create a second activity. If the user types the username and password correctly, the second activity will be displayed. After that, the file will be formed as shown below.
How to make login page in Android Studio
How to make login page in Android Studio

XML and Java Login Page in Android Studio

activity_main.xml

Explanation activity_main.xml:

  • Using ConstraintLayout. By using ConstrainLayout, elements will be bound. So, the display will adjust automatically.
  • Use the EditText element for Name and Password.
  • Use the Button element for Login Button.
  • Using TextView for information at the bottom of the Login button.
          <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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"     tools:context=".MainActivity">       <EditText         android:id="@+id/etName"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:ems="10"         android:hint="Name"         android:inputType="textPersonName"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintVertical_bias="0.186" />      <EditText         android:id="@+id/etPassword"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:ems="10"         android:hint="Password"         android:inputType="textPassword"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toBottomOf="@+id/etName"         app:layout_constraintVertical_bias="0.082" />      <Button         android:id="@+id/btnLogin"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Login"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintHorizontal_bias="0.498"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toBottomOf="@+id/etPassword"         app:layout_constraintVertical_bias="0.118" />      <TextView         android:id="@+id/tvInfo"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="No of incorrect attempt: "         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintHorizontal_bias="0.498"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toBottomOf="@+id/btnLogin"         app:layout_constraintVertical_bias="0.196" /> </androidx.constraintlayout.widget.ConstraintLayout>        

MainActivity.java

          package com.howcreateit.loginpage;  import androidx.appcompat.app.AppCompatActivity;  import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView;  public class MainActivity extends AppCompatActivity {      private EditText Name;     private EditText Password;     private TextView Info;     private Button Login;     private int counter = 10;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          Name = (EditText)findViewById(R.id.etName);         Password = (EditText)findViewById(R.id.etPassword);         Info = (TextView)findViewById(R.id.tvInfo);         Login = (Button)findViewById(R.id.btnLogin);          Info.setText("No of attempts remaining: 10");          Login.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 validate(Name.getText().toString(), Password.getText().toString());             }         });      }      //Create the Validate Function     private void validate(String userName, String userPassword){         if((userName.equals("HowCreateIt")) && (userPassword.equals("Yes"))){             Intent intent = new Intent(MainActivity.this, OtherActivity.class);             startActivity(intent);         }else{             counter--;              Info.setText("No of attempts remaining: " + String.valueOf(counter));              if(counter == 0){                 Login.setEnabled(false);             }         }     } }                  

activity_other.xml

          <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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"     tools:context=".OtherActivity">      <TextView         android:id="@+id/textView"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="This is Other Activity"         android:textSize="30sp"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintVertical_bias="0.314" /> </androidx.constraintlayout.widget.ConstraintLayout>        

OtherActivity.java

          package com.howcreateit.loginpage;  import androidx.appcompat.app.AppCompatActivity;  import android.os.Bundle;  public class OtherActivity extends AppCompatActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_other);     } }                  

You can add an image as a background in this Login Activity. How to add an image? Please read this article android studio background image. You can add gradient also in this activity. To do this please read gradient android studio.

By following the steps above, you will get a final result like this:

Final result How to make login page in Android Studio
Final result How to make login page in Android Studio

You can try the input Name and Password. When you the wrong Name and or Password, the No of attempts remaining will be reduced. But, if Name and Password correct then the second activity appeared.

Other Activity (Success)
Other Activity (Success)

That's the tutorial on how to create a login page. If you want to ask, please ask through comments. I will answer your question. I will give you another tutorial. Please share this article if it is useful. Share to your respective social media accounts. You can share it to FB, IG or other media.

Than you.

Related posts:

Android Studio How To Create A Login Page

Source: https://howcreateit.com/programming/android-studio/how-to-make-login-page-in-android-studio/

Posted by: furnesswidefirearm.blogspot.com

0 Response to "Android Studio How To Create A Login Page"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel