Android TextWatcher Registration Form

TextWatcher used for requiered field on the form such as registration form. With textWatcher the button to submit data from field form will be disable until the ruquired form has filled. TextWatcher will keep on watch the EditText widget when there is change. This is the tutorial how to create android textWatcher on registration form.

Create New Android Project

On android studio jast click file menu –> New –> New Project and fill the requiered information.

activity_main.xml

Create form using EditText widget with contain name, password, email.

<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:orientation="vertical"
    android:background="#2196F3">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:text="Please Register"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal"
        android:textColor="#fff"
        android:textStyle="bold"
        android:textSize="16dp"
        android:gravity="center"
        android:layout_marginBottom="10dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/name"
        android:layout_gravity="center_horizontal"
        android:hint="Name"
        android:background="#fff"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:layout_marginBottom="10dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/password"
        android:paddingBottom="5dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp"
        android:paddingLeft="10dp"
        android:hint="Password"
        android:background="#fff"
        android:layout_marginBottom="10dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/email"
        android:layout_marginBottom="10dp"
        android:background="#fff"
        android:hint="Email"
        android:paddingLeft="10dp"
        android:paddingTop="5dp"
        android:paddingRight="10dp"
        android:paddingBottom="5dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SUBMIT"
        android:id="@+id/submit"
        android:layout_gravity="center_horizontal"
        android:background="#009688"
        android:textColor="#fff"
        android:layout_marginTop="10dp" />

</LinearLayout>

Mainactivity.java

package id.rizki.textwatcher;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    EditText name, password, email;
    Button submit;
    public static final String PREFS_NAME = "UserData";
    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3)
        {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            checkFieldsForEmptyValues();
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    };

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

        name = (EditText)findViewById(R.id.name);
        password = (EditText)findViewById(R.id.password);
        email = (EditText)findViewById(R.id.email);

        submit = (Button)findViewById(R.id.submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences submit = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor userdata = submit.edit();
                userdata.putString("name",name.getText().toString());
                userdata.putString("password",password.getText().toString());
                userdata.putString("email", email.getText().toString());

                userdata.commit();

                Intent taketohome = new Intent(MainActivity.this, home.class);
                startActivity(taketohome);
            }
        });

        name.addTextChangedListener(textWatcher);
        password.addTextChangedListener(textWatcher);
        email.addTextChangedListener(textWatcher);
        checkFieldsForEmptyValues();
    }
    private void checkFieldsForEmptyValues(){
        Button s = (Button)findViewById(R.id.submit);

        String n = name.getText().toString();
        String p = password.getText().toString();
        String e = email.getText().toString();

        if (n.length() > 0 && p.length() > 0 && e.length() > 0) {
            s.setEnabled(true);
        } else {
            s.setEnabled(false);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Create second activity to show user data.

activity_home.xml

<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="id.rizki.textwatcher.home"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="20dp"
        android:text="Welcome,"
        android:id="@+id/tvWelcome"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:textSize="12dp" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvName"
        android:layout_gravity="center_horizontal"
        android:textSize="16dp"
        android:textStyle="bold"
        android:hint="Dodiet Prasetyo"
        android:gravity="center" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvEmail"
        android:layout_gravity="center_horizontal"
        android:textSize="16dp"
        android:textStyle="bold"
        android:hint="dodit@gmail.com"
        android:gravity="center" />
</LinearLayout>

home.java

package id.rizki.textwatcher;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class home extends AppCompatActivity {
    public static final String PREFS_NAME = "UserData";
    TextView  uName, uEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        SharedPreferences submit = getSharedPreferences(PREFS_NAME, 0);

        uName = (TextView)findViewById(R.id.tvName);
        uEmail = (TextView)findViewById(R.id.tvEmail);

        uName.setText(submit.getString("name",""));
        uEmail.setText(submit.getString("email",""));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

And this is the AndroidManifest file

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="id.rizki.textwatcher" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:windowSoftInputMode="stateVisible|adjustPan" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".home"
            android:label="@string/title_activity_home" >
        </activity>
    </application>

</manifest>

You can apply this textWatcher for any purpose. So have a nice day

Like
Like Love Haha Wow Sad Angry

Leave a Reply

Your email address will not be published. Required fields are marked *