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();
}
});
}
}
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, callshowDialog(int)in your click handler and implementonCreateDialog(int)in your activity.Check out the Creating Dialogs section of the Android docs for more information.