Saturday, March 30, 2019

Login form in android app | Android Studio | 2019

Login Form


How to make a login form in android.

Let's create a login app in just a few simple steps. Making a login form in the Android app is very simple. We are using two EditText components as username and password. Button as login button.

Follow the steps below:-
Step1:- Create an empty activity project and named it as LoginApp.
Step2:- Open up your activity_main.xml layout file.
Step3:- Copy and paste the below code as given.

<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=".MainActivity">

    <RelativeLayout        
        android:layout_marginTop="150dp"        
        android:padding="10dp"        
        android:layout_width="match_parent"        
        android:layout_height="wrap_content">

        <EditText            
           android:inputType="text"            
           android:textColorHint="@color/gray"            
           android:textColor="@color/white"            
           android:padding="10dp"            
           android:background="@color/colorAccent"            
           android:hint="@string/user"            
           android:id="@+id/user"            
           android:layout_width="match_parent"            
           android:layout_height="wrap_content" />

        <EditText            
           android:inputType="textPassword"            
           android:layout_marginTop="10dp"            
           android:background="@color/colorAccent"            
           android:textColorHint="@color/gray"            
           android:textColor="@color/white"            
           android:padding="10dp"            
           android:layout_below="@id/user"            
           android:hint="@string/user"            
           android:id="@+id/pass"            
           android:layout_width="match_parent"            
           android:layout_height="wrap_content" />

        <Button            
           android:textColor="@color/white"            
           android:background="@color/colorPrimaryDark"            
           android:layout_marginTop="5dp"            
           android:layout_centerInParent="true"            
           android:layout_below="@id/pass"            
           android:onClick="login"            
           android:text="@string/login_btn"            
           android:id="@+id/loginbtn"            
           android:layout_width="wrap_content"            
           android:layout_height="wrap_content" />

    </RelativeLayout>

</LinearLayout>
Step4:- Open your MainActivity.java java file.
Step5:-Copy and paste the below code:-

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

public class MainActivity extends AppCompatActivity {
       EditText user,pass;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        user = findViewById(R.id.user);
        pass = findViewById(R.id.pass);

    }

   public void login(View v){
       String id = user.getText().toString();
       String pwd = pass.getText().toString();
        if (!id.isEmpty() && !pwd.isEmpty()){
            Toast.makeText(getApplicationContext(),"username :"+id+"\n password:"+pwd,Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getApplicationContext(),"enter user and pass.!",Toast.LENGTH_SHORT).show();
        }
   }

}
Step6:-Now run your app.
Without any input text.

With some input text


As above in MainActivity.java file we have created a method as login() and passed an argument View to implement the method in our design layout.

After that, we check both the EditText value is empty or not, for checking empty value in android we got a method isEmpty which will return a boolean value true or false.

If username or password is empty then the empty method will return true else it will return false.
For the else part we have toasted a message as "enter user and pass.!". If we got both text boxes values then we toast username and password as given above in MainActivity.java file.

So, as you can see, it was very simple to make a login app but for more functionality. we will can insert a value in the SQLite database and check the value from our local database also.

Thank you for your support, Subscribe to youtube channel  Android Buddy.

No comments:

Post a Comment