Android error when getting password edit text value - android

I'm getting the password-edit-text value but I don't get what I want.
my code in getting the value:
String username = "", password = "";
username = ((EditText)findViewById(R.id.username_pet)).toString();
password = ((EditText)findViewById(R.id.password_pet)).toString();
but when I put the result to Toast I don't get it right. What I get is this
error.
any help will be much appreciated. Thanks!

Your code would return you the id of your EditText yet what you want is the Text.
Try this
Replace this by :
username = ((EditText)findViewById(R.id.username_pet)).toString();
password = ((EditText)findViewById(R.id.password_pet)).toString();
this
username = ((EditText)findViewById(R.id.username_pet)).getText();
password = ((EditText)findViewById(R.id.password_pet)).getText();
Hope this helps

username = ((EditText)findViewById(R.id.username_pet)).getText().toString();
password = ((EditText)findViewById(R.id.password_pet)).getText().toString();

This is happening because you are attempting to convert to string before getting the text of the input field. The app is doing exactly what you are telling it to do: Getting the EditText View and converting the View to a string, just not the text within the view. If that makes sense.
Try
EditText userName = (EditText) findViewById(R.id.username_pet);
String userNameFromEditText = (String) username.getText().toString();
Then toast the value of userNameFromEditText.

Related

Cannot resolve substring() or indexOf() methods

I am new to android and i am trying to search for a certain character in an edit text and then extract all the characters before it in a new variable using the substring() and indexOf() methods , but android studio saying that it cannot solve either method . So please tell me what i am doing wrong with the code. Here is the declaration for the edit text :
EditText text;
text = (EditText) findViewById(R.id.text);
And the code for calling these methods :
if(text.getText().toString().contains("+")) {
String before = text.substring(text.indexOf("+") - 1);
}
You're calling substring() and indexOf() on your text variable, which is an EditText object, and they're string methods. Convert to string first and then use them, like so:
String textString = text.getText().toString();
if(textString.contains("+")) {
String before = textString.substring(textString.indexOf("+") - 1);
}
You need to call them in the same way you call contains(): text.getText().toString() because the two methods are part of the String class, not EditText.
You can try String before = text.split("+")[0]; This will return you everything before the first '+'

Android-Prestashop user authentication

I'm developing Android App for a website which is Prestashop base.
When I'm signing in the user with email, I receive following value for Password field.
"passwd": "$2y$10$UwXekiYUoeH7K8rpi9YxKerHxHQZacMSIG3VKseVQ2fjlGlFRfN4W",
All I want to do is, just compare this value with a String(value from password EditText).
After successful comparison, I'll be able to Login the user.
Please help me! how can encrypt String value, to compare with above value?
(or is there any technique to decrypt above password value?)
You can use String1.equals(String2) in a if else statement to get this done.
String1 = received password value;
String2 = edittext.getText().toString; //value from edit text.
if(String1.equals(String2))
{
//Login sucessfull
}
else
{
//Failed
}
Decoding:
String password = "$2y$10$UwXekiYUoeH7K8rpi9YxKerHxHQZacMSIG3VKseVQ2fjlGlFRfN4W";
byte[] tmp2 = Base64.decode(password);
String val2 = new String(tmp2, "UTF-8"); // here you will get the decoded value.

How to get Text written after "," in MultiAutoCompleteTextView?

I am developing an application that uses MultiAutoCompleteTextView for showing hints in the drop down list.In this application I retrieve the value written in the MultiAutoCompleteTextView by using
multitextview.getText();
and then query this value to server to recieve JSON response which is shown as suggestions in the drop down list.
If a user types Mu and then Selects music from the list and then types box for another suggestion the content in the MultiAutoCompleteTextView becomes Music,box and now the value for querying to the server is Music,box instead of this I want to select only box.
My question is how to retrieve text written after "," in MultiAutoCompleteTextView?
Can this be achieved using getText()?
I solved this issue
String intermediate_text=multitextview.getText().toString();
String final_string=intermediate_text.substring(intermediate_text.lastIndexOf(",")+1);
I'm sure there are several ways to get around this. One way to do it would be:
String textToQuerryServer = null;
String str = multitextview.getText().toString(); // i.e "music, box" or "any, thing, you , want";
Pattern p = Pattern.compile(".*,\\s*(.*)");
Matcher m = p.matcher(str);
if (m.find()) {
textToQuerryServer = m.group(1);
System.out.println("Pattern found: "+ textToQuerryServer);
}else {
textToQuerryServer = str;
System.out.println("No pattern: "+ textToQuerryServer);
}

The method getText() is undefined for the type String error

String email = email.getText().toString();
Please help getting error The method getText() is undefined for the type String
Change - String email = email.getText().toString() to
String emailStr = email.getText().toString()
It seems that you have shadowed an attribute email by calling a variable with the same name.
Hence email does not reference what you meant anymore (a textfield I suppose) but a String.
You should change the name of your variable.
Assume the id of your EditText is email. To get the Text of the EditText you can use
String email = ((EditText)findViewById(R.id.email)).getText().toString();

Android: cannot convert from String to Editable

A quick question:
I am getting the error : cannot convert from String to Editable.
Here is the code :
Credential.getInstance().UserName = preferences.getString("UserName", "invalid value");
Credential.getInstance().Password = preferences.getString("UserName", "invalid value");
I've also tried casting like following:
Credential.getInstance().UserName = (Editable) preferences.getString("UserName", "invalid value");
Credential.getInstance().Password = (Editable) preferences.getString("UserName", "invalid value");
This time I am getting error : Cannot cast from String to Editable.
What to do?
For anyone interested in converting a String to Editable
Editable.Factory.getInstance().newEditable("myString");
From the docs, append is
Convenience for append(String.valueOf(text)).
Surely:
Credential.getInstance().UserName.append(preferences.getString("UserName", "invalid value"));
The API with the answers:
http://developer.android.com/reference/android/text/Editable.html

Categories

Resources