0% found this document useful (0 votes)
15 views2 pages

Toast Message

The document provides instructions for adding a toast message to a Login application in Android. It outlines the steps to open the project, modify the validate method to include a toast message for incorrect login attempts, and run the application on an Android Virtual Device (AVD). The provided script includes the necessary code for the MainActivity class, demonstrating the implementation of the toast message functionality.

Uploaded by

a78134449
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Toast Message

The document provides instructions for adding a toast message to a Login application in Android. It outlines the steps to open the project, modify the validate method to include a toast message for incorrect login attempts, and run the application on an Android Virtual Device (AVD). The provided script includes the necessary code for the MainActivity class, demonstrating the implementation of the toast message functionality.

Uploaded by

a78134449
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Add a toast message in Login Application.

Step 1: Open the ‘Login’ project.


Step 2: In the validate method, add a toast message with the syntax:
Toast.makeText(MainActivity.this, "Message", Toast.LENGTH_SHORT).show();

Script:
package com.example.login;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {


private EditText Name;
private EditText Password;
private TextView Info;
private Button Login;
private int counter=5;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Name = (EditText)findViewById(R.id.userNameText);
Password = (EditText)findViewById(R.id.userPasswordText);
Info = (TextView)findViewById(R.id.showInfoTextView);
Login = (Button)findViewById(R.id.loginButton);

Info.setText("No. of attempts remaining: 5");


Login.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
validate(Name.getText().toString(),
Password.getText().toString());
}
});

private void validate(String userName, String userPassword){

if((userName.equals("Admin")) && (userPassword.equals("1234"))){


Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
startActivity(intent);
} else {
counter--;
Name.setText("");
Password.setText("");
Toast.makeText(MainActivity.this, "Wrong ID/Password",
Toast.LENGTH_SHORT).show();
Info.setText("No. of attempts remaining:
"+String.valueOf(counter));
if (counter==0){
Login.setEnabled(false);
}
}
}
}

Step 3: Run the application on AVD.

You might also like