tags:

views:

160

answers:

3

hey! 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();
   }
  });
 }
}
A: 

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

mbaird
Hey, I wanted to know if the setOnClickListener was made in the correct place, that is, it is called IN the code for the View itself. Is that correct?I know it works, but is it OK to call that method IN the code of the View?
Ted
A: 

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.

Erich Douglass
yes, I know that the above code didnt show anything (it has now been edited). But that wasnt really my question :-) I wanted to know if the setOnClickListener was made in the correct place, that is, it is called IN the code for the View itself. Is that correct?I know it works, but is it the way it is meant to be?
Ted
If you want every instance of your view to have the same onclick behavior, then it's perfectly acceptable.
Erich Douglass
mm... Actually, a click on the View should always open up another "popup" (detailed view of the object sort of), but the info in the popup depends of course in the View first clicked.The code above is just test code (im new to Android, but have been programming for some years now in C#)
Ted
A: 

You probably should decouple the click action from the view. Is there any reason you want to hardcode the click listener?

James
Well, this is just test-code since Im new to Android. But the click should always open up a new popup (a custom view as a popup somehow). that second View, the popup, contaisn detailed data and thus should be populated depending on what View was clicked in the first place.
Ted
Yes, it looks fine for test code.If you're going to have more than one instance of this custom view, it seems like you will want to set the OnClickListeners from within the activity.
James