Showing posts with label e18.1.1. Show all posts
Showing posts with label e18.1.1. Show all posts
TextInputEditText

TextInputEditText

MainActivity.java
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 public class MainActivity extends AppCompatActivity {
  TextInputEditText txtUserName, txtPassword;
  Button btnLogin;
  @Override 
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   txtUserName = (TextInputEditText) findViewById(R.id.idUserName);
   txtPassword = (TextInputEditText) findViewById(R.id.idPassword);
   btnLogin = (Button) findViewById(R.id.idBtnLogin);
   btnLogin.setOnClickListener(new View.OnClickListener() {
    @Override 
     public void onClick(View view) {
     login();
   }
  });
 }
 private void login() {
  
  if (TextUtils.isEmpty(txtUserName.getText().toString().trim()) || TextUtils.isEmpty(txtPassword.getText().toString().trim())) {
   txtUserName.setError("Fields can't be Empty");
   txtPassword.setError("Fields can't be Empty");
   
  } else if (!emailValidator(txtUserName.getText().toString())) {
    txtUserName.setError("Please Enter Valid Email Address");
   
  } else {
    Toast.makeText(getApplicationContext(), "Login Successful", Toast.LENGTH_SHORT).show();
  }
 }
  //Email Validation Using Regex 
  public boolean emailValidator(String email) {
   Pattern pattern;
   Matcher matcher;
   final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
   pattern = Pattern.compile(EMAIL_PATTERN);
   matcher = pattern.matcher(email);
  
  return matcher.matches();
 }
}


activity_main.xml
<?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:orientation="vertical" 
     tools:context="arjun.example.com.materialdesign.MainActivity"> 
    
    <RelativeLayout 
     android:layout_width="match_parent"
     android:layout_height="match_parent" 
     android:layout_weight="3">
    
        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_centerInParent="true" 
         android:gravity="center" 
         android:text="Welcome" 
         android:textColor="#333333" 
         android:textSize="30sp" /> 
    </RelativeLayout> 
    
    <LinearLayout 
     android:layout_width="match_parent"
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical">
    
        <android.support.design.widget.TextInputLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content">
        
            <android.support.design.widget.TextInputEditText 
             android:id="@+id/idUserName"
             android:layout_width="match_parent"
             android:layout_height="wrap_content" 
             android:hint="User Name" 
             android:inputType="text" 
             android:maxLines="1" />
        </android.support.design.widget.TextInputLayout> 
        
        <android.support.design.widget.TextInputLayout 
         android:layout_width="match_parent"
         android:layout_height="wrap_content" 
         app:passwordToggleEnabled="true">
        
            <android.support.design.widget.TextInputEditText
             android:id="@+id/idPassword"
             android:layout_width="match_parent" 
             android:layout_height="wrap_content"
             android:hint="Password"
             android:inputType="textPassword"
             android:maxLines="1" /> 
            
        </android.support.design.widget.TextInputLayout> 
        <Button 
         android:layout_width="match_parent"
         android:layout_height="wrap_content" 
         android:id="@+id/idBtnLogin"
         android:layout_gravity="center" 
         android:text="Login"/> 
    </LinearLayout>  
</LinearLayout>