14

In my EditText field, I want to give some min text as mandatory and max text as the limit, is there any way to achieve that?

If one is to type text, the numeric count has to decrease. How would I do that?

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="24dp"
    android:maxLength="175"
    android:ems="10" />

this is my adding activity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_layout);
    System.out.println(PRAYER_CATEGORY.length);
    tvPrayer = (TextView) findViewById(R.id.mystate);
    spinnerPrayers = (Spinner) findViewById(R.id.spinnerstate);

    ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, PRAYER_CATEGORY);
    adapter_state
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerPrayers.setAdapter(adapter_state);

    value=(EditText)findViewById(R.id.editText1);
    value
       .setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (value.getText().toString().trim()
                            .length() < 3) {

                        value.setError("Failed");
                    } else {
                        value.setError(null);
                    }
                }
                else {
                    if (value.getText().toString().trim()
                            .length() < 3) {
                        value.setError("Failed");
                    } else {
                            value.setError(null);
                    }
                }
            }
        });
        btnSpeakprayer = (ImageButton) findViewById(R.id.btnSpeakprayer);
        btn=(Button)findViewById(R.id.button1);
        pb=(ProgressBar)findViewById(R.id.progressBar1);
        pb.setVisibility(View.GONE);
        btn.setOnClickListener(this);
Brenton Scott
  • 153
  • 3
  • 13
Karthick M
  • 769
  • 2
  • 11
  • 29
  • use patterns matching – KOTIOS Jul 22 '13 at 06:54
  • @Stacks28,Means not getting ,can you tell how to do – Karthick M Jul 22 '13 at 06:56
  • you want user to enter limited text in EditText like for example four letter ? m i right ?this is what ur question says – KOTIOS Jul 22 '13 at 06:58
  • 1
    @Stacks28 ya you are correct,for me in edit text box atleast one letter is mandatory and max length is 175 character like that i want. – Karthick M Jul 22 '13 at 07:01
  • You can use a OnChangeListener. Check [this post][1] for more details. [1]: http://stackoverflow.com/questions/4310525/android-on-edittext-changed-listener – mach Jul 22 '13 at 07:01
  • You don't need to track the length of input(decrementing or incrementing a variable, for example). You can, but most probably you don't need to. At some point, you'd want to use the value from this `EditText`. Perform validation on the length at that time. – Vikram Jul 22 '13 at 07:01
  • @mach this is my need,for me in edit text box atleast one letter is mandatory and max length is 175 character like that i want. – Karthick M Jul 22 '13 at 07:03
  • @vikram for me in edit text box atleast one letter is mandatory and max length is 175 character like that i want. this what i want dude – Karthick M Jul 22 '13 at 07:10
  • This is fairly straightforward. I'll post an answer. – Vikram Jul 22 '13 at 07:14
  • I updated it try the code. Here you are not dev any code in your else condition – sarabu Jul 22 '13 at 07:55

5 Answers5

11

You can try this code

First of all you set your maxlength in xml file like this

                    <EditText
                    android:id="@+id/editText"
                    android:layout_width="match_parent"
                    android:inputType="textPassword"
                    android:lines="1"
                    android:maxLength="15"
                    android:maxLines="1"
                    android:singleLine="true" />

Then in your code you can write like this

et_billamt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                if (et_billamt.getText().toString().trim().length() < 5) {
                    et_billamt.setError("Failed");
                } else {
                    // your code here
                    et_billamt.setError(null);
                }
            } else {
                if (et_billamt.getText().toString().trim().length() < 5) {
                    et_billamt.setError("Failed");
                } else {
                    // your code here
                    et_billamt.setError(null);
                }
            }

        }
    });

I designed if after no focus, so here you can write for min length condition and max length condition

sarabu
  • 503
  • 1
  • 8
  • 20
  • dude for min length,where i have to give here,For max length this is code android:maxLength="15" then for min length where it is – Karthick M Jul 22 '13 at 07:18
  • in your code inside this `et_first.getText().toString().trim().length() < 5` then you can write your condition here – sarabu Jul 22 '13 at 07:23
  • ValidationMorE cannot be resolved like this error showing dude,in this line if (!ValidationMorE.isValidEorM(et_first .getText().toString())) { – Karthick M Jul 22 '13 at 07:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33885/discussion-between-kartheek-and-karthick-m) – sarabu Jul 22 '13 at 07:33
9

Change your code TO this:

instead of value.getText().trim().length() try using value.getText().length()<3

value.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            if (value.getText().toString().trim().length() < 3)
                value.setError("Failed");
            else
                value.setError(null);
        }
    }
});
fihdi
  • 145
  • 1
  • 1
  • 12
jayeshkv
  • 2,180
  • 4
  • 24
  • 42
  • @Milad in that case you could check out what i have posted here ? i believe that pastie was more focused on version of the answer. – jayeshkv Aug 19 '16 at 16:57
  • yea i know that just want to notice that the link is not useful and i can be removed from the answer – Milad Faridnia Aug 20 '16 at 14:14
1
Try this
EditText value =(EditText) findviewbyId(R.id.urEditTextId);// this line must be oncreate

// place these line where u want to check

String ed1=value .getText().toString();
int size=ed1.length();

you can match the digit and perform appropriate action

if(size==0)
//Toast : kindly enter atleast one letter 

if(size>175)
//Toast : max length 175 char 
KOTIOS
  • 11,177
  • 3
  • 39
  • 66
1

you can extend the EditText class, and override the onTextChanged method to monitor the text length change by yourself. Then you can control the limitation.

public class CustomEditText extends EditText{

public CustomEditText(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

@Override
protected void onTextChanged(CharSequence text, int start,
        int lengthBefore, int lengthAfter) {
    // TODO Auto-generated method stub
    Log.i("length", "input text length = " + text.length());
    super.onTextChanged(text, start, lengthBefore, lengthAfter);
}

}

yushulx
  • 11,695
  • 8
  • 37
  • 64
0

Here's an EditText and a Button:

EditText myEditText;
Button button;

In your onCreate():

myEditText = (EditText) findViewById(R.id.edittext);
button = (Button) findViewById(R.id.button);

Say you submit or use the value that a user types in the myEditText using the button. Set an 'OnClickListener' to this button:

button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        if (mEditText.length() < 1 ||
            mEditText.length() > 175) {

               mEditText.setError("Value should be between 1 and 175 characters in length");

               mEditText.requestFocus();
        } else {

            // Value is valid (between 1 and 175 characters long)

               String text = mEditText.getText().toString();

            // submit
        } 
    }
});
Vikram
  • 51,313
  • 11
  • 93
  • 122