glaltv
glaltv

Reputation: 61

I am not able to toast messages in Android Studio

I have created an add button. No data is entered in the EditText field and when the user press the button I am not able to Toast the message.

If(text1.getText( ).toString( ).matches(" ") || text2.getText( ).toString( ).matches(" "))
{
    Toast.makeText(MainActivity.this,"input values",Toast.LENGTH_SHORT.show( );
}

Upvotes: 0

Views: 84

Answers (4)

Gowthaman M
Gowthaman M

Reputation: 8282

Use trim() this function will remove spaces.

  If(text1.getText( ).toString( ).trim().equals("") || text2.getText( ).toString.trim().equals(""))
    {
        Toast.makeText(MainActivity.this,"You did not enter a username and password",Toast.LENGTH_SHORT.show( );
    }

I hope this will helpful.

Upvotes: 0

Crono
Crono

Reputation: 2054

Try changing your code and IF condition like this:

If (String.valueOf(text1.getText()).equals("") || String.valueOf(text2.getText()).equals(""))
{
    Toast.makeText(MainActivity.this,"input values",Toast.LENGTH_SHORT).show();
}

That's all,

Hope it helps :)

Upvotes: 0

Saber Solooki
Saber Solooki

Reputation: 1220

You are matching wrong string. Instead of .match(" ") use .match("") or it is better to use text1.getText().toString().isEmpty(). In fact your if block never reached.

Upvotes: 1

Sunil P
Sunil P

Reputation: 3838

you can try this

EditText usernameEditText = (EditText) findViewById(R.id.editUsername);
sUsername = usernameEditText.getText().toString();
if (sUsername.matches("")) {
    Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_SHORT).show();
}

Upvotes: 1

Related Questions