title: Android - Accessing a button defined in another layout from MainActivity
body: <p>I want to access a button defined in a separate xml layout from MainActivity and do things like onClick on the button. I used LayoutInflater as shown below in MainActivity but the onClick method doesn't detect the button being clicked. The button is defined inside a LinearLayout, which is displayed on a custom ListView by a custom Adapter. I can setup an onClick method inside a class that inflates the LinearLayout (xml file), but then how would I do things in MainActivity after detecting a button click? </p>
  
  <p>Can I detect button clicks inside MainActivity? Or do I have to create communication between the LinearLayout which contains the button and MainActivity via Intent? Please tell me some of the possible ways of doing this.</p>
  
  <p>MainActivity:</p>
  
  <pre><code> LayoutInflater inflater = this.getLayoutInflater();
   LinearLayout list_item = (LinearLayout)inflater.inflate(R.layout.list_item, null);
   Button deleteBtn = (Button)list_item.findViewById(R.id.deleteBtn);
   deleteBtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Log.d(TAG, "delete button clicked");
  
              statusList.setOnDataSelectionListener(new OnDataSelectionListener() {
                  @Override
                  public void onDataSelected(AdapterView parent, View v, int position, long id) {
                      Log.d(TAG, "delete button clicked inside onDataselected");
  
                      DeleteStatusThread thread = new MainActivity.DeleteStatusThread(id);
                      thread.start();
                  }
              });
          }
  
      });
  </code></pre>