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);
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.
}
Here's an EditText and a Button:
In your onCreate():
Say you submit or use the value that a user types in the
myEditText
using the button. Set an 'OnClickListener' to this button:Change your code TO this:
instead of
value.getText().trim().length()
try usingvalue.getText().length()<3
You can try this code
First of all you set your maxlength in xml file like this
Then in your code you can write like this
I designed if after no focus, so here you can write for min length condition and max length condition