This code is in a layout file.
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textInputLayoutMobile"
>
<EditText
android:inputType="number"
android:id="#+id/mobileNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="10"
android:hint="Mobile Number" />
</android.support.design.widget.TextInputLayout>
This is Java code.
TextInputLayout textInputLayoutMobile = (TextInputLayout)findViewById(R.id.textInputLayoutMobile);
textInputLayoutMobile.setErrorEnabled(true);
textInputLayoutMobile.setError("This field is required");
Current behaviour: When we click on the field, it shows, "This field is required". Also when we start typing in the field, this message does not go away.
Desired behaviour: When we click for the first time, it should not show, "This field is required". When we move to another field after touching this field and without entering any data, it should show, "This field is required". Also when we start typing in the field, this message should go away.
I think you must use text change listener
edit.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//set error
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
and on his method to set your desire output.. hope to help you!
I believe the error is that those two lines of code are used while retrieving the textInputLayoutMobile from your layout (probably in onCreate). So the view is set to always display this error message.
textInputLayoutMobile.setErrorEnabled(true);
textInputLayoutMobile.setError("This field is required");
I suppose those two lines should be moved from this point and set there where you should validate the textInputLayoutMobile input and deciding that the validation is wrong, e.g. in an onClick callback or listener. Additionally when your input is validated to true then set textInputLayoutMobile.setErrorEnabled(false);
See an example here: http://code.tutsplus.com/tutorials/creating-a-login-screen-using-textinputlayout--cms-24168
I think you need take id="#+id/mobileNumber", set the widget and than try something like that
your_widget.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
if (yourwidget.getText().toString().length() == 0) {
yourwidget.setError("This field is required");}
else etc...
You could use this control on a "next" button or something like that (save button etc) insted of use that on the field directly (sorry for my english)
I think you need to take the reference of EditText and not the TextInputLayout.
Try
EditText editText = (EditText)findViewById(R.id.mobileNumber);
editText.setError("This field is required");
Related
I can disable EditText, but then the user will simply unable to change the text. The behaviour I am trying to implement is that, if the user tries to type something (or any other changing method such as pasting), showing a message like "You cannot edit this text because of something.".
At first, I thought I could show the message using TextWatcher, but there seems to be no way to cancel the change. How can I achieve the behaviour I am looking for? The only way I could think is the following really dirty way.
Have a backup of the text of the EditText. When EditText is changed,
if isReverting is false, show the message and set isReverting to
true. If isReverting is true, just set it to false. Set the backup
to the EditText.
A TextWatcher will fullfill the need in your case . Do the validation inside afterTextChange(). Below is an example.
et_Name.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
String input=s.toString();
if(s.length()>4){
Toast.makeText(MainActivity.this, "You can only input 4 letters",
Toast.LENGTH_SHORT).show();
String old=input.substring(0,4);
et_Name.setText(old);
et_Name.setSelection(old.length());
}
}
});
Your solution will work great. You can also just store the unmodifiable value in a variable and simply change the text back in afterTextChanged if the value is different.
<EditText
android:id="#+id/amount"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:editable="false"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp" />
You can make EditText non editable by using the following property programatically
editText.setEnabled(false);
no need of textChangeListner
I have an EditText that I want to use so people can input a short bio.
So I am trying to make it so it's fixed at, for example, a box that is 4 lines high. I don't want it to "scale" or "shift" with the input -- I'm trying to make it a fixed box of a fixed number of lines with word-wrap.
I did try adding lines="4" and maxLines="4" and inputType="textCapSentences|textMultiLine" but it doesn't quite seem to be right. When I set text, it appears in the middle of the EditText (and not the upper left), and it seems to let me hit enter a whole lot so I can have a word in the first row and then a character like 20 rows down.
Current XML:
<EditText
android:background="#00ff00"
android:id="#+id/editTextId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="4"
android:maxLines="4"
android:inputType="textCapSentences|textMultiLine"
android:gravity="left|top"/>
I'm using a background of green so I can more easily see it for now. Right now this lets you type as much as you want, but I want to limit it to the space as given.
There is in no built code to achieve what you need. But here is a workaround -
private String enteredText;
edtText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (edtText.getLineCount() > 4) {
edtText.setText(enteredText);
edtText.setSelection(edtText.getText().length()); //This statement is to move the cursor at the end of the text otherwise it'll be moved to the start of the text.
}
else {
enteredText = edtText.getText().toString();
}
}
});
Hope this helps !!
to fix the centered text issue add:
android:gravity="top|left"
To prevent the user from inputting more then 4 lines, you'll need to do it by code.
Add a TextWatcher to the EditText, and check the number of lines after each text-change:
TextWatcher textWatcher = new TextWatcher() {
onTextChanged(CharSequence s, int start, int before, int count) {
// Check number of lines here
}
};
editText.addTextChangedListener(textWatcher);
Just add this attribute to your edittext
android:inputType="textMultiLine"
I have a editText:
<EditText
android:textCursorDrawable="#drawable/edittext_cursor"
android:textColorHint="#color/colorBlackHintText"
android:fontFamily="#string/font"
android:inputType="text"
android:id="#+id/idEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:hint="Type something"
android:paddingBottom="#dimen/fab_margin"
android:paddingRight="#dimen/fab_margin"
android:paddingTop="#dimen/fab_margin"
android:textSize="#dimen/text_size" />
When anyone typing, it will call the function:
EditText idEditText = (EditText) findViewById(R.id.idEditText);
idEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
ChooseText(); // here
}
#Override
public void afterTextChanged(Editable s) {
}
});
ChooseText() function will show list then people can choose one of them.
It mean, when anyone try to change editText, the text of editText cannot be changed, the list and will show and this text will change in later, people cannot change this text by themselves.
The most important thing is people see text in editText not change.
Any helps. Thank!
Try to call ChooseText() function on afterTextChanged() method
#Override
public void afterTextChanged(Editable s) {
ChooseText();
}
<EditText
android:inputType="text"
android:id="#+id/idEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something"/>
try this
#Override
public void afterTextChanged(Editable s) {
s.clear();
}
Clicklistener
idEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ChooseText();
}
});
From what you are trying to do it seems more like a Spinner functionality. Where you want user to select from a fixed list of possible texts (values).
http://developer.android.com/guide/topics/ui/controls/spinner.html
Updated:
Checkout AutoCompleteTextView, maybe that matches what you are trying to do.
For the functionality you want, i suppose it would be better to have EditText + Spinner. And a Button to select between the two.
Make Layout such that EditText and Spinner overlay each other. By default have Spinner setVisibility to VISIBLE and EditText to GONE.
So normally user selects from list of items in spinner. When user is in a situation where none of the spinner list items are OK and needs to edit Text, he/she can press a Button you provide.
In OnClick of the button you make Spinner setVisibility GONE and EditText setVisibility VISIBLE. So now user has a EditText to enter Custom Text.
How about AutoCompleteTextView? This is an editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.
Find more examples here
Hi I am learning Android programming and have run into an issue that I couldn't get a clear answer to through researching.
I have a TextView which serves as a label for my EditText. I have a method which checks if the EditText is an empty String. If the string is empty I want to be able to get a reference to the TextView that corresponds to that EditText in order to make a toast saying something like "please enter a value for ".
I've looked into getLabelFor/setLabelFor but is there a way to do this in the layout XML?
What is best practice for this type of functionality.
You're describing a functionally that is build in to EditText. There is a special field you can define in xml called hint, which is the recommended way to label an EditText rather than a nearby TextView. Additionally, EditText has a method called setError() (link). If the user attempts to hit a submit button, for example, you can check to see if the EditText is empty and if so, call setError().
I wonder if the following is the thing that you need
TextWatcher inputTextWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().equals("")) {
textView.setText("please enter a value for ..");
} else {
textView.setText("<the textedit is not empty>");
}
}
public void afterTextChanged(Editable s) {
}
};
editText.addTextChangedListener(inputTextWatcher);
Here is my setup
<EditText
android:id="#+id/inputNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/inputHintText"
android:inputType="textCapCharacters"
android:maxLength="10"
android:textColor="#383838" />
and here is the java code
final EditText inputNo = (EditText)findViewById(R.id.inputNo);
inputNo.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
#Override
public void afterTextChanged(Editable s)
{
if (s.length() > 2)
{
if (!s.toString().startsWith("ABCD"))
{
inputNo.setError( "Must start with ABCD" );
}
}
}
});
The code works fine and shows the red error message in the text box until keystroke 10. But lets say if the user still hits 11th key on the textbox then the error is going away and not staying on the textbox.
One thing I realized that after the 10th input char is entered the afterTextChanged() method is not invoked meaning the addTextChangedListener is not really listening to input keys after the 10th chars because we have defined android:maxLength="10" in EditText.
How to resolve this? My desired result is that if the input do not start with "ABCD" then the error message stays on the edittext field until the user corrects it.
Any pointers/help appreciated!
Try this to limit EditText input:
TextView editEntryVew = new TextView(...);
InputFilter[] filterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(10);
editEntryView.setFilters(filterArray );
Make sure you handle the delete key being pressed while the EditText is empty: https://stackoverflow.com/a/13983097/832776
The problem happens when users input more texts than "maxLength" or delete an empty field. It seems to be an OS bug, I tried many ways and ended up with a workaround as below:
Suppose you have many textfields and a submit button, when users press submit, reset these textfields with their own texts to trigger validation again, such as:
textField1.setText(textField1.getText())
textField2.setText(textField2.getText())
if (TextUtils.isEmpty(textField1.getError()) && TextUtils.isEmpty(textField2.getError()))
// submit
else
Toast.makeText(mContext, "Please correct invalid fields", Toast.LENGTH_SHORT).show();
I hope this helps