Sunday, September 18, 2016

Android EditText TextWatcher example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#acb4a7"
    android:padding="10dp"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Write something bellow."
        android:background="#585858"
        android:textSize="25sp"
        android:textColor="#fff"
        />
    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/tv"
        app:layout_anchorGravity="bottom"
        android:layout_gravity="bottom"
        android:background="#8c7991"
        android:textSize="25sp"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Random;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private TextView mTV;
    private EditText mEditText;


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

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mTV = (TextView) findViewById(R.id.tv);
        mEditText = (EditText) findViewById(R.id.et);

        // Set a text change listener to the edit text
        /*
            TextWatcher
                When an object of a type is attached to an Editable,
                its methods will be called when the text is changed.
        */
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                // Do something before text changed
            }

            /*
                This method is called to notify you that, within charSequence, the count characters beginning
                at start have just replaced old text that had length before. It is an error to
                attempt to make changes to s from this callback.
            */
            @Override
            public void onTextChanged(
                    CharSequence charSequence,
                    int i, // Start
                    int i1, // Before
                    int i2 // Count
                )
            {
                // Do something on text changed
                mTV.setText(charSequence);
                mTV.setBackgroundColor(getRandomDarkerHSVColor());
            }

            @Override
            public void afterTextChanged(Editable editable) {
                // Do something after text changed
            }
        });
    }

    // Custom method to generate random darker HSV color
    public static int getRandomDarkerHSVColor(){
        Random rand = new Random();
        // Generate a random hue value between 0 to 360
        int hue = rand.nextInt(361);
        // We make the color depth full
        float saturation = 1.0f;
        // We make a full bright color
        float value = 0.8f;
        // We avoid color transparency
        int alpha = 255;
        // Finally, generate the color
        int color = Color.HSVToColor(alpha, new float[]{hue, saturation, value});
        // Return the color
        return color;
    }
}