Word and Character Counter in Android

Word and Character Counter in Android  

Difficulty level : Very-Easy  *   Last Updated On : 09th June, 2021


Word and Character Counter is an Android based program that is developed using Java Language.

This project counts the character or words that has been entered in the edit text field by the user.

Below is the picture showing the output of this Project













The Source Code for the above output is as follows: -

To display the imageview on the screen xml code is used.

XML CODE : (file name - activity_main.xml)


<?xml version="1.0" encoding="utf-8"?>

<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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Word and Counter Example"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="@color/black"
        android:layout_marginTop="20dp"
        tools:ignore="HardcodedText" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:paddingTop="15dp"
        android:paddingBottom="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <LinearLayout
                android:id="@+id/LayoutEditor"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp"
                android:background="@drawable/background_search"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:padding="15dp"
                android:paddingStart="10dp"
                android:paddingEnd="10dp"
                android:weightSum="2">

                <EditText
                    android:id="@+id/textEnter"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="7dp"
                    android:layout_marginEnd="7dp"
                    android:layout_weight="2"
                    android:background="@null"
                    android:hint="Enter Your Text Here"
                    android:imeOptions="actionDone"
                    android:importantForAutofill="yes"
                    android:includeFontPadding="true"
                    android:minLines="1"
                    android:inputType="text|textMultiLine"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textSize="13sp"
                    tools:ignore="HardcodedText"
                    android:autofillHints="" />


            </LinearLayout>


            <TextView
                android:id="@+id/characterCounter"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="15dp"
                android:layout_marginEnd="8dp"
                android:background="@drawable/background_search"
                android:importantForAutofill="no"
                android:includeFontPadding="false"
                android:padding="10dp"
                android:text="Character Count :  0\n\n\nWord Count :  0"
                android:textColor="@color/white"
                android:textColorHint="@color/white"
                android:textSize="13sp"
                tools:ignore="HardcodedText"  />
        </LinearLayout>

    </ScrollView>

</LinearLayout>


The colors that are used in the application or on the widget are written in the colors file of the android project.

COLORS FILE CODE :  (file name - colors.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>

    <color name="color1">#EE82EE</color>
    <color name="color2">#FDBE3B</color>
    <color name="color3">#FF4842</color>
    <color name="color4">#3a52fc</color>
    <color name="color5">#000000</color>
    <color name="colorSearchBackground">#333333</color>

</resources>

Java file is used to write the program and all the actions that are performed on the widget while on the screen is done by the Java file.

MAIN ACTIVITY CODE :  (file name - MainActivity.java) 


package com.example.differentandroidcodes;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText enterText;
    TextView characterText;
    int numberText;
    String[] words;

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

        enterText = findViewById(R.id.textEnter);
        characterText = findViewById(R.id.characterCounter);


        enterText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void afterTextChanged(Editable s) {
                if(s == null){
                    characterText.setText("Character Count :  0\\n\\n\\nWord Count :  0");
                }
                else{
                    numberText = s.length();

                    words = s.toString().split("\\s");

                    characterText.setText("Character Count :  "+numberText+"\n\n\nWord Count :  "+words.length);
                }

            }
        });

    }
}


Note : .addTextChangedListener is used to check whether the edit text has been modified or not.


Manifest file is generated automaticallyYou can view this file by clicking on the manifest option that is show the project structure image.

MANIFEST  FILE  CODE:  (file name - AndroidManifest.xml) 

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.DifferentStyleButtons">
        <activity android:name="com.example.differentandroidcodes.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output : - 



Light Theme (Without Text)






Light Theme (With Text and Change in the numbers in the output)





Dark Theme (Without Text)





Dark Theme (With Text and Change in the numbers in the output)



Output in video format : -










Project Structure



Note : The Selected Files are the important files and the code of those files are given in this article.


Get The Source Code on Github


Follow Me on 

     Github

Post a Comment

0 Comments

Close Menu