title: How to tell if button view exists in java/android
body: <p>I find this hard to believe that i can not find this answer anywhere. It does not seem like the code that i have works correctly for checking the existence of a button view. Here is what i have, any suggestions are greatly appreciated. Thanks!</p>
  
  <p>Java file:</p>
  
  <pre><code>public class LayoutsActivity extends Activity {
  
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.main);
      ActivateButtons();
  }
  
  void ActivateButtons()
  {
      Button mainLayoutButton = (Button) this.findViewById(R.id.MainLayout);
      if(mainLayoutButton != null)
      {
          mainLayoutButton.setOnClickListener(new View.OnClickListener() {    
              public void onClick(View v) {
                  setContentView(R.layout.main);
                  ActivateButtons();
              }
          });
      }
  
      Button linearLayoutButton = (Button) this.findViewById(R.id.LinearLayout);
      if(linearLayoutButton != null)
      {
          linearLayoutButton.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  setContentView(R.layout.mylinearlayout);
                  ActivateButtons();
              }
          });
      }
  
      Button tableLayoutButton = (Button) this.findViewById(R.id.TableLayout);
          tableLayoutButton.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
                  setContentView(R.layout.mytablelayout);
                  ActivateButtons();
              }
          });
  
  
      Button frameLayoutButton = (Button) this.findViewById(R.id.FrameLayout);
      if(frameLayoutButton != null)
      {
          frameLayoutButton.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
                  setContentView(R.layout.myframelayout);
                  ActivateButtons();
              }
          });
      }
      Button relativeLayoutButton = (Button) this.findViewById(R.id.RelativeLayout);
      if(relativeLayoutButton != null)
      {
          relativeLayoutButton.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
                  setContentView(R.layout.myrelativelayout);
                  ActivateButtons();
              }
          });
      }
  
      Button absoluteLayoutButton = (Button) this.findViewById(R.id.AbsoluteLayout);
      if(absoluteLayoutButton != null)
      {
           absoluteLayoutButton.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
                  setContentView(R.layout.myabsolutelayout);
                  ActivateButtons();
              }
          }); 
      }
  
      Button DynamicLayoutButton = (Button) this.findViewById(R.id.DynamicLayout);
      if(DynamicLayoutButton != null)
      {
          DynamicLayoutButton.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
                  LinearLayout dynamicLayout = new LinearLayout(LayoutsActivity.this);
                  dynamicLayout.setOrientation(LinearLayout.VERTICAL);
                  LinearLayout.LayoutParams size=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  
                  final TextView mytext= new TextView(LayoutsActivity.this);
                  mytext.setText("This is the dynamic layout");
                  mytext.setTextSize(20);
                  mytext.setGravity(Gravity.CENTER);
  
                  dynamicLayout.addView(mytext, size);
                  /* Button b= new Button(LayoutsActivity.this);
                  b.setText("Click Here to go to Main Layout");
                  dynamicLayout.addView(b, size); */
  
                  Button b= new Button(LayoutsActivity.this);
                  b.setText("Main Layout");
                  b.setId(R.id.DynamicLayout);
                  dynamicLayout.addView(b, size);
  
                  //Step L
                  Button linear = new Button(LayoutsActivity.this);
                  b.setText("Linear Layout");
                  b.setId(R.id.LinearLayout);
                  dynamicLayout.addView(linear, size);
  
                  Button table = new Button(LayoutsActivity.this);
                  table.setText("Table Layout");
                  table.setId(R.id.LinearLayout);
                  dynamicLayout.addView(table, size);
  
                  ActivateButtons();
  
                  setContentView(dynamicLayout);
              }
          });
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>XML file: I have one for each layout but all are similar</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" &gt;
  
      &lt;TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="@string/hello" /&gt;
  
      &lt;Button
          android:id="@+id/LinearLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Linear layout" /&gt;
          &lt;!-- The layout lays out its children next to each other (horizontally or vertically)--&gt;
      &lt;Button
          android:id="@+id/TableLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Table Layout" /&gt;
          &lt;!-- The layout represents a table like structure where there are columns and rows for the children--&gt;
      &lt;Button
          android:id="@+id/FrameLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Frame Layout" /&gt;
          &lt;!-- The layout places the children on top of one another--&gt;
      &lt;Button
          android:id="@+id/RelativeLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Relative Layout" /&gt;
          &lt;!-- The layout positions its children relative to each other--&gt;
      &lt;Button
          android:id="@+id/AbsoluteLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Absolute Layout" /&gt;
          &lt;!-- The layout has children with absolute coordinates--&gt;
      &lt;Button
          android:id="@+id/DynamicLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Dynamic Layout" /&gt;
          &lt;!-- The layout creates a second, new page --&gt;
      &lt;Button
          android:id="@+id/MainLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Main Layout" /&gt;
          &lt;!-- The layout returns to the original layout which seems to be similar to the linear layout--&gt;
  &lt;/LinearLayout&gt;
  </code></pre>