samedi 27 mars 2021

How to have a toast appear on a if else statements on android studio

I have been trying to get a toast to appear on my screen whenever a user types a certain name in the edit text box. When the user successfully types in the correct name I want the toast to display a successful message and if the name is not correct I want the toast to display an error message. Whenever I run the code below and type a name nothing appears whether right or wrong. This is what I have so far.

package com.example.app1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Invites extends AppCompatActivity {
    EditText username;
    Button invite, finished;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invites);
        username = (EditText) findViewById(R.id.username);
        invite = (Button) findViewById(R.id.invite);
        finished = (Button) findViewById(R.id.finished);

        invite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (username.getText().toString().contains("Fname Lname")) {
                    Toast.makeText(Invites.this, "Invitation Sent", Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(Invites.this, "User not found", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

Aucun commentaire:

Enregistrer un commentaire