title: Android - Using Buttons With Single Layout
body: <p>I have created an Activity that uses the ViewFlipper to Switch between Different elements. Each element represents an Item in a store. I would like to add a "Buy" Button to each View. I am however not sure how to do this, since all the views use the default layout i have created. I have added the information like the Price of the Item etc Programmatically. So i am uncertain how to add a listener to the button, since they will all refer to the same button in the xml file:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" &gt;
  
  &lt;Button
      android:id="@+id/credit_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentTop="true"
      android:layout_marginBottom="220dp"
      android:layout_marginLeft="30dp"
      android:layout_marginRight="30dp"
      android:layout_marginTop="104dp" /&gt;
  
  &lt;TextView
      android:id="@+id/credit_type"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignRight="@+id/credit_button"
      android:layout_alignTop="@+id/credit_button"
      android:layout_marginRight="22dp"
      android:layout_marginTop="21dp"
      android:text="Large Text"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:typeface="normal" /&gt;
  
  &lt;TextView
      android:id="@+id/credit_number"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/credit_button"
      android:layout_alignRight="@+id/credit_type"
      android:layout_centerVertical="true"
      android:layout_marginBottom="30dp"
      android:layout_marginLeft="15dp"
      android:text="Medium Text"
      android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>As you can see, the Button id is "Credit Button". So now to be able to differentiate between the different store items' buttons what would i have to do?</p>
  
  <p>Note, i am adding store items dynamically as well, so i cant simply create all the views separately using xml.</p>
  
  <p>OK HERE IS THE UPDATED ANSWER. I used everyones responses below to fix teh issue. So Thank you all :) </p>
  
  <pre><code>        // PerkView
      View PerkView = View.inflate(this, R.layout.store_category, null);
      viewFlipper.addView(PerkView);
      Button perkButton = (Button)
      PerkView.findViewById(R.id.StoreCatItem);
  
      // TitleView
      View TitleView = View.inflate(this, R.layout.store_category, null);
      viewFlipper.addView(TitleView);
      Button titleButton = (Button)
      TitleView.findViewById(R.id.StoreCatItem);
  
      // ProfileView
      View ProfileView = View.inflate(this, R.layout.store_category, null);
      viewFlipper.addView(ProfileView);
      Button profileButton = (Button)
      ProfileView.findViewById(R.id.StoreCatItem);
  </code></pre>
  
  <p>I simply Created Multiple Views Programmatically, and then retrieved the buttons from those views afterwards. I then added the listeners to the buttons as follows:</p>
  
  <pre><code>perkButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              Intent i = new Intent(getBaseContext(), Perks.class);
              i.putExtra("player", player);
              startActivity(i);
          }
      });
  
      titleButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              Intent i = new Intent(getBaseContext(), Titles.class);
              i.putExtra("player", player);
              startActivity(i);
          }
      });
  </code></pre>
  
  <p>Thanks A lot :)</p>