I have the following code that was working fine in Android 2.2 to format phone numbers by 555-555-5555, but in 4.x it is formatting them in 555555-555.
inputPhoneNumber = (EditText) findViewById(R.id.inputPhoneNumber);
inputPhoneNumber.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
Any suggestions on how to fix it?
There is another android class specially for formatting phone number PhoneNumberUtils
and some methods you can use:
formatNumber(String source) Breaks the given number down and formats
it according to the rules for the country the number is from.
formatNumber(Editable text, int defaultFormattingType) Formats a
phone number in-place.
Chect it out.
The comment by #learningslowly helped, but I found it to be still incomplete. The full allowed digits string needed for correct & 'normal' format is:
android:digits="0123456789()-+ "
I was previously missing the minus, plus, and space.
Related
I am PhoneNumberUtil to see phone numbers i enter into a text view and then send them a message. However it does not work as my Numbers are Formated with a '+44 ...' whereas PhoneNumberUtils needs numbers to be written like '44...'. Does anyone know how i can fix this?
If I am understanding your issue correctly, it sounds like you want to strip just the numbers from a string to leave an unformatted number for the formatter to work with.
I'm currently trying to implement this:
I've already done the spinner part, though the phone EditText, I'm not so sure about how to make a TextWatcher for it.
I have 2 options:
I use:
phoneNumberET.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
Or, I use
phoneNumberET.addTextChangedListener(new PhoneNumberFormattingTextWatcher("countryCode"));
Both of these have their flaws. In #1, it won't properly format since the country code isn't found in the EditText, but in the Spinner.
In #2 I can only use this in API level 21 and above.
So, is there a solution that can let me format the phone number properly with a country code from the spinner and be available from below API 21?
Earlier platform levels don't have an API for phone number formatting in other regions, you'll need to bundle it with your app. You can use a library such as googlei18n/libphonenumber (or a fork like libphonenumber-android) to provide the data and formatting, then add a TextWatcher for integration with Android.
My project isn't showing any portuguese characters. When I try to type a word like "Não" it returns Não".
The funny thing is that when I get the string from res/string.xml, it shows the word correctly.
Any idea why?
Things I've tried so far and did not work out:
File -> Settings -> Editor -> File Encondings, I've changed everything to UTF-8 and others, rebuild/cleaned the project, and it kept the same.
EDIT:
I can upload a video on youtube showing it, if it helps with the solution!
There goes an image of what is happening:
My file build.gradle had this line:
compileOptions.encoding = 'ISO-8859-1'
Because of that I wasn't able to change anything. Now it's fixed. :)
This is really hard to explain why is it is, I had same with russian characters, but only on SOME devices. I've just checked to do same as you on Genymotion and it displays correctly... From my investigation it is up to each device how to display given characters, but also I assume it could happen because Android knows how to works with Resources, but doesn't with Strings from code. When you create folders for different languages you don't say that default must be English. So system gonna detect and display. I'm not sure 100%, but this is what I understood from doc.
Anyways, for using String object in TextView from code and displaying foreign (from English) languages we have just 2 options:
1) Add .ttf file for particular text/Unicode
2) html format
Example for first option:
String s="(Mouy t'ngai) (១ ថ្ងៃ)";
TextView text_view1 = null;
text_view1 = (TextView) findViewById(R.id.textView2);
Typeface font= Typeface.createFromAsset(getAssets(), "khmerOS.ttf");
text_view1.setTypeface(font);
text_view1.setText(s);
// you can use different type of .ttf like
TAU_BHON.TTF
molten.ttf
arialuni.ttf
Example of Second option:
tv.setText(Html.fromHtml("\\u27A1");
Source.
P.S. If I missed something, please fill free to notice that.
I am making an app which shows an Indian Language (Punjabi). Is it possible if it shows exactly the same text on all the Android devices if my TextView is something like this: "ੴ ਸਤਿ ਨਾਮੁ ਕਰਤਾ ਪੁਰਖੁ ਨਿਰਭਉ ਨਿਰਵੈਰੁ ਅਕਾਲ ਮੂਰਤਿ ਅਜੂਨੀ ਸੈਭੰ ਗੁਰ ਪ੍ਰਸਾਦਿ " If not what's the best way to approach this?
If the file is encoded using UTF-8, you should be fine. But, don't forget that Android devices support multiple locales, so providing a translation is a good idea (unless you limit distribution to that locality).
I've got a regex that was working perfectly fine until I switched my locale to 'fa' (Persian). I suspect this would happen with Hebrew and Arabic too (not yet sure if it's the characters or the RTL direction that makes it break).
The line of code causing the exception is:
public static final Pattern NAME_REGEX = Pattern.compile(String.format("^[\\w ]{%d,%d}$", 2,24));
(the syntax is fine, it works in English & Spanish) but when the app tries to compile the regex in the 'incompatible' locales, I get the following:
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.util.regex.PatternSyntaxException: Syntax error U_REGEX_BAD_INTERVAL near index 8:
^[\w ]{٢,٢٤}$
^
at java.util.regex.Pattern.compileImpl(Native Method)
at java.util.regex.Pattern.compile(Pattern.java:400)
at java.util.regex.Pattern.<init>(Pattern.java:383)
at java.util.regex.Pattern.compile(Pattern.java:374)
at com.airg.hookt.config.airGConstant.<clinit>(airGConstant.java:131)
Any help would be appreciated.
Thanks
Looks like you're trying to specify the interval using Arabic-Indic digits (U+0660..U+0669); I would have been very surprised if that had worked. I've never heard of a regex flavor that accepts anything but ASCII digits as part of the regex itself.
Are you also expecting \w to match letters/digits from Persian, Hebrew, and Arabic scripts? That won't work either, but this time it's because of a shortcoming in Java's regex flavor. If you want to match characters from any writing system, you need to use Unicode properties like \p{L} and \p{N} (but see here for more details).
ANSWER
So ... the problem was indeed the String.format
Changing
public static final Pattern NAME_REGEX = Pattern.compile(String.format("^[\\w ]{%d,%d}$", 2,24));
to
public static final Pattern NAME_REGEX = Pattern.compile("^[\\w ]{" + 2 + "," + 24 + "}$");
fixed the crash. Thanks to everyone for their contribution.