I have a small activity that contains a textview and a password. I have written a small text to test this activity. But the assert on the last line is failing. Could someone please tell me why ?
loginScreenActivity=startActivity(mStartIntent, null, null);
assertNotNull(loginScreenActivity);
mInstrumentation.waitForIdleSync();
final EditText userName=(EditText)loginScreenActivity.findViewById(R.id.userName);
final EditText password=(EditText)loginScreenActivity.findViewById(R.id.password);
loginScreenActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
userName.requestFocus();
}
});
mInstrumentation.waitForIdleSync();
this.sendKeys(KeyEvent.KEYCODE_R);
this.sendKeys(KeyEvent.KEYCODE_SHIFT_LEFT);
this.sendKeys(KeyEvent.KEYCODE_A);
this.sendKeys(KeyEvent.KEYCODE_F);
this.sendKeys(KeyEvent.KEYCODE_A);
this.sendKeys(KeyEvent.KEYCODE_Q);
this.sendKeys(KeyEvent.KEYCODE_A);
this.sendKeys(KeyEvent.KEYCODE_T);
assertTrue(userName.getText().toString().equals("Rafaqat"));
From the sequence, I guess you are inputing "rAfarqat"?
Related
i am trying to create a login system on android studio using java, i have tried a piece of code i have found and modifies it to my own program- i am getting errors that the tutorial cannot explain and would appreciate if someone could tell me what i'm doing wrong.
on line 10 under username it says expression expected
The else statement says there should be an if but as you can see there is ?
public void LoginButton(){
UserName = findViewById(R.id.UserName);
userPassword = findViewById(R.id.userPassword);
userPin = findViewById(R.id.userPin);
GoBtn.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (UserName.getText().toString().equals("user");// here i would preferably link to a database but do to time restaitns i have modeled it with user
userPassword.getText().toString().equals("pass");
{
Toast.makeText(MainActivity.this, "Username and password is correct",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, StudentActivity.class);
startActivity(intent);
}
else {
Toast.makeText(MainActivity.this,"Username and password is NOT correct",
Toast.LENGTH_SHORT).show();
}
}
I need this to lead the user to the next activty if the input is correct but so far i cannot get it to run due to the errors.
Issues
You can't use a semicolon (;) right after if statement because an empty ; is also considered a statement.
If Statement is incorrect: It has to be a valid statement
Many braces ({ and }) are missing
Here is a sample code
public void LoginButton() {
UserName = findViewById(R.id.UserName);
userPassword = findViewById(R.id.userPassword);
userPin = findViewById(R.id.userPin);
GoBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (UserName.getText().toString().equals("user") &&
userPassword.getText().toString().equals("pass")) {
// use your code
} else {
// use your code
}
}
});
}
Suggestion: Please read more Java
your if condition syntax is wrong you need to use AND operator(&&).
change these lines
if (UserName.getText().toString().equals("user");
userPassword.getText().toString().equals("pass");
to
if((UserName.getText().toString().equals("user")) && (userPassword.getText().toString().equals("pass")))
I'm trying to check the editText condition. In the code below, I declared a setOnClickListener method to check the condition of editText. If condition is true, I want to print toast message, change the activity and to output a sound. If condition fails, it should toast a single message. In both cases if it's true or not, it prints me only "Incorect" no matter if editText is correct.
What I am doing wrong?
public void next(View v){
final MediaPlayer correctSound = MediaPlayer.create(this, R.raw.correctsound);
Button playCorrectSound = (Button) this.findViewById(R.id.angry_btn1);
final EditText editTextt = (EditText) findViewById(R.id.editText);
playCorrectSound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
editTextt.setVisibility(View.INVISIBLE);
if(editTextt.getText().toString() == "string")
{
Toast.makeText(getApplicationContext(), "Correct", Toast.LENGTH_SHORT).show();
correctSound.start();
Intent i = new Intent(Hereuu.this, MainActivity.class);
startActivity(i);
} else {
Context context = getApplicationContext();
CharSequence text = "Incorect";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
editTextt.setVisibility(View.VISIBLE);
}
});
}
Like everyone had said, you
Basically, when you use == operator, your are checking if the reference for object are the same/equals. When you use .equals(String), the method checks the content.
tip:
When your are working with Strings, remember to avoid NullPointerException situations. So,you should write "constant".equals(editTextValue) instead of editTextValue.equals("constant")
The link bellow will help you to understand how String objects and String content work:
Java String.equals versus ==
regards
in java you need to compare strings using equals method instead of ==
check this topic for more details
I would suggest you to take some basic JAVA lessons. That will immensely help you.
For now, the problem is in the way you are checking equality of the strings. You do not use == with strings, you use String#equals() method. So,
Change
editTextt.getText().toString() == "string"
to
editTextt.getText().toString().equals("string")
Make sure to compare strings in java with .equals and not ==. Use this if statement:
if(editTextt.getText().toString().equals("string"){
I am very new to coding. What i'm trying to do is that when someone does not write anything, use an setError() like "Please write your your answer." However, when someone click the text box, i want the writing to go away but the error symbol to stay. I have seen and tried solution where you override the setError to just set the picture.
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void setError(CharSequence error, Drawable icon) {
setCompoundDrawables(null, null, icon, null);
}
However, with this method, i cannot set a charsequence
you can set error on click on the button say register or signin whatever is your function to go ahead. and check that the string is empty than
set the error like
String str = myEditText.getText().toString();
if (str.length() == 0 || str.equals("")) {
myEditText.setError("field can't be empty");
}
Simple handle in button click and show error msg like below. its work for me.
#Override
public void onClick(View v) {
String data = mEditText.getText().toString();
if(data.isEmpty())
{
mEditText.setError(""Please write your your answer.");
return;
}
}
I am working on an android app. I am having an EditText on which I have applied OnCllickListener().
EditText _input = new EditText(context);
_input.setSingleLine(true);
_input.setFocusable(false);
I am setting OnClickListener() on this EditText:
_input.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// OnClick work here
}
});
I have applied some validations on this field and calling setError() method on this EditText. It shows that red icon of error in the EditTextwhen the validation fails. But when I click on that error icon it executes the OnClickListener() on this EditTextand I am not able to see the error occurred.
Is there any other way to do so, so that I can handle both functions.
Thanks a lot in advanced !!!
You would check if currently the edit text is in an error state, within the onclick handler:
_input.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (view.getError() == null) {
// do not handle the click
return;
}
}
});
You can check for an error like like this:
if(view.getError() == null) return;
See the doc here
getError()
Returns the error message that was set to be displayed with
setError(CharSequence), or null if no error was set or if it the error
was cleared by the widget after user input.
Source
I have solved this with a simple solution. Just set text of editText to empty string before setting error and error message will show again like without onClickListener.
Ok so I only have a EditText field and a button, which when pressed triggers an AsyncTask.
EditText playerName = (EditText)findViewById(R.id.playerEditText);
if(playerName.getText().toString().length() == 0 )
playerName.setError("Player name is required!");
else {
// do async task
}
The problem is that the error message seems to stay up even after when I input valid text to search. Is there a way to remove the error as soon as the EditText is not empty?
In your else bracket, put playerName.setError(null), which will clear the error.
API documentation: "The icon and error message will be reset to null when any key events cause changes to the TextView's text."
Though it is not so - and therefore we can regard this as bug.
If you use inputType such as textNoSuggestions, textEmailAddress, textPassword, the error is unset after a character is typed. Nearly as documented but again not exactly - when you delete a character, error stays.
It seems, a simple workaround with addTextChangedListener and setError(null) can attain promised behavior.
Besides there are posts about icon losing on Android 4.2. So use with care.
Try this listener:
playerName.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable edt){
if( playerName.getText().length()>0)
{
playerName.setError(null);
}
}
If you want to hide the error message one way is you apply onclicklistener on the edit box and then
editTextName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
editTextName.setError(Null)
}
});
Below code worked for me
#OnTextChanged(
value = R.id.editTextName,
callback = OnTextChanged.Callback.TEXT_CHANGED)
public void afterInput(CharSequence sequence) {
editTextName.setError(null);
editTextName.setErrorEnabled(false);
}
'
editTextName.setError(null) Will clear the error message.
editTextName.setErrorEnabled(false) Will remove additional padding.
Add a TextWatcher to your EditText and onError, show your error message using et.setError(errorMessage) else you can remove the error message and error icon like below.
// to remove the error message in your EditText
et.setError(null);
// to remove the error icon from EditText.
et.setCompoundDrawables(null, null, null, null);
This code worked for me.
textInputSetting(binding.emailEdt)
fun textInputSetting(view: TextInputLayout) {
view.apply {
this.editText!!.addTextChangedListener {
if (this.editText!!.text.isNotEmpty()) {
this.error = null
this.isErrorEnabled = false
}
}
}
}