-4

Android app, i wrote to test image button doesn't work. I have created a image button and implement an event listener for that button. What's wrong with this source code?

import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;

public class ImageButtonTestApp extends Activity {

    ImageButton imageButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void eventListenerOnButton() {

        imageButton = (ImageButton) findViewById(R.id.imageButton1);

        imageButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

               Toast.makeText(ImageButtonTestApp.this, "ImageButton is clicked!", Toast.LENGTH_SHORT).show();

            }

        });

    }

}

2 Answers2

2

u have written setOnClickListener in different method but u didnot call that method any where call that method in oncreate.

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        eventListenerOnButton();
    }

try this one,

0

Try it

imageButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

           Toast.makeText(ImageButtonTestApp.this, "ImageButton is clicked!", Toast.LENGTH_SHORT).show();

        }

    });
imdungnguyen
  • 216
  • 1
  • 3
  • 14