Hello friends today we are learn about how to validate android form.
Create new android project and see below code.
activity_main.xml
Create new android project and see below code.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
>
<FrameLayout
android:id="@+id/FragContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
>
<FrameLayout
android:id="@+id/FragContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</RelativeLayout>
ActivityMain.java
public class MainActivity extends AppCompatActivity implements OnCustomclickListener{
Stack<String> stringStack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stringStack=new Stack<>();
Stack<String> stringStack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stringStack=new Stack<>();
// add first fragment in stack
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment fragment = new LoginFragment();
String tag = fragment.toString();
stringStack.add(tag);
transaction.add(R.id.FragContainer, fragment, tag);
transaction.addToBackStack(tag);
transaction.commit();
}
@Override
public void OnClick(String tag)
{
if (tag.equalsIgnoreCase("login"))
{
LoginFragment loginFragment=new LoginFragment();
beginTransction(loginFragment);
}else if (tag.equalsIgnoreCase("registration"))
{
RegistratinFragment fragment=new RegistratinFragment();
beginTransction(fragment);
}
}
private void beginTransction(Fragment fragment)
{
Bundle args = new Bundle();
String tag = fragment.toString();
fragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(stringStack.peek());
transaction.hide(currentFragment);
transaction.add(R.id.FragContainer, fragment, fragment.toString());
// This is important, we must addToBackStack so we can pull it out later.
transaction.addToBackStack(tag);
stringStack.add(tag);
transaction.commit();
}
@Override
public void onBackPressed() {
// from the stack we can get the latest fragment
Fragment fragment = getSupportFragmentManager().findFragmentByTag(stringStack.peek());
// If its an instance of Fragment1 I don't want to finish my activity, so I launch a Toast instead.
if (fragment instanceof LoginFragment){
Toast.makeText(getApplicationContext(), "YOU ARE AT THE TOP FRAGMENT", Toast.LENGTH_SHORT).show();
}
else{
// Remove the framg
removeFragment();
super.onBackPressed();
}
}
private void removeFragment(){
// remove the current fragment from the stack.
stringStack.pop();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment fragment = getSupportFragmentManager().findFragmentByTag(stringStack.peek());
transaction.show(fragment);
transaction.commit();
}
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment fragment = new LoginFragment();
String tag = fragment.toString();
stringStack.add(tag);
transaction.add(R.id.FragContainer, fragment, tag);
transaction.addToBackStack(tag);
transaction.commit();
}
@Override
public void OnClick(String tag)
{
if (tag.equalsIgnoreCase("login"))
{
LoginFragment loginFragment=new LoginFragment();
beginTransction(loginFragment);
}else if (tag.equalsIgnoreCase("registration"))
{
RegistratinFragment fragment=new RegistratinFragment();
beginTransction(fragment);
}
}
private void beginTransction(Fragment fragment)
{
Bundle args = new Bundle();
String tag = fragment.toString();
fragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(stringStack.peek());
transaction.hide(currentFragment);
transaction.add(R.id.FragContainer, fragment, fragment.toString());
// This is important, we must addToBackStack so we can pull it out later.
transaction.addToBackStack(tag);
stringStack.add(tag);
transaction.commit();
}
@Override
public void onBackPressed() {
// from the stack we can get the latest fragment
Fragment fragment = getSupportFragmentManager().findFragmentByTag(stringStack.peek());
// If its an instance of Fragment1 I don't want to finish my activity, so I launch a Toast instead.
if (fragment instanceof LoginFragment){
Toast.makeText(getApplicationContext(), "YOU ARE AT THE TOP FRAGMENT", Toast.LENGTH_SHORT).show();
}
else{
// Remove the framg
removeFragment();
super.onBackPressed();
}
}
private void removeFragment(){
// remove the current fragment from the stack.
stringStack.pop();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment fragment = getSupportFragmentManager().findFragmentByTag(stringStack.peek());
transaction.show(fragment);
transaction.commit();
}
}
loginfragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.samset.validationloginform.fragments.LoginFragment">
<TextView
android:layout_centerHorizontal="true"
android:padding="10dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="@color/colorAccent"
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
android:paddingLeft="40dp"
android:paddingRight="40dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_email"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_email"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="@string/hint_password" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="40dp"
android:background="@color/colorPrimary"
android:text="@string/btn_login"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/dontaccount"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:gravity="center"
android:textColor="@color/colorAccent"
android:layout_gravity="center_horizontal"
android:text="Don't have account?"
android:textSize="15dp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:padding="10dp"
android:text="By samset"
android:textSize="10dp" />
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.samset.validationloginform.fragments.LoginFragment">
<TextView
android:layout_centerHorizontal="true"
android:padding="10dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="@color/colorAccent"
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
android:paddingLeft="40dp"
android:paddingRight="40dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_email"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_email"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="@string/hint_password" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="40dp"
android:background="@color/colorPrimary"
android:text="@string/btn_login"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/dontaccount"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:gravity="center"
android:textColor="@color/colorAccent"
android:layout_gravity="center_horizontal"
android:text="Don't have account?"
android:textSize="15dp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:padding="10dp"
android:text="By samset"
android:textSize="10dp" />
</RelativeLayout>
LoginFragment.java
public class LoginFragment extends Fragment {
private View view;
Button login;
private TextView dontHave;
private TextInputLayout ti_email,ti_password;
private EditText et_email,et_password;
private OnCustomclickListener listener;
public LoginFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_login, container, false);
dontHave= (TextView) view.findViewById(R.id.dontaccount);
ti_email= (TextInputLayout) view.findViewById(R.id.text_input_email);
ti_password= (TextInputLayout) view.findViewById(R.id.text_input_password);
et_email= (EditText) view.findViewById(R.id.input_email);
et_password= (EditText) view.findViewById(R.id.input_password);
login= (Button) view.findViewById(R.id.btn_login);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
et_email.addTextChangedListener(new MyTextWatcher(et_email));
et_password.addTextChangedListener(new MyTextWatcher(et_password));
dontHave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.OnClick("registration");
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Login();
}
});
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
listener= (OnCustomclickListener) activity;
//mOnFragment1ClickedListener = (OnFragment1ClickedListener) activity;
} catch (ClassCastException e) {
e.printStackTrace();
}
}
private void Login() {
if (!validateEmail()) {
return;
}
if (!validatePassword()) {
return;
}
Toast.makeText(getActivity(), "sucessfully login!", Toast.LENGTH_SHORT).show();
}
private boolean validateEmail() {
String email = et_email.getText().toString().trim();
if (email.isEmpty() || !isValidEmail(email)) {
ti_email.setError(getString(R.string.err_email));
requestFocus(et_email);
return false;
} else {
ti_email.setErrorEnabled(false);
}
return true;
}
private boolean validatePassword() {
if (et_password.getText().toString().trim().isEmpty()) {
ti_password.setError(getString(R.string.err_password));
requestFocus(et_password);
return false;
} else {
ti_password.setErrorEnabled(false);
}
return true;
}
private static boolean isValidEmail(String email) {
return !TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
private void requestFocus(View view) {
if (view.requestFocus()) {
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
private class MyTextWatcher implements TextWatcher {
private View view;
private MyTextWatcher(View view) {
this.view = view;
}
public void beforeTextChanged(CharSequence charSequence, int i0, int i1, int i2) {
}
public void onTextChanged(CharSequence charSequence, int i0, int i1, int i2) {
}
public void afterTextChanged(Editable editable) {
switch (view.getId()) {
case R.id.input_email:
validateEmail();
break;
case R.id.input_password:
validatePassword();
break;
}
}
}
}
private View view;
Button login;
private TextView dontHave;
private TextInputLayout ti_email,ti_password;
private EditText et_email,et_password;
private OnCustomclickListener listener;
public LoginFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_login, container, false);
dontHave= (TextView) view.findViewById(R.id.dontaccount);
ti_email= (TextInputLayout) view.findViewById(R.id.text_input_email);
ti_password= (TextInputLayout) view.findViewById(R.id.text_input_password);
et_email= (EditText) view.findViewById(R.id.input_email);
et_password= (EditText) view.findViewById(R.id.input_password);
login= (Button) view.findViewById(R.id.btn_login);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
et_email.addTextChangedListener(new MyTextWatcher(et_email));
et_password.addTextChangedListener(new MyTextWatcher(et_password));
dontHave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.OnClick("registration");
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Login();
}
});
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
listener= (OnCustomclickListener) activity;
//mOnFragment1ClickedListener = (OnFragment1ClickedListener) activity;
} catch (ClassCastException e) {
e.printStackTrace();
}
}
private void Login() {
if (!validateEmail()) {
return;
}
if (!validatePassword()) {
return;
}
Toast.makeText(getActivity(), "sucessfully login!", Toast.LENGTH_SHORT).show();
}
private boolean validateEmail() {
String email = et_email.getText().toString().trim();
if (email.isEmpty() || !isValidEmail(email)) {
ti_email.setError(getString(R.string.err_email));
requestFocus(et_email);
return false;
} else {
ti_email.setErrorEnabled(false);
}
return true;
}
private boolean validatePassword() {
if (et_password.getText().toString().trim().isEmpty()) {
ti_password.setError(getString(R.string.err_password));
requestFocus(et_password);
return false;
} else {
ti_password.setErrorEnabled(false);
}
return true;
}
private static boolean isValidEmail(String email) {
return !TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
private void requestFocus(View view) {
if (view.requestFocus()) {
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
private class MyTextWatcher implements TextWatcher {
private View view;
private MyTextWatcher(View view) {
this.view = view;
}
public void beforeTextChanged(CharSequence charSequence, int i0, int i1, int i2) {
}
public void onTextChanged(CharSequence charSequence, int i0, int i1, int i2) {
}
public void afterTextChanged(Editable editable) {
switch (view.getId()) {
case R.id.input_email:
validateEmail();
break;
case R.id.input_password:
validatePassword();
break;
}
}
}
}
Thank you.
I hope this blog helps you.
No comments:
Post a Comment