Skip to content Skip to sidebar Skip to footer

Android: Setting Edittext Max Chars Per Line?

Is there any way to set a maximum of Chars to a line in an EditText Field? I tried it with a TextWatcher, But I cant really do what I want. I would like to set a maximum of lines

Solution 1:

The following filter adds a new line after every n characters:

publicclassAutoWrapFilterimplementsInputFilter {
    privatefinalint mLineChars;

    publicAutoWrapFilter(int pLineChars) {
        mLineChars = pLineChars;
    }

    @Overridepublic CharSequence filter(CharSequence src, int srcStart, int srcEnd, Spanned dest, int destStart, int destEnd) {
        CharSequenceoriginal= dest.subSequence(0,destStart);
        CharSequencereplacement= src.subSequence(srcStart,srcEnd);

        if(replacement.length() < 1){
            returnnull;
        }

        intlastLineCharIndex= -1;

        for (intj= destStart - 1; j >= 0; j--){
            if(original.charAt(j) == '\n'){
                lastLineCharIndex = j;
                break;
            }
        }

        intcharsAfterLine= lastLineCharIndex < 0 ? original.length() : original.length() - lastLineCharIndex;

        StringBuildersb=newStringBuilder();

        for (intk=0; k < replacement.length(); k++){

            if(charsAfterLine == mLineChars+1){
                charsAfterLine = 0;
                sb.append('\n');
            }

            sb.append(replacement.charAt(k));
            charsAfterLine++;

        }


        return sb;
    }

    publicstaticvoidapplyAutoWrap(EditText et, int wrapLength){
        InputFilter[] filters = et.getFilters();
        InputFilter[] newFilters = Arrays.copyOf(filters, filters.length + 1);
        newFilters[filters.length] = newAutoWrapFilter(wrapLength);
        et.setFilters(newFilters);
    }
}

Usage:

publicclassMyActivityextendsActivity{

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        EditTextedit= (EditText) findViewById(R.id.edit);

        AutoWrapFilter.applyAutoWrap(edit,10);

    }

}

Result:

Sceenshot

Also, you can add more code to append new-line char at word beginning, to make it wrap by word.

Solution 2:

Add this in the layout:

android:maxLength="10"// If you want 10 symbols entered in the EditText

EDIT:

android:lines="10"// For the numer of lines in the EditText

Solution 3:

Add this

android:maxLength="5"

Solution 4:

String s="asaaskkjskjkajskjkajskajksjkfldfassasaasssssssssa.";
        int l=s.length();
        String y = "";
       for(int i=0;i<l;i+=20){
        int z=20;
        if(s.length()<z){
            z=s.length();
        }
        y=y+s.substring(0, z)+"\n";
        s=s.substring(z);
    }
    ((EditText)findViewById(R.id.editText1)).setText(y);

Solution 5:

Set these parameters in the layout:

android:layout_width="30dip"android:singleLine="false"android:maxLines="5"

Experiment with the value of android:layout_width to find the value that fits your needs.

Post a Comment for "Android: Setting Edittext Max Chars Per Line?"