I'm very new to android and I have an editText in which I wish to set up the current mobile number of that device if presents else leave as such.
I know I can get the current mobile number this way:
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String number = tm.getLine1Number();
But how do I say to android so that it can set the editText field to the current mobile number?
First, find your EditText by its id:
EditText <YourEditText> = (EditText)findViewById(R.id.<YourEditTextID>);
<YourEditText>.setText(number); //your String
Related
This question already has answers here:
Where am I? - Get country
(10 answers)
How can I get my Android device country code without using GPS?
(10 answers)
Closed last month.
The community is reviewing whether to reopen this question as of 11 days ago.
How can I get current region's code like (i.e. US, UK) selected in Android device.
I don't want to get region/country code from locale because it only returns country code from language selected. Using locale:
String cCode = Locale.getDefault().getCountry();
Whenever user explicitly updates region/country, it must reflect in the app. I need code to get current selected region/country of Android device.
I was working on the same. This helped me:
Locale current = getResources().getConfiguration().locale;
To get the country code, you can use the getCountry() method of the Locale class as shown in the code.
To get the full region/country name, you can use the getDisplayCountry() method of the Locale class as shown in the code above. This will return the full name of the country for the device's current locale in the language of the device.
Locale currentLocale = Locale.getDefault();
String countryCode = currentLocale.getCountry();
String regionName = currentLocale.getDisplayCountry();
For example, if the device's current locale is set to United States, the country code will be "US" and the region name will be "United States". If the device's current locale is set to France, the country code will be "FR" and the region name will be "France". So, this will return both the country name and region name.
you can use the getNetworkCountryIso method of the TelephonyManager class to get the country code of the device's current network
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getNetworkCountryIso();
I hope this will work ..
TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String countryCodeValue = tm.getNetworkCountryIso();
Try this:
String countryCode = Locale.getDefault().getCountry();
String languageCode = Locale.getDefault().getLanguage();
I'm trying to get phone number from contact and if it has country code, separate it from mobile number. We've two fields CountryCode and MobileNumber.
There are 4 different scenarios i like to handle
(+CountryCode)(MobileNumber) : I like to get both
(CountryCode)(MobileNumber) : I like to get both
(0)(MobileNumber) : I like to get MobileNumber
(MobileNumber) : I like to get MobileNumber
I've tried using Google libphonenumber, but it's failing in 4th scenario.
For example,In case if my contact is like this 8109112345. I'll get country code as 81 and mobileNumber as 09112345 because Country code of Japan is +81.
This is how i'm using the library.
int countryCode = phoneNumberUtil.extractCountryCode(new StringBuilder(contact), new StringBuilder(""));
String regionCode = phoneNumberUtil.getRegionCodeForCountryCode(countryCode);
final Phonenumber.PhoneNumber phoneNumber = util.parse(contact, regionCode);
Is there any other library which does this or any other way this can be achieved ?
I am allowing user to register with mobile number only and i wanna validate number in app without using internet, what i wanna do is that get valid phone number length of country selected by user , validate it with length and formatting of number in that country's format.
for e.g. length of India's phone number is 10 and format is 0**********
Please tell me how to do that.
Add Following Code.
In gradle
compile 'com.googlecode.libphonenumber:libphonenumber:8.5.2'
In Activity class
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
String isoCode = phoneNumberUtil.getRegionCodeForCountryCode(Integer.parseInt(countryCode));
String exampleNumber= String.valueOf(phoneNumberUtil.getExampleNumber(isoCode).getNationalNumber());
int phoneLength=exampleNumber.length();
editTextLoginPhone.setFilters( new InputFilter[] {
new InputFilter.LengthFilter(phoneLength)});
Hope this code will help you.
if you not want to use the internet of checking the number valid or not , you can do programmatically though its long but solution can be this only .
Go to link : https://en.wikipedia.org/wiki/List_of_mobile_phone_number_series_by_country
and do programmatically the validation with conditions.
You can try this code for Indian Phone numbers:-
private static boolean isValidPhoneNumber(String mobile) {
String regEx = "^[0-9]{10}$";
return mobile.matches(regEx);
}
you should force user to enter the country code and you can validate the phone number also just go through the links below
https://github.com/googlei18n/libphonenumber
and
List of phone number country codes
This question already has an answer here:
Android: Determine active input method from code
(1 answer)
Closed 1 year ago.
I have an android application which handles input from Standard keyboard differently from input given by Swype keyboard. Is there any way to programmatically find out which keyboard is currently being used?
Thanks!
The best way to get that is using the InputDeviceId.
int[] devicesIds = InputDevice.getDeviceIds();
for (int deviceId : devicesIds) {
//Check the device you want
InputDevice device = InputDevice.getDevice(deviceId);
//device.getName must to have virtual
//That check the keyboard type
device.getKeyboardType(); //returns an int
}
Reference:
https://developer.android.com/reference/android/view/InputDevice.html#getKeyboardType%28%29
Use Secure static inner class of Settings to get a default keyboard type.
String defaultKeyboard = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD);
Return type will be a string sequence separated by "/", so split this string wrt to slash to get a list of substrings.
The 1st element of that list will give you the package name of your default keyboard.
defaultKeyboard = defaultKeyboard.split("/")[0];
P.S - Good practice to wrap Settings.Secure calls with try catch.
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....