Monday, November 16, 2015

Words- Characters Counter Android Demo



Description:

It is very easy to handle words and characters counter for your application. If you are developing an app which uses EditText, It is very important to give feature like word counter / Character counter within the app. Here is the full demo code which helps you.

First add below code to your activity_main.xml file.


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="0.4"
    android:orientation="vertical"
    android:paddingLeft="5dip"
    android:paddingRight="5dip" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:paddingBottom="10dp" >

        <TextView
            android:id="@+id/tvCharWatcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerInParent="true"
            android:textSize="22dp"
            android:text="Characters:" />

        <TextView
            android:id="@+id/tvWordWatcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerInParent="true"
            android:textSize="22dp"
            android:text="Words:" />
    </RelativeLayout>

    <EditText
        android:id="@+id/note"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:autoText="false"
        android:background="@android:color/white"
        android:capitalize="none"
        android:ems="10"
        android:fadingEdge="none"
        android:gravity="top"
        android:scrollbars="vertical|horizontal"
        android:textColor="@android:color/black"
        android:textSize="22dp" >

        <requestFocus />
    </EditText>

</LinearLayout>


Your layout is ready, now you need to register components to MainActivity.java file and add below code step by step:

1. Create method init() to initialize needful variables,components etc.
2. TextWatcher class handles your EditText and it will call with every text you add.
3. WordCount() custom method counts words and return number of words.

Here, is the full code of MainActivity.java file

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

EditText etText;
TextView tvWordWatcher, tvCharWatcher;

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

init();
}

private void init() {

etText = (EditText) findViewById(R.id.note);
etText.setVerticalScrollBarEnabled(true);
etText.requestFocus();

tvCharWatcher = (TextView) findViewById(R.id.tvCharWatcher);
tvWordWatcher = (TextView) findViewById(R.id.tvWordWatcher);
tvCharWatcher.setText("Characters:0");
tvWordWatcher.setText("Words:0");

etText.addTextChangedListener(textWatcher);
}

TextWatcher textWatcher = new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
tvCharWatcher.setText("Characters:"
+ String.valueOf(s.length()));
tvWordWatcher.setText("Words:" + wordcount(s.toString()));
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {

}

@Override
public void afterTextChanged(Editable s) {

}
};

public static long wordcount(String line) {
long numWords = 0;
int index = 0;
boolean prevWhiteSpace = true;
while (index < line.length()) {
char c = line.charAt(index++);
boolean currWhiteSpace = Character.isWhitespace(c);
if (prevWhiteSpace && !currWhiteSpace) {
numWords++;
}
prevWhiteSpace = currWhiteSpace;
}
return numWords;
}
}


Download Full Source Code: GitHub


3 comments: