title: Android How to call Fragment from my Main Activity
body: <p>i am very new to android development.</p>
  
  <p>in my case i have one main activity and one fragment created.Main activity as two buttons.</p>
  
  <p>when i click on button, my fragment should load .how can i achieve that?</p>
  
  <p>Mainactivity .XML
      
      </p>
  
  <pre><code>&lt;Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
      android:text="Fragment 2"
      android:id="@+id/fragment_button_2"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true" /&gt;
  
  &lt;Button
      style="?android:attr/buttonStyleSmall"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Fragment 1"
      android:id="@+id/fragment_button_1"
      android:layout_above="@+id/fragment_button_2"
      android:layout_alignLeft="@+id/fragment_button_2"
      android:layout_alignStart="@+id/fragment_button_2"
      android:layout_marginBottom="33dp" /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Main activity .java</p>
  
  <pre><code>package com.bentgeorge.fragment;
  
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  
  public class MainActivity extends AppCompatActivity implements    View.OnClickListener{
  private Button fragment_btn_1;
  private Button fragment_btn_2;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      fragment_btn_1 = (Button) findViewById(R.id.fragment_button_1);
      fragment_btn_2 = (Button) findViewById(R.id.fragment_button_2);
  
  
  }
  
  @Override
  public void onClick(View v) {
  
  Fragment1 frag = new Fragment1();
  FragmentManager manager = getSupportFragmentManager();
  FragmentTransaction transaction = manager.beginTransaction();
  transaction.add(R.id.mainlayout,frag,"Test Fragment");
  transaction.commit();
  
  }
  }
  </code></pre>
  
  <p>fragment_fragment1.xml</p>
  
  <pre><code>&lt;FrameLayout 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="com.bentgeorge.fragment.Fragment1"&gt;
  
  &lt;!-- TODO: Update blank fragment layout --&gt;
  &lt;TextView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:text="@string/hello_blank_fragment" /&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>Fragment1.java</p>
  
  <pre><code>package com.bentgeorge.fragment;
  
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  
  /**
  * A simple {@link Fragment} subclass.
  */
  public class Fragment1 extends Fragment {
  
  
  public Fragment1() {
      // Required empty public constructor
  }
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      return inflater.inflate(R.layout.fragment_fragment1, container,  false);
  }
  
  }
  </code></pre>
  
  <p>Not done much codes as i am confused with internet available tutorials.</p>
  
  <p>Please help me to fix this </p>
  
  <p>Thanks &amp; Regards,</p>
  
  <p>Update:</p>
  
  <p>i have done some changes and my fragment is getting loaded now. ut the buttons are still visible . How can i hide the buttons</p>
  
  <p><a href="https://i.stack.imgur.com/pSpt5.png" rel="noreferrer"><img src="https://i.stack.imgur.com/pSpt5.png" alt="Fragment"></a></p>