Q:

button.setOnClickListener is giving me errors

 Button myButton = new Button(this);
        myButton.setText("Change View");
        myButton.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        myButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Context context = getApplicationContext();
                CharSequence text = "Hello toast!";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();

                }
        });

What exactly is wrong with this code? It gives me an error where I set the onClickListener. I also had to remove the @override above public void despite the fact that other examples have them.

A:

Since Button is a view, you should create View.OnClickListener:
myButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Context context = getApplicationContext();
        CharSequence text = "Hello toast!";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
     }
});

