How to know text lengh in EditView - android

How do i know the text lengh when i typing on the EditText.
this is my codes below.
I tried getting the lengh of EditText by using getText
but i connot control it to attch the other text.
EditText et = (EditText) findViewById(R.id.edit);
I uesd
OnKeyListener
in my code but It's not clear for me.
so I'm looking for other solution ~ to solve it.
What should i do for this?

If you don't want to use OnKeyListener, you can use TextWacher that is listener for EditText while you typing on the EditText.
If you typing on it even 1 letter, afterTextChanged will be called. so you can know how length it is by using s.getText.length().
et1.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.getText.length() > 5){
// do something you want
}
}
});

et.length() give you the length

Related

Disable the dictionary suggestion, backspace and delete word or sentence in EditText

Update: Solved.
I am using EditText as Label display.
I added these lines in XML, but no change.
android:cursorVisible="false"
android:inputType="textMultiLine|textNoSuggestions"
android:textIsSelectable="false"
In code side, added this line:
editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
I tried all of those settings, none of theme helped, also used all of them together still does not work.
What is the appropriate way?
Use this tag in your xml will fix suggestion problem:
android:inputType="textFilter|textMultiLine"
but for backspace or delete problem you can use addTextChangedListener like this:
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) {
// here you can check the changes and revert change or anything you like
}
#Override
public void afterTextChanged(Editable s) {
}
});
for more detail see the official docs of TextWatcher

How to focus on next edittext if there are a lot of them

I have several number of EditText's in my layout if I have to go to next EditText I have to use et1.addTextChangedListener with following code inside it:
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (et1.getText().toString().length() == 1) {
et2.requestFocus();
}
}
I know that very well, but the problem is I have from 17 up to 20 EditText's If I keep writing the code for every EditText my code will be the worst code of for the next 10 decades. How to handle this problem?
My case is very specific: when user presses a key on keyboard he need to focus on next EditText only. No ACTION_UP or ACTION_DOWN.
create a function that gets the first and second edittexts and check
public void goToAnotherEditext(final EditText et1, final EditText et2){
et1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
if(et1.getText().toString().trim().length()==1){
et2.requestFocus();
}
}
});
}
then put the edittexts inside the function
EditText et1 = new EditText(getApplicationContext());
EditText et2 = new EditText(getApplicationContext());
goToAnotherEditext(et1,et2);
In your XML file, what you can do is make your edittext,
android:singleLine="true"
and you can then add imeOptions to change your current focus to next edittext,
android:imeOptions="actionNext"
imeOptions are used to navigate to next action item focusable. to know in deep use other attribute values of imeOptions.
In this way, you don't need to make your hands dirty by performing programmatical part. you can easily do this by XML Part.

Entering Decimal value in Edit text dynamically

I am working in an Android application.In my application there is a edit text. in that
edit text if user enters 12, it has to change dynamically as 12.00. which means it only
accept decimal values. if user enters as 12.3 then it should become 12.30.and if 12.35
then it should be 12.35 only. it will not allow user to enter more than two after dot.
Please help in this scenario?
Use a TextWatcher to handle dinamically the input, then inside the TextWatcher use something like DecimalFormat to change the text.
yourEditText.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) {
// do something here
DecimalFormat form = new DecimalFormat("0.00");
String FormattedText=form.format(s.toString());
}
});

Editext validation android

In Edittext Android :
How to implement this feature if user entered any valid numeric value it should get displayed in the form of decimal value.
e.g If user enters 4 then it should get displayed as 4.000 instead of only 4.
Also can be done using DONE button click handler of virtual keypad.
You can use like this
EditText et = (EditText)findViewById(R.id.myEditText);
et.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){
//Check your et value and set it back.
// Retrieve 4.. set it to 4.000
}
});
Have you tried this on your XML?
android:inputType="numberDecimal"
Parse the string you are getting from editext to double or float in the click of a done button or wherever you want.

How to search a character from a string android

I have 3 widgets on my screen, Text View, Edit Text and a Button. What ever I insert in my edit text, when I click the button, the text view gets the string from the edit text. Now, what I want to do is that, if I have already inserted the character "\" or "," or what ever character I want, it will not be inputted anymore. It's like, you can only put that character once in the edit text. Do you guys have any idea about it?
Well what I am thinking is that, I have to search from the edit view then validate it. But I don't know what code to use. Could somebody please help me? Thank you!
Use Android TextWatcher on EditText.
There are delegates which returns the charsequence that is entered
onTextChanged
afterTextChanged
beforeTextChanged
Fill the entered character in set everytime. If size is not incrementing that means a duplicate.
then avoid adding of that character in edittext
I think you need to add a TextChangedListener to your editText.
et.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) {
}
});

Categories

Resources