How to Create a Login Form in Android Studio


Create a Login Form in Android Studio

Creating a login form at android studio is not too difficult, provided we want to make it. Previously we discussed a tutorial on how to install android studio in Windows. If you haven't installed it, you can view it in the article how to install Android Studio in Windows. After you install it, please open your android studio and create a new project. The trick is to click File> New> New Project ... and name it prject with the name Login Form.

Create a Login Form design

After you create the project as above, then please open the activity_main.xml file and enter the code as below.


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

     <ImageView
        android:id="@+id/logo"
        android:layout_width="62dp"
        android:layout_height="51dp"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="32dp"
        android:src="@drawable/home"
        android:text="Selamat Datang"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

     <TextView
        android:id="@+id/textView"
        android:layout_width="259dp"
        android:layout_height="54dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="32dp"
        android:text="Silahkan Login"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

     <TextView
        android:id="@+id/user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/logo"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="108dp"
        android:text="Username :"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

     <EditText
        android:id="@+id/inuser"
        android:singleLine="true"
        android:layout_width="359dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/user"
        android:layout_marginTop="135dp"
        android:hint="Username"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.52"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

     <TextView
        android:id="@+id/pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/inuser"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="208dp"
        android:hint="Password :"
        android:inputType="textPassword"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
         />

     <EditText
        android:id="@+id/inpass"
        android:singleLine="true"
        android:layout_width="359dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pass"
        android:layout_marginTop="235dp"
        android:hint="Password"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.52"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

     <Button
        android:id="@+id/btnlng"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/inpass"
        android:layout_centerHorizontal="true"
        android:hint="Login"
        android:textColorHint="#000000"
        android:onClick="pindah"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.699"
        android:background="@drawable/drawable"/>

 </android.support.constraint.ConstraintLayout>


Create a MainActivity.java Configuration

After you enter the above coding then please open the MainActivity.java file and enter the coding as follows.



import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

 public class MainActivity extends AppCompatActivity {
    private Button btnlng;
    private EditText inuser, inpass;
    //public String username, password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         inuser = (EditText) findViewById(R.id.inuser);
        inpass  = (EditText) findViewById(R.id.inpass);

         btnlng = (Button)findViewById(R.id.btnlng);
        btnlng.setOnClickListener(new View.OnClickListener() {

             @Override
            public void onClick(View view) {

                 if (inuser.getText().toString().equals("erji") && inpass.getText().toString().equals("123")){
                    Toast.makeText(getApplicationContext(),"Anda Login Sebagai : "+ inuser.getText().toString() + " dan Password : "+inpass.getText().toString(),Toast.LENGTH_SHORT).show();
                    Intent i = new Intent(MainActivity.this, HasilLogin.class);
                    i.putExtra("username",inuser.getText().toString());
                    i.putExtra("password",inpass.getText().toString());
                    startActivity(i);
                }
                else {
                    Toast.makeText(getApplicationContext(),"Username dan Pssword tidak sesuai Anda gagal Login",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

 


Create Form Hasil_Login.xml


After all the coding above we enter, then please create a new Activity with the name Hasil Login, how to make it is to right-click on the app folder> New> Activity> Empty Activity then fill in the activity name as above. After that enter the code as below for the xml file.


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".HasilLogin">

     <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Username :"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="106dp"
        android:layout_marginLeft="30dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

     <TextView
        android:id="@+id/txthasiluser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="username"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="106dp"
        android:layout_marginLeft="130dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

     <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password :"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="156dp"
        android:layout_marginLeft="30dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

     <TextView
        android:id="@+id/txthasilpass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="156dp"
        android:layout_marginLeft="130dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

     <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Anda Login Sebagai"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

     <Button
        android:id="@+id/btnExit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Exit"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="206dp"
        android:layout_marginLeft="30dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@drawable/drawable"/>

 </android.support.constraint.ConstraintLayout>

 


Make configuration in HasilLogin.java

After you enter the code above in the Hasil_login.xml file, then we will enter the coding for the java file. For the code, please enter it as below.


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;

 public class HasilLogin extends AppCompatActivity implements View.OnClickListener {

     private TextView txtUsername,txtPassword;
    private Button keluar;

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

         txtUsername = (TextView)findViewById(R.id.txthasiluser);
        txtPassword = (TextView)findViewById(R.id.txthasilpass);

         Intent i = getIntent();
        String txthasiluser = i.getStringExtra("username");
        String txthasilpass = i.getStringExtra("password");

         txtUsername.setText(txthasiluser.toString());
        txtPassword.setText(txthasilpass.toString());

         keluar = (Button) findViewById(R.id.btnExit);
        keluar.setOnClickListener(this);
    }

     public void onClick(View clicked) {
        switch (clicked.getId()) {
            case R.id.btnExit:
                exit();
                break;
        }
        }

     public boolean onKeyDown(int keyCode, KeyEvent event) {
        //jika tombol BACK ditekan
        if(keyCode == KeyEvent.KEYCODE_BACK){
            exit();
        }
        return super.onKeyDown(keyCode,event );
    }
    private void exit() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setMessage("Apakah Kamu Benar-Benar ingin keluar?")
                .setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("Ya", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }
                });
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}


Tadaaa !!! The process of creating a Login Form in Android Studio has been completed, then we will do the testing, to run it you can use an emulator or even through your Android phone using a debugger. After you run it, the application will run with tamplan as below.



Belum ada Komentar untuk "How to Create a Login Form in Android Studio"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel