Getting the first 2 digit inputed in EditText - android

I've already been search on how to get the first 2 digit inputed in the EditText but the result was getting the first digit only. In my app I want to make sure that the inputed number is a phone number. I want to be sure that it's started with 09 because it is our countrycode. Is there anyway to do it? Any help will appreciated. Thank you in advance!

Let's say et is your EditText name. You can achieve what you want by using following code.
if (et.getText().toString().startsWith("09")) {
//do what you want
}

Try this
String number = edittext.getText().toString();
String first_two_digits = number.substring(0,2);
first_two_digits are first two numbers of your edittext string

Related

Android, Xamarin, EditTexts: Disable the user from deleting the first letters

I want the user to type in his or her Facebook account-link (don't have a better solution atm).
Now, when the user clicks the edittext it is supposed to say : "www.facebook.com/". Now the cursor is supposed to be at the END of the edittext (after the "/") and the user is not supposed to delete the first letters so that the "www.facebook.com/" stays exactly where it is. This will have the user to ONLY type in his or her facebook name and therefore connect the profile.
Is there a way of doing this?
Thank you :)
You can do that by using the event "TextChanged" and in your code validate if the size is big than your string, like that:
if (((EditText)sender).Text.Length >= 17)
{
((EditText)sender).Text = e.NewTextValue;
}
else
{
((EditText)sender).Text = "www.facebook.com/";
}
So if the value is bigger than your string you will replace that for the value, if not you just set the value with your string

(Appium) Using sendKeys to write a number in an input field which already has a +91 hardcoded as a prefix, does not enter the number correctly

It selects and copies the number which I am sending and sometimes enters it before the +91 which eventually results in an error.
Below is the code I am using to send a number in to the number input field.
WebElement number = driver.findElement(By.id("com.ulink.agrostar.debug:id/edt_enter_mobileNumber"));
number.sendKeys("7976358798");
You have to first check whether your edit field consist of predefined field like
"+91", If it is there you can override text like these.
if(element.getText().trim().equals("+91")) {
number.clear();
}
number.sendKeys("+91" + "7976358798");
It should work please check.

Android Insert mutliple phone numbers in an editText

I managed to create a button that launches the contact book and returns a phone number to an editText, upon selection. How do I insert multiple phone numbers into the editText (a.k.a sending sms to a group of people instead of one)
Its better to separate the numbers with comma(",") , so that get list of numbers as
String number = et.getText().toString();
String[] numArr = number.split(",");
try adding multiple phone numbers inside the edit text seperating with SPACE then when you get the text from edit text. do things below...
String temp = edittext.getText().toString();
String[] tempArr = temp.split(" ");
tempArr will contain all the numbers inputed in edit text....

How to write Toast notification code for a simple math exercise?

I need to configure a button with a code that takes the first two digits of a number (when clicked) (from a textbox) and adds a number to it; and then takes the 3rd and 4th digits and does somewhat the same to it and displays the result value in a toast message. The user inputs the original number to which the additions and subtractions must be done.
So far I have only done: Toast.makeText(TimerCodeActivity.this, String.valueOf(
After that I am just lost
Try something like the following:
EditText theTextBox = (EditText)findViewById(R.id.idOfTheEditText);
int digits = Integer.parseInt(theTextBox.getText().toString().subString(0,2)); // Take the first two digits
int newnumber = digits + 4; // Add a number? Or are you trying to add like a string?
Toast.makeText(TimerCodeActivity.this, String.valueOf(newnumber), Toast.LENGTH_LONG).show();

Android NumberFormatException error with a simple number input from EditText

So I have a simple Edit-text with number input.
User enters his phone number and it is saved into DB obviously that is the goal.
now every time i do
int contactNumber=Integer.parseInt(mContactNumber.getText().toString());
I get an error thrown saying NumberFormatException for some reason it doesn't like a string of number. I have made sure that mContactNumber field in the android input field has the
android:input="number"
now i also tried just to be sure
int contactNumber=Integer.parseInt(mContactNumber.getText().toString().trim());
still the same error
Guys any ideas?
Try putting the Type to ' long ' instead of ' int '. Hope that will work for you.
This might be because you are leaving the text field empty. Are you sure you are not parsing an empty string?
You need to put a simple condition:
if(mContactNumber.getText().length()>0)
{
int contactNumber=Integer.parseInt(mContactNumber.getText().toString().trim());
}
For phone Number you can not take it as Integer. try to take it as string only and after getting that string you can do the check for the numbers only by
TextUtils.isDigitsOnly(str);

Categories

Resources