R.id value requirements - android

I am currently working on an app, and I have noticed that if I assign any element (e.g. Textview) a numeric id value (such as android:id="#+id/1") - I get an error and it will not compile until I add a letter to the id.
My questions are:
1) Why are we not able to use numeric values?
2) Are there any other requirements of R id's?
Just trying to better understand the logic behind this..
I have tried searching with not much luck...
Thanks

taken from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_"
i believe this goes the same for R.id's. meaning they have to start with a letter, "$" or "-".
for your second question , the convention for R.id is that it should be all lower case and no spaces between words just underscore "_".

To my understanding it is just Java convention. For example, you wouldn't call a variable "1" you would call it "one". It is so the compiler can differentiate between numbers and strings. I recommend just labeling the ids based on what it is going to do, makes it easier on you.

Variables can only start with letters or underscores. Other than that, there are no requirements.

Related

Regex with numbers and alphabets mandatory, special characters optional

In my application edittext value need at least one digit and one alphabet is mandatory, and some special characters are optional i.e ".-", like any whare in the string.
For example ram123-. or r_m-12.m or .--ram123 or ram123.-_.
For this I need regex. I have already tried with this one
str_userId.matches("[A-Za-z0-9]*+[?.?_?-]")
But not working. Here how to add special characters are optional.
Thanks, In Advance
You could use a positive lookahead (?= to assert at least one occurrence of a-z and after that match at least a single digit [0-9].
Before and after matching the digit, you could add the . _ and - to the character class [A-Za-z._-]* and repeat it 0+ times.
Note that a character class matches on of the listed characters. This notation [?.?_?-], which could be written as [?._-] would also match a question mark instead of making the others optional
^(?=[^a-z\n]*[a-z])[A-Za-z._-]*[0-9][A-Za-z0-9._-]*$
Regex demo

android Regex issue

I had an issue with this regex:
(\{(([^\p{Space}][^\p{Punct}])+)\})
The problem is in number of chars. If I typing even number of chars it's works, when odd - not. I was trying to replace '+' with '?' or '*', but result still the same. How can I fix this?
I expect from this regex to block such strings: {%,$ #fd}. And allow this:
{F} or {F242fFSf23}.
Currently, it matches a {, then 1 or more repetitions of 2 chars, a non-space and then a non-punctuation, and then a }, hence you cannot use 1 char in between {...}.
To fix that, you need to use both the character classes inside bracket expression:
\{[^\p{Punct}\p{Space}]+\}
or
\{[^\p{P}\p{S}\s]+\}
Details
\{ - a { char
[^\p{Punct}\p{Space}]+ - 1 or more repetitons (+) of any char that does not belong to the \p{Punct} (punctuation) or \p{Space} (whitespace) class.
\} - a }.
Note that if the contents between the braces can only include ASCII letters or digits (in regex, [A-Za-z0-9]+), you may even use a mere
\{[A-Za-z0-9]+\}
Disassembling your regex... the reason why it only accepts an even number in between is the following part:
([^\p{Space}][^\p{Punct}])+
This basically means: something which isn't a space, exactly 1 character and something which isn't a ~punct, exactly 1 character and this several times... so exactly 1 + exactly another 1 are exactly 2 characters... and this several times will always be even.
So what you probably rather want is the following:
[^\p{Space}\p{Punct}]+
for the part shown above... which will result in the following for your complete regex:
\{[^\p{Space}\p{Punct}]+}
that of course can be simplified even more. I leave that up to you.

Combination of rules with Regex

In an android project, im trying to validate a password that the user inputs, and it must follow some rules
The rules are:
it must have 7 characters and 3 of the following conditions
**
-One lowercase character
-One uppercase character
-One number
-One special character
**
for example:
asd123!!!
PPPppp000
TTT999###
i was trying with this regex
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!#$%^&*-]).{7,}+$
but this enforces all rules at same time.
The approach is wrong here. The regex you created looks like a monster from under the bed, and is highly illegible even for someone regex-literate.
Why not split it into 4 (or as much as there are rules) regexes and check against whether 3 of them return a match? Not only will you make your regexes cleaner, but you will be able to add more rules if need be without changing whole regex.
You can also use inbuilt methods for checking (if applicable under Android development kit).
Some pseudocode would look like this:
result1 = Regex.IsMatch(password, rule1regex)
result2 = Regex.IsMatch(password, rule2regex)
...
resultN = Regex.IsMatch(password, rule3regex)
if(three_out_of_four_rules_apply)
password_valid = true
You can also apply method suggested in comments by #pskink and iterate over each character of a password and set the output accordingly.
Without going into the details of your lookaheads (which seem correct), here's how you would need to implement "three out of four criteria" in pure regex :
(?=.*A)(?=.*B)(?=.*C)|(?=.*A)(?=.*B)(?=.*D)|(?=.*A)(?=.*C)(?=.*D)|(?=.*B)(?=.*C)(?=.*D)
You can test it here.
Factorizing doesn't really make it better :
(?=.*A)(?:(?=.*B)(?=.*(?:C|D))|(?=.*C)(?=.*D))|(?=.*B)(?=.*C)(?=.*D)
I obviously recommend using a higher level language to implement these sorts of constraints.

Password regex string in strings.xml file in android

I have declared a regex for password validation purposes in strings.xml file.
The criteria is
-should be atleast 8 characters
-should contain atleast one upper case letter
-should contain atleast one lower case letter
-should contain atleast one special character within these "##$%^+&="
So my whole regex looks like this now
^(?=.[0-9])(?=.[a-z])(?=.[A-Z])(?=.[##$%^+&=])(?=\S+$).{8,}$
But when I enter this, I get an error saying that & is
"Unescaped or non terminated character entity/reference"
So instead I used the escape sequence as & but the validation fails for &
I would b glad if anyone could help me out on this!!
Use * quantifers in the look-aheads. Right now, you check if 2nd character in the string meets your conditions. We need to test them all in the string.
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^+&=])(?=\S+$).{8,}$
Here is a demo.
EDIT
Since the regex is located inside the XML code, it should be properly encoded. Or, use it inside CDATA block.
Are you missing a parameter in your curly braces? The last bit "{8,}" seems off.

Android Replace "..." with ellipsis character

Since AVD tools 16 I'm getting this warning:
Replace "..." with ellipsis character (..., …) ?
in my strings.xml
at this line
<string name="searching">Searching...</string>
How do I replace ...? Is it just literally …?
Could someone explain this encoding?
… is the unicode for "…" so just replace it. It's better to have it as one char/symbol than three dots.
To make thing short just put … in place ...
Link to XML character Entities List
Look at Unicode column of HTML for row named hellip
If you're using Eclipse then you can always do the following:
Right click on the warning
Select "Quick Fix" (shortcut is Ctrl + 1 by default)
Select "Replace with suggested characters"
This should replace your three dots with the proper Unicode character for ellipsis.
Just a note: The latest version of ADT (21.1) sometimes won't do the replace operation properly, but earlier versions had no problem doing this.
This is the character: …
The solution to your problem is:
Go to Window -> Preferences -> Android -> Lint Error Checking
And search for "ellipsis". Change the warning level to "Info" or "Ignore".
This answer is indirectly related to this question:
In my case textView1.setTextView("done&#8230"); was showing some box/chinese character. Later, I checked into fileformat.info for what the value represents and I found this is a Han character.
So, what to do? I searched for "fileformat.info ellipse character" and then everything became clear to me once I saw its values are;
UTF-16 (hex) 0x2026 (2026)
UTF-16 (decimal) 8,230
So, you have several encoding available to represent a character (e.g. 10 in Decimal is represented as A in hexa) so it is very important to know when you are writing an unicode character, how receiving function decodes it. If it decodes as decimal value then you have to provide decimal value, if it accept hexadecimal then you have to provide hexadecimal.
In my case, setTextView() function accepts decimal encoded value but I was providing hexadecimal values so I was getting wrong character.
The quick fix shortcut in Android Studio is Alt + Enter by default.
Best not to ignore it as suggested by some, it seems to me. Use Android Studio to correct it (rather than actually typing in the character code), and the tool will replace the three dots with the three-dot unicode character. Won't be confusing to translators etc.

Categories

Resources