Am using Custom Dialog to shows the list of number based on user input,in Edittext box. I added Textwatcher everything is wrking fine until user try to give input more fast its showing one more Dialog with Black stripes with some alpha characters How to rectify this one?
My Screenshot is 1
Custom Dialog code here
this is my code which am using in textwatcher
#Override
public void afterTextChanged(Editable s) {
String str = s.toString();
str_length = str.length();
Log.v("length_before", "" + count + "" + str_length);
if (str_length == count + 1) {
return;
}
if (str_length >= 3) {
return;
}
if (str_length > count) {
count = str.length();
AmountDialog.amount_dialog(TicketIssueActivity.this, str,
amount);
} else if (str_length < count) {
count = str_length - 1;
Log.v("length slese", "" + count + "" + str_length);
}
}
If all you want is number in amount field. you can set:
android:inputType="number"
in the EditText field of your layout xml file.
That's all I got from the question. Please post more details or code if this doesn't solve the problem.
Related
its may be silly but am confused on that this i want to start count up to one and if press comma(,)then i want to count comma only, here how i am try.
String conCount;
conCount = "1";
int countComma = conCount.length() - conCount.replace(",", "").length();
String lenVar;
lenVar = conCount;
convert = String.valueOf(countComma);
if (conCount.length() == 0) {
lenVar = "0";
} else {
textViewConCount.setText(convert);
}
String editTextString = "abc,efg,pqr,xyz";
if (editTextString.contains(",")) {
int countStringsSeperatedByComma = 0;
countStringsSeperatedByComma = editTextString.split(",").length;
System.out.println("Count of strings seperated by comma : " + countStringsSeperatedByComma);
int commaCount = countStringsSeperatedByComma - 1;
System.out.println("Count of commas : " + commaCount);
} else {
System.out.println("Count of characters in editText string : " + editTextString.length());
}
Output for above condition will be :
Count of strings seperated by comma : 4
Count of commas : 3
Suppose if your string is "abcefgpqrxyz" i.e. without comma then it will execute else part and print characters count as 12 in this case
Count of characters in editText string : 12
Your question statement is so ambiguous. Elaborate it completely and explain your end result with example. It's regarding string functions, I can give you the answer about it if I understand it :) :D
thanks for that i got that answer like that.
String varStr = editextContact.getText().toString();
//int VarCount = editextContact.getText().length();
int countStringsSeperatedByComma = varStr.split(",").length;
String convet=String.valueOf(countStringsSeperatedByComma);
textViewConCount.setText(varStr);
if (varStr.length() == 0){
textViewConCount.setText("0");
}else {
textViewConCount.setText(convet);
}
I made a code where user can't enter first space in a string.
User is allowed to enter white space after min 2 characters.
I need to redefine my method so user enters white space once, and only once after the two or more characters. After that it should be prevented. How do I do that?
case UPDATE_NAME:
if (firstName.getText().toString().startsWith(" "))
firstName.setText(firstName.getText().toString().trim());
if (firstName.getText().toString().contains(" "))
firstName.setText(firstName.getText().toString().replace(" ", " "));
int indexOfSpace = firstName.getText().toString().lastIndexOf(" ");
if (indexOfSpace > 0) {
String beforeSpace = firstName.getText().toString().substring(0, indexOfSpace);
String[] splitted = beforeSpace.split(" ");
if (splitted != null && splitted.length > 0) {
if (splitted[splitted.length - 1].length() < 2)
firstName.setText(firstName.getText().toString().trim());
}
}
Use a regex pattern. I made one that should match your requirements.
\S{2}\S*\s\S*\n
Explanation:
\S{2} two non whitespace
\S* n non whitespace
\s a whitespace
\S* n non whitespace
\n newline (i only added that for regexr, you may not need it)
Alternate way:
Iterate over String.charAt(int), return false if there is a whitespace in the first two chars, count all whitespaces, return false if n > 1.
This method should meet your requirements:
private static boolean isValidFirstName(String firstName) {
if (firstName != null && !firstName.startsWith(" ")) {
int numberOfSpaces = firstName.length() - firstName.replace(" ", "").length();
if (firstName.length() < 2 || numberOfSpaces <= 1) {
return true;
}
}
return false;
}
What you need to do is use a TextWatcher
public class CustomWatcher implements TextWatcher {
private String myText;
private int count = 0;
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after){
myText= s;
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
//check if there is a space in the first 2 characters, if so, sets the string to the previous before the space
if(s.length() < 3 && s.contains(" "))
s= myText;
//if the length is higher than 2, and the count is higher than 0 (1 space added already), puts the string back if a space is entered
else if(s.contains(" ") && count > 0)
s= myText;
//If none of the above is verified and you enter a space, increase count so the previous if statement can do its job
else if(s.contains(" "))
count++;
}
}
And then, set it to your EditText
mTargetEditText.addTextChangedListener(new CustomWatcher());
You can control your editText(I assume) with a TextWatcher, you would only need to check inside afterTextChanged() if length is <2 and else if the string contains the char " ".
I had a problem when I type the text I do not want the keyboard changes automatically, but after a space keyboard changes to the original state.
For example, I want to dial numbers that I move into this state the keyboard: But when I need to enter the number followed by a space, the keyboard itself is changed automatically:And it is necessary that the user himself can change the state of the keyboard, if it is necessary to enter characters. I use a mask on the text of Edit Text. With the help of this library set mask: MaskFormatter. an example of a mask: private static final String MASK = "99 AA 999999";
private EditText mInputCertificate;
#Override
public void setViews(View rootView, Bundle savedInstanceState) {
// Some code
mInputCertificate = (EditText) rootView.findViewById(R.id.input_car_certificate);
MaskFormatter maskFormatter = new MaskFormatter(MASK, mInputCertificate);
mInputCertificate.addTextChangedListener(maskFormatter);
}
There are ways to solve this problem?
I made my custom TextWatcher for EditText:
private String getString (String s) {
String newValue = s.replaceAll("\\s", "");
/*if (newValue.length() < 2 || newValue.length() >= 4) {
mEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
} else {
mEditText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
}*/
StringBuilder builder = new StringBuilder();
for (int i =0; i < newValue.length(); i++) {
if (i == 2 || i == 4) {
builder.append(' ');
builder.append(newValue.charAt(i));
} else {
builder.append(newValue.charAt(i));
}
}
return builder.toString();
}
#Override
public void afterTextChanged(Editable s) {
Log.d("EDITTEXT", "getEditable " + s);
String text = getString(s.toString());
mEditText.removeTextChangedListener(this);
mEditText.getText().clear();
mEditText.append(text.toUpperCase());
mEditText.addTextChangedListener(this);
mEditText.setSelection(mEditText.length());
}
This work for me. And I used .append(SomeText) instead .setText(SomeText).
I am adding a backspace button in my calculator app and it is working fine also. The problem is, by default, my calculator is taking digits from decimal part i.e. initially it is 0.00, when I input 1 it becomes 0.01,when I input 2 it becomes 0.12 and so on and so forth. Now, when I press backspace it is deleting 2 and showing 0.01 but if I press 3 instead of showing 0.13 it shows 1.23. How to resolve this?
Code for the backspace button:-
private String addCurrency(String digits) {
String string = ""; // Your currency
enteredNumber = enteredNumber + digits;
// Amount length greater than 2 means we need to add a decimal point
if (enteredNumber.length() > 2) {
String rupee = enteredNumber.substring(0,
enteredNumber.length() - 2); // Pound part
String paise = enteredNumber.substring(enteredNumber.length() - 2); // Pence
// part
if (enteredNumber.contains(".")) {
}
string += rupee + "." + paise;
} else if (enteredNumber.length() == 1) {
string += "0.0" + enteredNumber;
Log.d("TextWatcher", "length 1 " + string);
} else if (enteredNumber.length() == 2) {
string += "0." + enteredNumber;
Log.d("TextWatcher", "length 2 " + string);
}
return string;
}
WHERE:-
enteredNumber is just a String type variable.
If you wish to show your numbers with two decimal points all the time, then why so much trouble? Use this -
public void getMyNumber(String yourInputNumber){
Double result = Integer.ParseInt(yourInputNumber)/100;
private static DecimalFormat REAL_FORMATTER = new DecimalFormat("0.##");
textview1.setText(REAL_FORMATTER.format(result));
}
I have doubt to manage the phone number in android. I want to looks like when user enter the number inside the EditText then formate entering looks like 999 999 9999 and after complete the entering thf format should be looks like (999) 999-9999. How can achieve this thing. I did my coding something but when user click on cross key of keyboad to back the text the it is not working perfect. Any help please!
Code:
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
String phone = edit_profilephone.getText().toString();
if(phone.length() == 3)
{
edit_profilephone.setText(phone + " ");
edit_profilephone.setSelection(4);
}
else if(phone.length() == 7)
{
edit_profilephone.setText(phone + " ");
edit_profilephone.setSelection(8);
}
else if (phone.length() == 12)
{
int maxLength = 14;
InputFilter[] fArray = new InputFilter[1];
fArray[0] = new InputFilter.LengthFilter(maxLength);
edit_profilephone.setFilters(fArray);
StringBuilder buileString = new StringBuilder();
String splitnumber[] = phone.split("\\s+");
for (int i=0; i<splitnumber.length; i++)
{
if (i == 0)
{
buileString.append("(" + splitnumber[i] + ")");
}
else if (i == 1)
{
buileString.append(" " + splitnumber[i]);
}
else
{
buileString.append("-" + splitnumber[i]);
}
}
edit_profilephone.setText(buileString.toString());
}
}
you can also go through with
EditText inputField = (EditText) findViewById(R.id.inputfield);
inputField.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
For more Idea...
You can use PhoneNumberUtils.formatNumber() methods.
Or you can use a PhoneNumberFormattingTextWatcher. I've never used this before, but it seems to do all the work you want to do.
Also don't forget to put the text type in XML document for the edit text, it might help:) Good luck! Like this:
EditText
android:id="#+id/editTextId"
android:inputType="phone"
android:digits="0123456789+"
/>