Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Min text (Mandatory) and Max text in EditText

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);
like image 935
Karthick M Avatar asked Jul 22 '13 06:07

Karthick M


People also ask

Is there a way to define a min and max value for EditText in android?

Testing the min value is a different story as we cannot be sure if the user has finished typing or not and therefore cannot decide if we should block or not. Then in Activity set the InputFilterMax and the OnFocusChangeListenerMin to EditText note : You can 2 both min and max in onFocusChangeListener .

How do I change the default text in EditText?

You can use EditText. setText(...) to set the current text of an EditText field.

How do you center text in EditText?

Simpler Solution to align text in EditText is to add android:gravity="center" in layout xml. There is no need to add Java code.

How do you make EditText not editable and clickable?

For the above requirement the solution in XML is android:editable="false" but I want to use this in Java. et. setKeyListener(null); It makes the EditText not EDITABLE but at the same time it makes it non clickable as well.


1 Answers

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

like image 56
sarabu Avatar answered Oct 06 '22 00:10

sarabu