Actually I have A requirement, where I need to take mobile no from edit text, and as the count is equal to 10 then it must call the Intent. So I am using "addTextChangedListener".
et_mobile_no.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(count == 10){
Toast.makeText(getApplicationContext(),
"Count is " + count, Toast.LENGTH_SHORT).show();
}
}
It is working when I am putting characters or string but when I am putting mobile no means int value it is not giving the toast. Please help me how can I get the count while entering mobile no. ????
Try using afterTextChanged in the TextWatcher instead.
Yes it little tricky. I got the answer. Here is answer.
et_mobile_no.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (s.length() == 10) {
if (et_mobile_no.getText().toString().equals("9920263416")) {
Intent i = new Intent(SplashActivity.this,
CustomerLookUp.class);
startActivity(i);
} else {
hided_views.setVisibility(View.VISIBLE);
}
}
}
Related
I am working on Keyboard for android devices where i am Text editing options like Google Keyboard like (Text selection, copy paste etc).
for example i typed a text ABSCEONDER and now i want to selection some portion of text. like i want text selection from position E. What i did is i drooped cursor at position E manually. now how do i find the position of Cursor to select text from that position? can any one help?
ExtractedText extractedText = mLatinIme.getCurrentInputConnection().getExtractedText(new ExtractedTextRequest(), 0);
if (extractedText == null || extractedText.text == null) return;
int index = extractedText.text.length();
mLatinIme.getCurrentInputConnection().setSelection(0, index);
Thank you #mohammadReza Abiri. I found the solution for this.
ExtractedText extractedText = mLatinIme.getCurrentInputConnection().getExtractedText(new ExtractedTextRequest(), 0);
if (extractedText == null || extractedText.text == null) return;
int selectionStart = extractedText.selectionStart;
int selectionEnd = extractedText.selectionEnd;
mLatinIme.getCurrentInputConnection().setSelection(selectionStart, selectionEnd + 1);
you can get the cursor position from your EditText 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) {
}
#Override
public void afterTextChanged(Editable s) {
int pos = editText.getSelectionStart();
Layout layout = editText.getLayout();
float x = layout.getPrimaryHorizontal(pos);
}
});
I'm new to android development and was trying to make a betting game.
In my app there's a field where user is required to enter his bet, now the bet should satisfy the following conditions:
-Minimum value of bet is 5
-Bet should be less than player's current point.
Now don't want the game(app) to proceed further untill the player enters a Valid bet.
How can I achiecve this? I'm struck on this problem since past 2 days.
it is not about simple if statements ?
if(editTextbetamount>5 && editTextbetamount<usersCurrentPoint){
}else{
//toasting something for example
}
Add a textChange listenerto your editText
editTextView.addTextChangeListener(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) {
// Do your logic here
if(Integer.parseInt(s.toString()) >= 5 && playersCurrentPoint >= Integer.parseInt(s.toString){
// proceed
}
}
#Override
public void afterTextChanged(Editable s) {
}
};
I am trying to add only two numbers after a decimal point input in an EditText.
So I implemented a TextWatcher to check the string during input.
The function I am using below works amazing but has one major flaw. When you input any value,then you add a decimal point,delete that decimal point and proceed to add more values,only 3 values are accepted as input.
Case example: I input 300. but then I realize I wanted to input 3001234567, so I delete the decimal point . and proceed to add 1234567 to 300, Only 123 will be accepted and the rest ignored.
How should i handle this? Any suggestions will be appreciated.
My code:
price.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
public void afterTextChanged(Editable arg0) {
if (arg0.length() > 0) {
String str = price.getText().toString();
price.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
count--;
InputFilter[] fArray = new InputFilter[1];
fArray[0] = new InputFilter.LengthFilter(100);
price.setFilters(fArray);
//change the edittext's maximum length to 100.
//If we didn't change this the edittext's maximum length will
//be number of digits we previously entered.
}
return false;
}
});
char t = str.charAt(arg0.length() - 1);
if (t == '.') {
count = 0;
}
if (count >= 0) {
if (count == 2) {
InputFilter[] fArray = new InputFilter[1];
fArray[0] = new InputFilter.LengthFilter(arg0.length());
price.setFilters(fArray);
//prevent the edittext from accessing digits
//by setting maximum length as total number of digits we typed till now.
}
count++;
}
}
}
});
Try this:
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String input = s.toString();
if(input.contains(".") && s.charAt(s.length()-1) != '.'){
if(input.indexOf(".") + 3 <= input.length()-1){
String formatted = input.substring(0, input.indexOf(".") + 3);
editReceiver.setText(formatted);
editReceiver.setSelection(formatted.length());
}
}else if(input.contains(",") && s.charAt(s.length()-1) != ','){
if(input.indexOf(",") + 3 <= input.length()-1){
String formatted = input.substring(0, input.indexOf(",") + 3);
editReceiver.setText(formatted);
editReceiver.setSelection(formatted.length());
}
}
}
Please note, german decimals are , seperated instead of . seperated
You can remove that else part, if it is not needed.
I have a field filled with an EditText control, it figures: 475759403048575663648495004945757590.
What can I use to choose the first 10 numbers from this EditText:
and then, insert the numbers 4757594030, in the TextView?
You can substring the string to 10 characters.
String s = somestring.substring(0,10);
So this will return the first 10 characters, and now you can put the value S in your control!
You can put a textwatcher to the edittext, then count as the edit text is being edited, when you get to the number of characters (10 in this case), you can then display those characters.
Example
TextView textView = ....
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 textVal) {
String inputText = textVal.toString();
if (inputText.length() == 10) {
textView.setText(inputText);
}
}
});
All-
I have a TextWatcher that formats an EditText to currency format:
private String current = "";
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!s.toString().equals(current)){
editText$.removeTextChangedListener(this);
String cleanString = s.toString().replaceAll("[$,.]", "");
double parsed = Double.parseDouble(cleanString);
String formated = NumberFormat.getCurrencyInstance().format((parsed/100));
current = formated;
editText$.setText(formated);
editText$.setSelection(formated.length());
editText$.addTextChangedListener(this);
}
}
This works great, the problem is that my EditText only needs whole numbers so I do not the user to be able to enter cents. So instead of 0.01 than 0.12 than 1.23 than 12.34, I want 1 than 12 than 123 than 1,234. How can I get rid of the decimal point but keep the commas? Thank you.
If you don't mind removing the period and trailing zeroes, you could do this:
mEditText.addTextChangedListener(new TextWatcher() {
private String current = "";
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals(current)) {
annualIncomeEntry.removeTextChangedListener(this);
String cleanString = s.toString().replaceAll("[$,]", "");
if (cleanString.length() > 0) {
double parsed = Double.parseDouble(cleanString);
NumberFormat formatter = NumberFormat.getCurrencyInstance();
formatter.setMaximumFractionDigits(0);
current = formatter.format(parsed);
} else {
current = cleanString;
}
annualIncomeEntry.setText(current);
annualIncomeEntry.setSelection(current.length());
annualIncomeEntry.addTextChangedListener(this);
}
}
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});
This will set the number formatter's maximum fraction digits to zero, removing all trailing zeroes and the period. I also removed the division by 100 so that all entered numbers are integers.
Also make sure that your EditText's inputType is "number" or this will crash if the user tries to enter a non-numeric character.
Hexar's answer was useful but it lacked error detection when the user deleted all the numbers or moved the cursor. I built on to his answer and an answer here to form a complete solution. It may not be best practice due to setting the EditText in the onTextChanged() method but it works.
/* don't allow user to move cursor while entering price */
mEditText.setMovementMethod(null);
mEditText.addTextChangedListener(new TextWatcher() {
private String current = "";
NumberFormat formatter = NumberFormat.getCurrencyInstance();
private double parsed;
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals(current)) {
/* remove listener to prevent stack overflow from infinite loop */
mEditText.removeTextChangedListener(this);
String cleanString = s.toString().replaceAll("[$,]", "");
try {
parsed = Double.parseDouble(cleanString);
}
catch(java.lang.NumberFormatException e) {
parsed = 0;
}
formatter.setMaximumFractionDigits(0);
String formatted = formatter.format(parsed);
current = formatted;
mEditText.setText(formatted);
/* add listener back */
mEditText.addTextChangedListener(this);
/* print a toast when limit is reached... see xml below.
* this is for 6 chars */
if (start == 7) {
Toast toast = Toast.makeText(getApplicationContext(),
"Maximum Limit Reached", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
}
A quick way to ensure the user doesn't enter invalid information is to edit the xml. For my program, a limit of 6 number characters was set.
<!-- it says maxLength 8 but it's really 6 digits and a '$' and a ',' -->
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number|textVisiblePassword"
android:maxLength="8"
android:digits="0123456789"
android:id="#+id/mEditText"
android:hint="Price"/>
Why don't you format the amount using currencyFormat and then take out the .00 from the String.
private static final ThreadLocal<NumberFormat> currencyFormat = new ThreadLocal<NumberFormat>() {
#Override
protected NumberFormat initialValue() {
return NumberFormat.getCurrencyInstance();
}
};
currencyFormat.get().format( < your_amount_here > )
etProductCost.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) {
if (s.toString().length() == 1){
//first number inserted.
if (s.toString().equals(getString(R.string.currency_symbol))){
//if it is a currecy symbol
etProductCost.setText("");
}else {
etProductCost.setText(getString(R.string.currency_symbol) + s.toString());
etProductCost.setSelection(s.toString().length());
}
return;
}
//set cursor position to last in edittext
etProductCost.setSelection(s.toString().length());
}
#Override
public void afterTextChanged(Editable s) {}
});