Ted
Ted

Reputation: 20184

Clickable view / onclick: Correct approach to make View clickable?

The following code will make the View clickable, but I am wondering if this is the correct approach to make a custom view clickable?

Code:

public class NodePickup extends LinearLayout
{
 public NodePickup(Context context, AttributeSet attributeSet)
 {
  super(context, attributeSet);

  LayoutInflater inflater = (LayoutInflater)     context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.nodepickup, this);

        this.setOnClickListener(new OnClickListener() 
        {
         @Override
   public void onClick(View v)
   {
          AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
          builder.setMessage("Ajabaja!")
          .setCancelable(true)
          .setPositiveButton("JA!", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) 
                    {
                     dialog.cancel();
                    }
                });
          builder.show();
   }
  });
 }
}

Upvotes: 1

Views: 3552

Answers (2)

Erich Douglass
Erich Douglass

Reputation: 52002

The code in onClick() is simply creating the dialog - there's nothing there that would cause it to get displayed on screen. To make this work, call showDialog(int) in your click handler and implement onCreateDialog(int) in your activity.

Check out the Creating Dialogs section of the Android docs for more information.

Upvotes: 1

Mark B
Mark B

Reputation: 200722

Calling setOnClickListener() is the appropriate way of making a view clickable.

Upvotes: 0

Related Questions