smiley validation not working in android - android

I have EditText, in that we can enter all keyboard characters like alphanumerics, smileys, special characters.
I have to validate for some special characters and all smileys not to be allowed. Needs to show all the entered non-allowed characters as alert dialog.
I tried with many smileys, for smileys it is taking only empty spaces inside the below function and for validation it is showing only one smiley, sometimes it is showing two.
Below is the code.
public static String validateSpecialCharacters(String message, Pattern messagePattern){
StringBuilder matchedCharacters = new StringBuilder();
Map<Character, Integer> uniqueSpecialCharacters = new HashMap<>();
Matcher m = messagePattern.matcher(message);
while (m.find()){
if(!uniqueSpecialCharacters.containsKey(message.charAt(m.start()))) {
uniqueSpecialCharacters.put(message.charAt(m.start()), 1);
matchedCharacters.append(message.substring(m.start(),m.end()));
}
}
return matchedCharacters.toString();
}
Inside the while the if condition is getting true only once or twice. But I have entered different smileys. The issue is the previous key added for the first smiley is similar with the second. But actually the smiley's are not similar. How to solve this.

Related

EditText force textDirection to ltr doesn't work, can any body advice?

I have an edit text field, which I need to format every word to hashtag
Example if I type ABC it should be formatted to #ABC
I’ve implemented it with input filter but I’m not able to manage it when user enter Arabic text, #abc# مرحبا #مرحبا appears like this, I assume I can solve the problem if I can force the EditText android:textDirection="ltr", but even after this change android EditText behaving rtl when input is Arabic text.
So my question is, how to force an EditText to behave ltr even for rtl text inputs like Arabic?
To make for both direction automatically use below codes
android:layout_gravity="start"
android:textAlignment="viewStart"
Please refer it on the following site for more description Very nice blog by: Sven Bendel or his github page
/**
* Reformat the String as LTR to ensure that it is laid out from
* left to right, but it doesn't affect overall layouting of
* TextViews etc..
*/
public static String makeLtr ( String string ) {
if (checkRtl(string)) {
/* prepend the string with an LTR control sign (so
that Android's RTL check returns false) and an RTL
control sign (so that the string itself is printed in
RTL) and append an LTR control sign (so that if we
append another String it is laid out LTR). */
return "\u200E" + "\u200F" + string + "\u200E";
} else {
return string;
}
}
/**
* Check if the given String is probably written in RTL by
* checking if the very first character is within the range of
* RTL unicode characters.
*/
public static boolean checkRtl ( String string ) {
if (TextUtils.isEmpty(string)) {
return false;
}
char c = string.charAt(0);
return c >= 0x590 && c <= 0x6ff;
}

Android Vertical Text view - display character one below another as show below

I want to display the text in vertical text view (i.e. characters should be placed one below another as below)
a
b
c
d
I wish to do this in a single text view.
We can turn the entire text by using android:toDegrees="-90" or "90" but i want to display as shown above which is not achieved with android:toDegress tag.
Appreciate your help!
UPDATE
I will get the String from another app through AIDL which is subjected to change as per the sender, in addition, he will send me a flag to display the text in a vertical or horizontal direction. Based on the flag I should display accordingly. I have no problem with a horizontal view as it is the default one but to display it in vertical(as shown above) I need help.
If String is Dynamic then you can add \n after each character.
private String build(String str){
if(!TextUtils.isEmpty(str)) {
StringBuilder builder = new StringBuilder();
builder.append(str.charAt(0));
for (int i = 1; i < str.length(); i++) {
builder.append("\n"+str.charAt(i));
}
return builder.toString();
}
return "";
}
textView.setText(build("abcd"));
It will show vertically. This way text can go out of Bound so you should make TextView scrollable in this case.
You can add line break to your string with either \n or <br>
With \n:
<string name="sample_string">a\nb\nc\nd</string>
textView.setText(R.string.sample_string)
With <br>:
<string name="sample_string"><![CDATA[a<br />b<br />c<br />d]]></string>
textView.setText(Html.fromHtml(context.getString(R.string.sample_string))

UnitTesting for EditText , testing multiple values

Hi I am trying to test the edit Text placing 2 different values but the second test is failing .reasons unknown...below is my code
TestCase1
public void testvalues1() {
// clearing the edit text
mTextView.clearComposingText();
TouchUtils.tapView(this, mTextView);
// sending input as 7
sendKeys("7");
String userInput1;
String expected = "158269.3778";
String parameterFrom1 = "0.0027";
String parameterTo1 = "61.04676";
// getting the input from the mTextView reference
userInput1 = mTextView.getText().toString();
String resultset = UnitCalculation.Converter(parameterFrom1,userInput1,parameterTo1);
assertEquals(resultset, expected);
}
In the above test case iam sending value 7 and output is as expected
TestCase2
public void testvalues2() {
// clearing the edit text
mTextView.clearComposingText();
TouchUtils.tapView(this, mTextView);
// sending input as 23
sendKeys("23");
String userInput1;
String expected = "150.5011";
String parameterFrom1 = "1.092607";
String parameterTo1 = "7.149502";
// getting the input from the mTextView reference
userInput1 = mTextView.getText().toString();
String resultset1 = UnitCalculation.Converter(parameterFrom1,userInput1,parameterTo1);
System.out.println("printing resilt set "+ resultset1);
assertEquals(resultset1, expected);
}
But the method is returning value 0 instead of 150.5011
Iam using the same methos to calculate, When i give User value hardcoded like this String userInput1="23"; it is working, but when is taking the value from edittext its is not working.
can i send multiple values to edit text on the same testfile??
sendKeys with more than one character is what messed it up. See this reference.
To sum up, sendKeys needs a String which contains space separated keys. Your sendKeys("23")is trying to find a key in the soft keyboard called 23, though there isn't. Try using this:
sendKeys("2 3");
As it will send these two key strokes individually, instead of trying to find a key named 23. That's why sending just a 7 worked, because 7 is the key name for "7".

Sending USSD code with alphabetic characters

In my android app, I am sending USSD codes (#144#73#) using below Intent :
String baseUssd = Uri.encode("#") + "144" + Uri.encode("#");
StringBuilder builder = new StringBuilder();
builder.append(baseUssd);
builder.append("73");
builder.append(Uri.encode("#"));
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + builder.toString()));
It's working well.
What I want now is to send this code :
#144#73MA#
I run this using the dial pad, following the Operator USSD menu, that worked.
But if I try to do this programmatically using the above Intent that didn't work.
I know that alphabetic characters can't be used when typing code with the Dial Pad, but I though that It can be possible programmatically !!
Any Idea please !
Edit
When I try to send this programmatically : #144#73MA# I noticed that the Dialer application changes the alphabetic characters to their corresponding digit in the dial pad. Meaning that the dialer transform this : #144#73MA#
to this #144#7362# : why ?
Because :
the M matches the digit 6
the A matches the digit 2
Meaning that the dialer transform this : #144#73MA#
to this #144#7362# : why ?
I will try to answer only the why part.
Intent.ACTION_CALL is handled by OutgoingCallBroadcaster class. If you look at processIntent() method, there is this piece of code (lines 438~448 as of this writing):
String number = PhoneNumberUtils.getNumberFromIntent(intent, this);
// Check the number, don't convert for sip uri
// TODO put uriNumber under PhoneNumberUtils
if (number != null) {
if (!PhoneNumberUtils.isUriNumber(number)) {
number = PhoneNumberUtils.convertKeypadLettersToDigits(number);
number = PhoneNumberUtils.stripSeparators(number);
}
} else {
Log.w(TAG, "The number obtained from Intent is null.");
}
There PhoneNumberUtils.convertKeypadLettersToDigits() converts the letters into the equivalent numeric digits:
public static String convertKeypadLettersToDigits (String input)
Translates any alphabetic letters (i.e. [A-Za-z]) in the specified phone number into the equivalent numeric digits, according to the phone keypad letter mapping described in ITU E.161 and ISO/IEC 9995-8.
Returns
the input string, with alpha letters converted to numeric digits using the phone keypad letter mapping. For example, an input of "1-800-GOOG-411" will return "1-800-4664-411".
Hope this helps.

Format Android EditText to specific pattern

I need the user to enter text in an EditText according to this specfic pattern:
123.456-7890-123.456
The user can input any number of integers, so they could as well enter 123.456-7
I do not want the user to enter . or - just the numbers, like an input mask.
Also the numeric keyboard should only show.
I've searched StackOverflow extensively and have seen examples that use InputFilter, ChangedListener, TextWatcher but have not found anything simlar to what I'm trying to do. I've tried in various implementations of what I've found, but I'm inexperienced in using these so I may have overlooked something.
Any suggestions would be welcome.
You're going to have to use a TextWatcher and a regular expression pattern matcher to accomplish what you're trying to do.
This answer should be helpful: Android AutoCompleteTextView with Regular Expression?
You can create your own class that implements InputFilter. Then you would apply it as follows:
MyInputFilter filter = new MyInputFilter(...);
editText.setFilters(new InputFilter[]{filter});
Refer to the docs for how InputFilter is intended to work, then refer to the source code for some of the InputFilters used in Android for some ideas how to implement them.
After many failed attempts to implement InputFilter or Regular Expressions I opted for something a little more straight forward:
public void onTextChanged(CharSequence s, int start, int before, int count) {
String a = "";
String str = id.getText().toString();
String replaced = str.replaceAll(Pattern.quote("."),"");
replaced = replaced.replaceAll(Pattern.quote("-"),"");
char[] id_char = replaced.toCharArray();
int id_len = replaced.length();
for(int i = 0; i < id_len; i++) {
if(i == 2 || i == 12) {
a += id_char[i] + ".";
}
else if (i == 5 || i == 9) {
a += id_char[i] + "-";
}
else a += id_char[i];
}
id.removeTextChangedListener(this);
id.setText(a);
if(before > 0) id.setSelection(start);
else id.setSelection(a.length());
id.addTextChangedListener(this);
}
I don't know if this is the best approach but it does work. One problem I still haven't solved is how to handle cursor placement after the user deletes or inserts a number. If the user inserts the cursor somewhere in the EditText and enters a new number the cursor jumps to the end of the EditText. I would like the cursor to stay where it is at. Another problem if the user inserts the cursor within the EditText number and backspaces to delete a number, then the first key entry doesn't work and on the second key the number is entered. I can only guess this has to do with focus?
Use this: https://github.com/alobov/SimpleMaskWatcher.
Just set your mask for this watcher (###.###-####-###.###). It will add special symbols automatically and wont check your input string for being complete.
But showing the numeric keyboard you must handle by your own using android:inputType="number" tag for your EditText.

Categories

Resources