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

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

Related

Android CountryCodePicker set country to null

I use the CountryCodePicker from the CountryCodePickerProject by hbb20.
I get the User's data from the DataBase, and sometimes for whatever reason the User has "" (empty string) saved as their country.
I use this Picker to edit their country, and if the User does not change his country then I get India "IN" as the selectedCCPCountry since the defaultCCPCountry automatically returns India if no country was set.
How can I set the selectedCCPCountry or some other flag to null or "" so that in this case I can know not to change the User's data?
Thanks!
So I created an issue for this on GitHub and got a quick response from the author:
It seems like your primary use case is to select country without phone
number validation, did I get that right? if so, you should try Android
Country Picker (https://github.com/hbb20/AndroidCountryPicker). It has out
of box support for non selection and much more. Please let me know if that
works for you.
Here is my response and my temporary workaround:
As I don't want to undo all the code I have already, I made a temporary workaround.
mCountryChangedByUser is false and turns true only when a country is selected inside the CountryCodePicker.
mCountryCCP.setOnCountryChangeListener(new CountryCodePicker.OnCountryChangeListener() {
#Override
public void onCountrySelected() {
mCountryChangedByUser = true;
mCountryTextTV.setText(mCountryCCP.getSelectedCountryName());
mUser.setCountry(mCountryCCP.getSelectedCountryNameCode());
}
});
If the user did not change his country then just return the original country in my saveUser() function.
if (mCountryChangedByUser) {
userUpdate.setCountry(mCountryCCP.getSelectedCountryNameCode() );
} else {
userUpdate.setCountry(mUser.getCountry());
}
Maybe next time I'll use the AndroidCountryPicker.
Thanks!

Phone number format for EditText

I am using library input-mask-android to format the text as user is typing.
I have an array of phone number masks. By default, the very first mask in the array is selected. The user can choose a different mask using the Spinner. But when the user selects a mask using the Spinner, it does not work as he would like.
Suppose the array of masks contains two masks: ["+12 - ###", "+34 - ###"]. If the user selects the second mask, then the following will appear when typing into the EditText:
Entered 5 will:
+34-512.
That is, after entering it will automatically put the key characters of the previous mask.
My code in Kotlin:
val listener: MaskedTextChangedListener = installOn(
etPhoneNumber!!,
maskT,
affineFormats, AffinityCalculationStrategy.PREFIX,
object : ValueListener {
override fun onTextChanged(
maskFilled: Boolean,
extractedValue: String,
formattedValue: String
) {
logValueListener(maskFilled, extractedValue, formattedValue)
Log.e("CHANGE FORMAT:", affineFormats.toString())
}
}
)
maskT - is the selected mask
affineFormats - dynamic array where the last mask is stored.
Any suggestions would be greatly appreciated.
input-mask-android library author here.
The installOn method won't suit your needs. You've got to manually initialise a MaskedTextChangedListener, then call an addTextChangedListener method on your EditText. On mask select, you've got to make sure the previous listener is removed (by calling a removeTextChangedListener on your EditText).
The installOn method does not consider there could be other TextWatcher instances attached to your EditText. Thus, all of those installed listeners are trying to take over the text, adding their own "+12" and "+34" prefixes simultaneously.

Android AutoCompleteTextView with '#' mentions filtering like twitter and facebook

I have a requirement for implementing an edit text that the user can type in a anything but when they type in a new word that starts with '#' the autocomplete should start showing potential users.
I understand how to use the AutoCompleteTextView function for filtering. But I am not sure how to go about capturing the characters from the last word after the '#' symbol (ignoring any previous words).
Consequently when the user has been selected from the AutoCompleteTextView list, it should replace the the word with '#', eg.
"This is a message for #steve"
when the user clicks on "Steve" from the list, the text should replace to:
"This is a message for Steve"
I also need to obtain the string in a form that I can send off to the server. i.e. from the above example I would need to send off the string:
"This is a message for [username:steve#bloggs.com, id:44]."
I have looked at https://github.com/splitwise/TokenAutoComplete
Which seems great for typing emails in a list, but I am not sure how to cater it for my needs. Bare in mind, I need to support multiple/duplicate mentions:
eg
"This is a message for Steve and Bob. this is the second sentence in the message for Bob"
If anyone knows or has done anything like this, would really appreciate it!
I ended up using the spyglass library from linkedin which does exactly what I was looking for. It provides a MentionsEditText (that can be customised). I also used a ListPopupWindow to show the suggestions in a list (like an AutoCompleteTextView).
Here is the link...
https://github.com/linkedin/Spyglass
The following method will extract words starting with "#":
private void parseText(String text) {
String[] words = text.split("[ \\.]");
for (int i = 0; i < words.length; i++) {
if (words[i].length() > 0
&& words[i].charAt(0) == '#') {
System.out.println(words[i]);
}
}
}
Once you have the words, use your auto complete filter and finally replace text using String.replace.

Registered user check for lowercase or uppercase

I want to know how to check the existing users for either upper or lowercase when a new user is signing up. At present for example the followig two users can be registered
tom and Tom, both of these are considered as two separate users, so if this happens I want to sound off to the user that the username already exists. Where in code exactly can I do this?
You can create a textwatcher for the edittext where user enters loginname and when focus changes from the edittext get the string in there using getText and pass this to an api which will check the username with db to check for similarity and will return a status false if similar user exits and you can show a red underline or red hidden badge to side of the edittext on the basis of this result to notify the user that such a username already exists.

Appium on Android - SendKeys to EditText also types default text

I am trying to write some automated tests with Appium for Android for Wordpress Mobile (https://github.com/wordpress-mobile/WordPress-Android).
The first thing I'm trying to do is type the username in the main login screen to be able to login to my Wordpress site, and I have a problem with SendKeys on the "username" field.
Here is how the element is seen in the uiautomatorviewer:
Here is what I have tried so far:
List<WebElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText");
WebElement login = textFieldsList.get(0);
login.sendKeys("username");
And:
driver.findElementsByClassName("android.widget.EditText").get(0).sendKeys("username");
And:
driver.findElement(By.xpath("//android.widget.EditText[#text='Editing. Username or email. ']")).sendKeys("username");
With all 3 versions of trying to send "username" as the username, when I run the test, what is actually typed in the field is: "Editing. Username or email. username"
So, it seems that the placeholder text is also kept, then my username is added.
NOTE that the text that is added when I send the username with appium is not there in the first place (see screenshot), but in the UI tree view, that appears to be the text in the EditText. When Appium is running the test, it is actually writing the "Editing. Username or email" text before adding my own username.
I have also tried, as suggested in one of the answers for a different question here: Appium : Clear a field
the following code, where the sendKeyEvent(67) should clear the field:
List<WebElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText");
WebElement login = textFieldsList.get(0);
login.click();
driver.sendKeyEvent(67);
login.sendKeys("username");
Using .clear() crashes and I have noticed that others suggested avoiding it if possible.
Of course, if I try to do this manually, the placeholder text is not added, I can just add my username in the field by typing it.
I can also use the driver.sendKeyEvent() function and send the characters one my one, but I would like to send the username as a parameter and be able to type any username in the field.
Because the extra text is typed every time I try to type the username, to workaround this I have to first type "username" - in the app, the actual text that is typed is "Editing. Username or email. username" - then move the cursor left in front of the word "username" and start deleting the rest - but this is EXTREMELY slow. Here is the code that does work this way:
String setUsername = "username";
login.click();
login.sendKeys(setUsername);
// hack to delete extra text that gets typed
int stringLength = login.getText().length() - setUsername.length();
for (int i = 0; i < setUsername.length(); i++) {
driver.sendKeyEvent(21); //KEYCODE_DPAD_LEFT
}
for (int i = 0; i < stringLength; i++) {
driver.sendKeyEvent(67); // "KEYCODE_DEL
}
What am I missing? Any help would be greatly appreciated. I am trying to understand why the extra text gets typed.
After a lot of searching, I found that this is really a bug in Appium v 1.2.2:
https://discuss.appium.io/t/android-appium-1-2-2-sendkeys-issue-with-hinted-edit-text/309
Hopefully, as it is said there, it will be fixed in version 1.2.3.
Thanks for all your help!
I see that you have used a List and there are 3 text fields on your screenshot - why not use an index of those to pass your value? This way it won't add extra text to it.
List<WebElement> tfl=driver.findElements(By.className("android.widget.EditText"));
tfl.get(0).sendKeys("username"); // tfl=text field list
tfl.get(1).sendKeys("password");
Problem is you are using sendKeyEvent(67) only one time and that too at no proper place. What you have to do is first set your cursor at the end of the Text in Login field and then keep deleting each alphabet one by one until no more text is left in the field.
Eg: Try something like this
WebElement login = textFieldsList.get(0);// Get the webelement where text is to be entered
string str = login.Text;// it will store the Text already written in login field
int textLength = str.Length;// find the lenght of the text that is to be removed
int x = login.Location.X +(textLength * 40); //
int y = login.Location.Y;//
TouchAction action = new TouchAction(driver);// TouchAction class is defined in appium library
action.Tap(testObject, x, y);//
for (int delete = 0; delete < str.Length; delete++)
{
rm.KeyEvent(67);
}
}
Hope it helps..
You're looking for
MobileElement textfield = driver.findElement(By.however(value))
textfield.setValue('foo')
I haven't tested that function on Android, but it was built because textfield.sendKeys is unreliable on iOS/Instruments.
You could also try textfield.click(), textfield.clear(), and then textfield.sendKeys('foo')
Example usage
You'll need the Appium java-client
You might want to downgrade one version as I think this bug doesn't exist in the last version.

Categories

Resources