ZiZo
ZiZo

Reputation: 61

Specify the number of characters in edit text in android

I have a small trick on how to restrict the number of characters in an edit text in android..for example I want to put a limit say 10 characters for the name field, so it will not allow the user to enter more than 10 characters.

Also, I have another problem in specifying the inputtype for text, I know the procedure which is android:inputType="text|textPersonName" but it dosen't work since it allows the user to enter characters, numbers and also special characters.

I will much appreciate your help.

Thanks in advance

Upvotes: 2

Views: 20369

Answers (7)

Navadip Patel
Navadip Patel

Reputation: 260

it simple way in xml:

if u require to set 4 character in edit-text so,use this

android:maxLength="4"

Upvotes: 1

Keshav Gera
Keshav Gera

Reputation: 11244

Only 7, 8 ,9 digits start in Mobile number first character in android Working

Source Code

https://drive.google.com/open?id=0BzBKpZ4nzNzUQkU3S25fcXV4cEE

First Letter Start with 7,8,9 in Edit text in android

package com.keshav.mobilenumberrestriction_1_6_digits;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    EditText et_mobile_feedback;
    String blockCharacterSet = "1234560";

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

        et_mobile_feedback = (EditText) findViewById(R.id.et_mobile_feedback);

        et_mobile_feedback.setFilters(new InputFilter[] { filter });

        et_mobile_feedback.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
                // you can call or do what you want with your EditText here
            }

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

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(et_mobile_feedback.getText().toString().length()==0)
                    blockCharacterSet="1234560";
                else
                    blockCharacterSet="";
                if(et_mobile_feedback.getText().toString().length()==10)
                    blockCharacterSet="1234567890";
            }
        });
    }
    private InputFilter filter = new InputFilter() {

        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

            try {
                if (source != null && blockCharacterSet.contains("" + source.charAt(0))) {
                    return "";
                }
            }catch (StringIndexOutOfBoundsException e){

            }
            return null;
        }
    };
}

    <EditText
        android:id="@+id/et_mobile_feedback"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:editable="true"
        android:fontFamily="sans-serif"
        android:gravity="top"
        android:hint="Mobile"
        android:textColor="@color/colorPrimary"
        android:inputType="number"
        android:maxLength="10"
        android:padding="15dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp"
        android:singleLine="true">

Upvotes: 1

Harish Rana
Harish Rana

Reputation: 484

android:maxLength="10"

This worked for me.

Upvotes: 3

Jayabal
Jayabal

Reputation: 3619

Use following xml attributes to set maximum characters and digits to allow

android:digits
android:maxLength

For ex:

<EditText
android:id="@+id/et_name"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:digits="abcdefghijklmnopqrstuvwxyz ."
android:maxLength="10" />

Here, This allows only lower case alphabets, space and dot (.)

Upate

set filter by java

InputFilter myFilter = new InputFilter() { 
        public CharSequence filter(CharSequence source, int start, int end, 
Spanned dest, int dstart, int dend) { 
                for (int i = start; i < end; i++) { 


                        if (!Character.isLetter(source.charAt(i))) { 
                                return ""; 
                        } 

                        if (i == start) {
                           return source.toUpperCase();
                        }

                } 
                return source; 
        } 
}; 

edit.setFilters(new InputFilter[]{myFilter}); 

Upvotes: 10

Antrromet
Antrromet

Reputation: 15414

For setting the restriction on the number of characters you should use as already posted

android:maxLength="10"

in the EditText in XML File. And if you want the user to restrict to only alphabets then you will have to create a custom input filter in JAVA code as follows

InputFilter filter = new InputFilter() { 
        public CharSequence filter(CharSequence source, int start, int end, 
Spanned dest, int dstart, int dend) { 
                for (int i = start; i < end; i++) { 
                        if (!Character.isLetter(source.charAt(i))) { 
                                return ""; 
                        } 
                } 
                return null; 
        } 
}; 

edit.setFilters(new InputFilter[]{filter}); 

Code taken from here.

Upvotes: 1

sokie
sokie

Reputation: 1986

For the first part what you have to do is write:

android:maxLength="10"

as far as your second question is concerned writing only textPersonName is sufficient

android:inputType="textPersonName" 

as referenced here input type

You have to use bit flags only when doing it programaticaly,which would correspond to:

 setInputType(InputType.TYPE_CLASS_TEXT  | InputType. TYPE_TEXT_VARIATION_PERSON_NAME);

Upvotes: 1

Chirag
Chirag

Reputation: 56925

Try this:

android:maxLength="10"

in EditText in XML file.

Upvotes: 1

Related Questions