Monday, January 17, 2022

How we use OnClickListener and simple Toast in android Studio

 package com.example.practice_section;


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button2,button3; // here we define the id
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
button2=findViewById(R.id.button2); //here we find the button by id
button3=findViewById(R.id.button3);
button2.setOnClickListener(new View.OnClickListener() { //this is the method of writing oncclicklistener
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "You have been Sign Up", Toast.LENGTH_SHORT).show(); //this is the method of writing toast
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "You have been sign In", Toast.LENGTH_SHORT).show();
}
});

}
}

No comments:

Post a Comment