How to set minimum text (required) and maximum text in EditText

In my EditText field, I want to give some minimal text as required and maximum text with some restrictions that I want to provide in my edittext block, that it is so to give such a value in my EditText value.

If we type, the number of digits should decrease. How to do it.

 <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 add 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); 
+7
java android android-edittext
source share
5 answers

or you can just change your code to this

 value.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { // TODO Auto-generated method stub if (hasFocus) { if (value.getText().toString().trim() **try using value.getText().length()<3** .length() < 3) { **instead of the value.getText().trim().length()** value.setError("Failed"); } else { value.setError(null); }}}}); 
+5
source share

You can try this code

First of all, you set the maximum length in the XML file as follows:

  <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 constructed if after the focus, so here you can write a condition of minimum length and a condition of maximum length

+4
source share
 Try this 
 EditText value =(EditText) findviewbyId(R.id.urEditTextId);// this line must be oncreate 

// put this line where you want to check

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

you can match the number and perform the corresponding action

 if(size==0) //Toast : kindly enter atleast one letter if(size>175) //Toast : max length 175 char 
+1
source share

Here's the EditText and button:

 EditText myEditText; Button button; 

In your onCreate ():

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

Say that you are sending or using the value that the user enters into myEditText using the button. Set "OnClickListener" for 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 } } }); 
0
source share

you can extend the EditText class and override the onTextChanged method to track the change in the length of the text yourself. Then you can control the restriction.

 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); } 

}

0
source share

All Articles