Custom button example not working

I am new to Android app development. I am using the sample form provided at http://developer.android.com/resources/tutorials/views/hello-formstuff.html . In this I used an example of a custom button.

My code is as follows:

    package com.example.helloformstuff;

    import android.app.Activity;
    import android.content.DialogInterface.OnClickListener;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;

    public class HelloFormStuff extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks
                Toast.makeText(HelloFormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show();
            }
        });    

    }
   }

The following error is shown:

The type new DialogInterface.OnClickListener(){} must implement the inherited 
     abstract method DialogInterface.OnClickListener.onClick(DialogInterface, int)
    - The method setOnClickListener(View.OnClickListener) in the type View is not 
     applicable for the arguments (new DialogInterface.OnClickListener(){})

I can not understand the reason for this error.

Please help me with this.

thank

Pankai

+5
source share
3 answers

Replace import android.content.DialogInterface.OnClickListener with

import android.view.View.OnClickListener

The rest of the code remains unchanged, the code will execute fine.

+1
source

OnClickListener. :

   button.setOnClickListener(new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // Perform action on clicks
            Toast.makeText(HelloFormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show();
        }
    });   
+3

You are using the wrong OnClickListener. Try View.OnClickListener:

button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            Toast.makeText(HelloFormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show();
        }
    }); 
+1
source

All Articles