How to show custom validation message for username input? - android

I am trying to validate username input when creating new account.
I have these constrains:
^(?=.{3,14}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$
^(?=.{3,14}$) for username is 3-14 characters long
(?![_.]) username can`t start with . or _
(?!.*[_.]{2}) no __ or . or . or .. inside username
[a-zA-Z0-9._] allowed characters
(?<![_.])$ username can not end with
and other error messages also.
This is my method (Note that i use TextInputEditText object)
private boolean isUsernameValidated() {
String newUsername = et_Signup_username.getText().toString().trim();
if (newUsername.isEmpty()) {
etLayout_Signup_username.setError("Field can't be empty.");
return false;
} else {
etLayout_Signup_username.setError(null);
return true;
}
}
How can I show custom error message like if the username start with _ or . message popup Username cant start with _ or .
or other error message as shown above ?

So, I got your issue. Basically, you are trying to show different error messages for different type of errors .
Instead of writing all the long codes, you can use a simple library known as "AwesomeValidation". And you can simply put the following line of code, to make the library validate everything for you.
mAwesomeValidation.addValidation(activity, R.id.et_Signup_username, "(?![_.])", R.string.err_name);
You can import it by pasting this in your gradle
implementation 'com.basgeekball:awesome-validation:4.2'
For more details about this library, please check this link.

Related

How to wait and grab the text of android.widget.TextView in an appium test when there is no resource_id?

The appium test is to perform certain combination of few input fields, and one field is password field.
If the username is already taken, a corresponding message will be shown below the password button saying "username already taken".
But if username is valid, but password is wrong, the message will be shown below as "username and password combo didn't work".
The error message in above cases is shown in a android.widget.TextView**
Two questions:
I recorded the test using Appium Studio. When I run the test, I want to wait for couple of second and grab the text of error message. How to do the wait part and grab the text? (More details below). THe challenge is there is no resource id for the textView, but xpath and other details are available.
Note: Since the error message is shown right below the pwd field, it has no resouceid, only xpath available.
This is the structure
<android.widget.LinearLayout resource-id="com.a.b/textinputlayout_lgin_username">
<android.widget.FrameLayout>
<android.widget.EditText resource-id="com.a.b/lgin_username_edit">
<!--above 3 lines are for username field, just added for more clarity -->
<android.widget.LinearLayout resource-id="com.a.b:id/textinputlayout_lgin_pwd">
<android.widget.FrameLayout>
<android.widget.EditText>
<android.widget.ImageButton resource-id="com.a.b/text_input_end_icon">
<android.widget.TextView> <!-- ** this is the text I'm trying to grab -->
<android.widget.TextView resource-id="com.a.b/tv_forgotpwd">
<android.widget.Button resource-id="com.a.b/btn_login">
Please help. Thanks.
I assume, you have no ability to update the app in order to add resource id for the element.
The best, you can do in this case, is to use xpath.
Find the first parent by resorce-id attribute, and then go down and search by tag. I see 2 text tag elements here in LinearLayout, so you might try to get the first text tag by index, or search for text tag without resouce id attribute.
"(//android.widget.LinearLayout[#resouce-id='com.a.b:id/textinputlayout_lgin_pwd']//android.widget.TextView)[1]"
or
"//android.widget.LinearLayout[#resouce-id='com.a.b:id/textinputlayout_lgin_pwd']//android.widget.TextView[not(#resouce-id)]"
Based on appium client you're using, just utilize webdriver wait + expected conditions and use xpath location strategy for the element.
PS: Here is the code
public class Test_9 {
private String reportDirectory = "reports";
private String reportFormat = "xml";
private String testName = "Untitled";
protected AndroidDriver<AndroidElement> driver = null;
DesiredCapabilities dc = new DesiredCapabilities();
#BeforeEach
public void setUp() throws MalformedURLException {
dc.setCapability("reportDirectory", reportDirectory);
dc.setCapability("reportFormat", reportFormat);
dc.setCapability("testName", testName);
dc.setCapability(MobileCapabilityType.UDID, "123456");
dc.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.a.b");
dc.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, ".view.base.SplashActivity");
driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), dc);
driver.setLogLevel(Level.INFO);
}
#Test
public void testUntitled() {
driver.findElement(By.xpath("(//*[#contentDescription='Google Map']/*[#class='android.view.View'])[26]")).click();
driver.findElement(By.xpath("//*[#text='log in']")).click();
driver.findElement(By.xpath("//*[#id='lgin_edit']")).sendKeys("1234567789");
driver.findElement(By.xpath("//*[#class='android.widget.EditText' and (./preceding-sibling::* | ./following-sibling::*)[#id='text_input_end_icon']]")).sendKeys("qwer");
new WebDriverWait(driver, 120).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#class='android.widget.EditText' and (./preceding-sibling::* | ./following-sibling::*)[#id='text_input_end_icon']]")));
driver.hideKeyboard();
driver.findElement(By.xpath("//*[#class='android.widget.EditText' and (./preceding-sibling::* | ./following-sibling::*)[#id='text_input_end_icon']]")).click();
driver.findElement(By.xpath("//*[#text='Log In']")).click();
driver.findElement(By.xpath("//*[#text='Log In']")).click();
}
#AfterEach
public void tearDown() {
//driver.quit();
}
}
Ok, this is what resolved my issue.
After posting the questino I realized, the button wasn't enabled, that's because the previous input text field is not clicked or something.
All this time I was recording via Appium, but I was manually clicking the keyboard and buttons on mobile phone, not the mobile simulator of appium.
After 2.5 days, I used mouse pointer of my computer to click all the fields and all simulation on the simulator of appium and recorded and re-ran the tests and it worked.
I did try printing out the button state and it was always btn.isEnabled() was returning false.

XMPP jid-malformed(400) error Android while creating user

I am trying to develop XMPP chat in Android and while creating new user using AccountManager I am having the following exception :
jid-malformed(400)
My user-connection code goes like this:
AccountManager manager = connection.getAccountManager();
try {
manager.createAccount(username, password);
}
catch(XMPPException e){
e..printStackTrace();
}
here my
username = abc#xyz.com
password = 12345678
I learned that we need not require to send service name with the username from post
But in my username the format says that my user is "abc" and my service is "xyz.com"
what should I do to keep '#' in my username?
Thank You. :)
JID escaping is done as per XEP-0106. Specifically, the "#" character should be replaced by "\40" to keep the "#" as part of the JID.

Place Autocomplete for autocomplete textview

I'm trying to give auto suggestion for places when user typing in auto complete text view in android. Right now i'm using the new Google play services libraries and sample code from the following link Place Auto complete . But When trying to give suggestion for the user typed text, I'm getting the following error
Error getting autocomplete prediction API call:
Status { statusCode = NETWORK_ERROR, resolution = null }
Any idea what may be the problem?
I have found this: "public static final int NETWORK_ERROR
A network error occurred. Retrying should resolve the problem.
Constant Value: 7 (0x00000007) " see here.

Login in Facebook SDK for Unity3d in android

I need help using Facebook SDK for Unity3d in android. I was trying to do all the tutorial said, but I can't login succesfully. I do FB.Init and its ok, but FB.Login never works. Always return this:
{
"is_logged_in":false,
"user_id":"",
"access_token:"",
"access_token_expires_at":"01/01/0001 00:00:00"
}
please, i need fix it as soon as possible.
Thanks for all.
i had the same problem this fixed it for me i was using the wrong keyhash in facebook, maybe you have the same problem
open the AndroidFacebook script and scroll till you see this:
public void OnLoginComplete(string message)
{
var parameters = (Dictionary<string, object>)MiniJSON.Json.Deserialize(message);
if (parameters.ContainsKey("user_id"))
{
isLoggedIn = true;
userId = (string)parameters["user_id"];
accessToken = (string)parameters["access_token"];
accessTokenExpiresAt = FromTimestamp(int.Parse((string)parameters["expiration_timestamp"]));
}
if (parameters.ContainsKey("key_hash"))
{
keyHash = (string)parameters["key_hash"];
print (keyHash);
}
OnAuthResponse(new FBResult(message));
}
as you can see inside where it pertains to keyhash i put
print (keyHash);
so now if you run it and log in and use logcat on eclipse or a logger to see the print() that come out of the code check the keyhash you have in your facebook app and the one that is printed out with print (keyHash); witch is the correct one, hope this helps :)

cannot implement rest webservice in android app

I want to implement rest webservice in android application
to do that
I create the class from this tutorial: here
but when I clcok on the matched button I have the error
unfortunetly, android app has stopped
I don't have anything in the Logcat
then I canno't know from which is the problem
here is the event code:
case R.id.btn_rest_test :
Log.d("method event get", "execite :)");
Rest r = new Rest();
r.get("http://www.cheesejedi.com/rest_services/get_big_cheese.php?puzzle=1");
r.getResponseString();
EditText etdit = (EditText) findViewById(R.id.rest_text);
EditText etdit2 = (EditText) findViewById(R.id.rest_error_text);
etdit.setText(r.getResponseText());
etdit2.setText(r.getError());
break;
how can I achieve that
This could be due to a lot of errors.
Are you sure it does not crash because of your two EditTexts not being found?
etdit.setText would cause NullPointerExceptions.
Did u try to check the response Its something like this:
[{"id":"755","level":"1","time_in_secs":"3","par":"0","initials":"KiB","quote":"Got'cha!","time_stamp":"2012-04-09 22:50:52"},{"id":"977","level":"1","time_in_secs":"3","par":"0","initials":"WJF","quote":"dats right numero uno","time_stamp":"2013-03-11 02:29:36"},{"id":"874","level":"1","time_in_secs":"3","par":"0","initials":"WJF","quote":"Look, Ma. No hands!","time_stamp":"2012-05-25 15:44:10"},{"id":"59","level":"1","time_in_secs":"4","par":"0","initials":"TMF","quote":"Hey hey ole ' hickery","time_stamp":"2012-03-03 04:36:15"},{"id":"61","level":"1","time_in_secs":"4","par":"0","initials":"WJF","quote":"I'm on a boat!","time_stamp":"2012-03-03 04:38:54"}]
As you can see its not ResponseString its a ResponseArray. Modify it accordingly.
Also check whether it type is POST or GET

Categories

Resources