title: OnClick listener for button inside compound view
body: <p>I created a compound view which consist of three views, two <code>ImageButtons</code> and one EditText. I am trying to create new "<code>MyView</code>" view everytime button is clicked. Firstly I decided to set onClick in xml hoping that everytime a new MyView is created, it will have listener set already but it does not seem to work. So my question is, how to set onClick listener, for a button inside compound view, and method is in other class? Method I am talking about is addViewBelow() in <code>ViewControl</code> vlass</p>
  
  <pre><code>MyView.xml
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" &gt;
  
  &lt;EditText
      android:layout_width="0sp"
      android:layout_height="wrap_content"
      android:id="@+id/text"
      android:layout_weight="80" /&gt;
  
  &lt;ImageButton
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_gravity="center"
      android:layout_weight="10"
      android:id="@+id/add"
      android:onClick="addViewBelow"
      android:clickable="true"
      android:background="@drawable/add" /&gt;
  
  &lt;ImageButton
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_gravity="center"
      android:layout_weight="10"
      android:id="@+id/rmv"
      android:background="@drawable/rmv" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>MyView.java</p>
  
  <pre><code>public class MyView extends LinearLayout {
  
  private EditText txt;
  private ImageView add, rmv;
  
  public MyView(Context context)
  {
  super(context);
  
  LayoutInflater inflater = (LayoutInflater) context
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  inflater.inflate(R.layout.myview, this);
  
  txt = (EditText) findViewById(R.id.text);
  add = (ImageView) findViewById(R.id.add);
  rmv = (ImageView) findViewById(R.id.rmv);
  
  }
  
  
  
  public MyView(Context context, AttributeSet attrs) {
  super(context, attrs);
  
  LayoutInflater inflater = (LayoutInflater) context
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  inflater.inflate(R.layout.myview, this);
  
  txt = (EditText) findViewById(R.id.text);
  add = (ImageView) findViewById(R.id.add);
  rmv = (ImageView) findViewById(R.id.rmv);
  
  
  }
  </code></pre>
  
  <p>ViewControl.java</p>
  
  <pre><code>public class ViewControl implements OnClickListener {
  
      LinkedList&lt;MyView&gt; pathView;
      Button mainButton;
      RelativeLayout rl;
      Activity act;
      int lastId;
  
  public ViewControl(Activity activity) {
  
  act = activity;
  //stuff
  
  }
  
  @Override
  public void onClick(View v) {
  //stuff
  
      }
  
  public void addViewBelow() {
  
  RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
          LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
  params.addRule(RelativeLayout.BELOW, lastId);
  
  MyView mv = new MyView(act);
  
  mv.setId(UniqueID.generateViewId());
  mv.setLayoutParams(params);
  lastId = mv.getId();
  pathView.add(mv);
  rl.addView(pathView.getLast());
  }
  }
  </code></pre>