title: Changing Fragments on button Click
body: <p>I want to change fragments depending on button click. (I have made two fragment classes called fragmentLeft and fragmentMiddle)</p>
  
  <p>This is my MainActivity.java</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          setContentView(R.layout.activity_main);
  
  
      }
  Fragment fr;
      public void selectFrag(View view) {
  
  
           if(view == findViewById(R.id.bigbutton_left)) {
               fr = new fragmentLeft();
  
  
  
           }else {
               fr = new fragmentMiddle();
  
           }
  
           FragmentManager fm = getFragmentManager();
  
           if (fm != null) {
               // Perform the FragmentTransaction to load in the list tab content.
               // Using FragmentTransaction#replace will destroy any Fragments
               // currently inside R.id.fragment_content and add the new Fragment
               // in its place.
               FragmentTransaction ft = fm.beginTransaction();
               ft.replace(R.id.fragment_place, fr);
               ft.commit();
           }
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.activity_main, menu);
  
  
          return true;
      }
  
  }
  </code></pre>
  
  <p>This is fragmentLeft.java</p>
  
  <pre><code>public class fragmentLeft extends Fragment {
     @Override
     public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState) {
  
         //Inflate the layout for this fragment
  
        return inflater.inflate(
                R.layout.fragmentleft, container, false);
     }
  }
  </code></pre>
  
  <p>the same is for fragmenttwo with the exception of the layout id passed to infater.inflate()</p>
  
  <p>I have made two layout files two, with the correct names as well .
  This is the definition of my fragment from my activity_main.xml</p>
  
  <p></p>
  
  <p>But I am unable to get the buttons to do anything. No transactions take place, and I am stuck with the first fragment.</p>
  
  <p>After a bit of googling , I came to know that its not recommended that you use fragment in the layout, but some other type of layout, but I saw this example code <a href="http://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/" rel="nofollow">http://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/</a>
  that seems to work just fine.</p>
  
  <p>what is the problem ?</p>
  
  <p><strong>::EDIT::</strong>
  <strong>There is something really weird going on. 
  If I use this layout xml file  for main Activity:</strong></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="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
    &lt;Button
          android:id="@+id/button1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Fragment No.1"
          android:onClick="selectFrag" /&gt;
  
       &lt;Button
           android:id="@+id/button2"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:onClick="selectFrag"
           android:text="Fragment No.2" /&gt; 
  
     &lt;fragment
          android:name="com.mainpackage.FragmentOne"
          android:id="@+id/fragment_place"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>The fragments change and behave as they are expected to.
  But if I use this layout:</strong></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity" &gt;
  
  
  
      &lt;Button
          android:id="@+id/button2"
          android:layout_width="100dp"
          android:layout_height="100dp"
          android:layout_alignBaseline="@+id/bigbutton_middle"
          android:layout_alignBottom="@+id/bigbutton_middle"
          android:layout_marginLeft="7dp"
          android:layout_toRightOf="@+id/bigbutton_middle"
          android:background="@drawable/mainbutton_right" /&gt;
  
      &lt;Button
          android:id="@+id/bigbutton_left"
          android:layout_width="100dp"
          android:layout_height="100dp"
          android:layout_alignBaseline="@+id/bigbutton_middle"
          android:layout_alignBottom="@+id/bigbutton_middle"
          android:layout_marginRight="7dp"
          android:layout_toLeftOf="@+id/bigbutton_middle"
          android:background="@drawable/mainbutton_left" /&gt;
  
      &lt;Button
          android:id="@+id/bigbutton_middle"
          android:layout_width="100dp"
          android:layout_height="100dp"
          android:layout_alignParentTop="true"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="187dp"
          android:background="@drawable/mainbutton_middle" /&gt;
        &lt;fragment
      android:id="@+id/fragment_place"
      android:name="com.mainpackage.FragmentOne"
      android:layout_width="match_parent"
      android:layout_height="80dp"
      android:layout_below="@+id/bigbutton_right" /&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>The fragments dont seem to change,i.e. the buttons seem to be dead.</strong> </p>
  
  <p>What could be the problem ?</p>
  
  <p>I am using Eclipse Juno</p>