Count characters inside EditText in android programmatically

How to count and show number of characters typed inside EditText in android.

In this tutorial we are counting the total sum of characters which user have typed into EditText. Because some times app developer requires some type of special functionality in android app. So here is the complete step by step tutorial for Count characters inside EditText in android programmatically.

android-project-download-code-button

How to Count characters inside EditText in android programmatically.

Code for MainActivity.java file.

 package com.countcharactersinsideedittext_android_examples.com;

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

public class MainActivity extends Activity {

 TextView textview;
 EditText edittext;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 textview = (TextView)findViewById(R.id.textView1);
 edittext = (EditText)findViewById(R.id.editText1);
 
 edittext.addTextChangedListener(new TextWatcher() {
 
 @Override
 public void onTextChanged(CharSequence s, int start, int before, int count) {
 // TODO Auto-generated method stub
 
 int length = edittext.length();
 String convert = String.valueOf(length);
 textview.setText(convert);
 
 }
 
 @Override
 public void beforeTextChanged(CharSequence s, int start, int count,
 int after) {
 // TODO Auto-generated method stub
 
 }
 
 @Override
 public void afterTextChanged(Editable s) {
 // TODO Auto-generated method stub
 
 }
 });
 }
}

Code for activity_main.xml layout file.

<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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.countcharactersinsideedittext_android_examples.com.MainActivity" >

 <EditText
 android:id="@+id/editText1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:ems="10"
 android:hint="EditText" >

 </EditText>

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/editText1"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="22dp"
 android:text="Count Display Here"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshot:

Count characters inside EditText in android programmatically

Click here to download Count characters inside EditText in android programmatically project with source code.

Leave a Reply

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