How to cancel text change of EditText after showing a message? - android

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

Related

Fixed-size multi-line EditText?

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"

error message displaying continuously in TextInputLayout

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

Make editText can type but text not change in Android?

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

Android: How to keep Default number always even if user wants to delete it, and how to delete default number when user inputs a value?

I have a simple EditText, which inputs only numbers.
I want to show 0(zero) even if user presses backspace.
Now zero is getting deleted.
Moreover, I need to remove this default zero, when user starts entering values.
How do i achieve it?
You should use android:hint="0" or in more complex way..
You should use TextWatcher and override the below three methods. THer you can get the text in the arguments as CharSequence in the beforeTextChanged and onTextChanged methods and in afterTextChanged you have Editable from where you can get the data in the EditText. Write in the desired function as per your logic and it should work like a charm.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(final CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
It sounds like you want:
<EditText
android:hint="0" />

Android EditText("Textbox") : Auto capitalizing first letter of each word while user typing

I know this question has asked several times but non of the answers worked in my case.
I found some of the same sort of questions and answers in the following links , which didn't work for me at all.
LINK1
LINK2
In my layout file, I have defined my EditText as follows.
<EditText
android:id="#+id/test_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="90dp"
android:inputType="textCapWords"
android:ems="10" >
Also in the Activity class in the onCreate method I have added the following code.
EditText testEditText = (EditText) findViewById(R.id.test_editText);
testEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
If someone have experienced the same issue and solved it would like to hear what I have done wrong in my case. Thanks.
Edits...
Following above explained steps I am unable to make this get it done(Auto capitalizing the first letter of each word while user typing).
According to my code, When user typing it displays first charater in all the words (including first word) in lower case.
android:inputType="textCapWords"
Works for me
I used the same xml code you have in the questions:
<EditText
android:id="#+id/test_editText"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="90dp"
android:ems="10"
android:inputType="textCapWords" >
Just check if you are overriding the inputType attribute in your Activity.
Just try without changing anything in the Activity.
testEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
or android:inputType="textCapSentences"
will only work If your device keyboard Auto Capitalize Setting enabled.
This is what I have done. android:inputType="textCapWords" is not working for me, either.
public static void setCapitalizeTextWatcher(final EditText editText) {
final TextWatcher textWatcher = new TextWatcher() {
int mStart = 0;
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mStart = start + count;
}
#Override
public void afterTextChanged(Editable s) {
//Use WordUtils.capitalizeFully if you only want the first letter of each word to be capitalized
String capitalizedText = WordUtils.capitalize(editText.getText().toString());
if (!capitalizedText.equals(editText.getText().toString())) {
editText.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) {
editText.setSelection(mStart);
editText.removeTextChangedListener(this);
}
});
editText.setText(capitalizedText);
}
}
};
editText.addTextChangedListener(textWatcher);
}
Earlier it used to be android:capitalize="words", which is now deprecated. The recommended alternative is to use android:inputType="textCapWords"
Please note that this will only work if your device keyboard Auto Capitalize Setting enabled.
To do this programatically, use the following method:
setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Have you tried android:inputType="textPersonName"
should you try with android:inputType="textCapSentences" ?
Are you trying Emulator or android device?
this works for me in emulator
android:inputType="textCapWords"
Simply add this code to your EditText android:capitalize="words" it must work.
To capitalize first letter of every word use
android:inputType="textCapWords"
Output:
This Is Simple Text.
To capitalize first letter of every sentence then use
android:inputType="textCapSentences"
Output:
This is first statement. This is second statetment.
To capitalize all letter
android:inputType="textCapCharacters"
Output:
THIS IS SIMPLE TEXT
Note: In output the bold letter used just for formatting answer. Above mentioned properties does not force these capitalization. User can enter small case letter.
TRY this on your layout...
android:inputType="textCapWords|textCapSentences"
works fine on me.. hope it works also on you...

Categories

Resources