title: Implementing multiple fragments in a single activity Dynamically
body: <h2>I am working on fragments</h2>
  
  <hr>
  
  <p><strong>Use case i am trying to implement</strong>::</p>
  
  <ul>
  <li>I am using dynamic fragments</li>
  <li>I am using three fragments in a single activity</li>
  <li>my goal is to communicate between all the three fragments</li>
  <li>I am using support package for fragments</li>
  </ul>
  
  <hr>
  
  <p>Each fragment has a single widget</p>
  
  <ul>
  <li><code>my_fragment1</code> has <code>edittext</code></li>
  <li><code>my_fragment2</code> has <code>button</code></li>
  <li><code>my_fragment3</code> has <code>TextView</code></li>
  </ul>
  
  <hr>
  
  <h2>On click of <code>button</code> the text from the <code>edittext</code> must be displayed in the <code>textview</code></h2>
  
  <hr>
  
  <p><strong><em>What i have tried so far i have constructed most of the scenario below</em></strong></p>
  
  <hr>
  
  <p><strong>Top_Fragment.java</strong></p>
  
  <pre><code>public class Top_Fragment extends Fragment{
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          // TODO Auto-generated method stub
  
          View view=inflater.inflate(R.layout.my_fragment1, container, false);
  
          return view;
      }
  }
  </code></pre>
  
  <p><strong>Middle_Fragment.java</strong></p>
  
  <pre><code>package com.example.deleteme;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewGroup;
  import android.widget.Button;
  
  public class Middle_Fragment extends Fragment{
  
      View view;
      Button btn;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          // TODO Auto-generated method stub
  
          view=inflater.inflate(R.layout.my_fragment2, container, false);
          btn=(Button) view.findViewById(R.id.button1);
          btn.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
  
  
  
              }
          });
          return view;
      }
  }
  </code></pre>
  
  <p><strong>Bottom_Fragment.java</strong></p>
  
  <pre><code>public class Bottom_Fragment extends Fragment{
  
      View view;
      TextView display_text;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          view=inflater.inflate(R.layout.my_fragment3, container,false);
          display_text=(TextView) view.findViewById(R.id.editText1);
          return view;
      }
  
      public void setName(String Name){
          display_text.setText("Result::" + Name);
      }
  
  
  
  }
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends FragmentActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Top_Fragment frg=new Top_Fragment();//create the fragment instance for the top fragment
          Middle_Fragment frg1=new Middle_Fragment();//create the fragment instance for the middle fragment
          Bottom_Fragment frg2=new Bottom_Fragment();//create the fragment instance for the bottom fragment
  
          FragmentManager manager=getSupportFragmentManager();//create an instance of fragment manager
  
          FragmentTransaction transaction=manager.beginTransaction();//create an instance of Fragment-transaction
  
          transaction.add(R.id.My_Container_1_ID, frg, "Frag_Top_tag");
          transaction.add(R.id.My_Container_2_ID, frg1, "Frag_Middle_tag");
          transaction.add(R.id.My_Container_3_ID, frg2, "Frag_Bottom_tag");
  
  
          transaction.commit();
  
      }
  
  
  }
  </code></pre>
  
  <p><strong>activity_main.xml</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="fill_parent"
      android:layout_height="fill_parent"
      tools:context=".MainActivity" 
      android:background="@color/black"&gt;
  
      &lt;FrameLayout
          android:id="@+id/My_Container_1_ID"
          android:layout_width="fill_parent"
          android:layout_height="150dp" 
          android:background="@color/yellow"&gt;
      &lt;/FrameLayout&gt;
  
      &lt;FrameLayout
          android:id="@+id/My_Container_2_ID"
          android:layout_width="fill_parent"
          android:layout_height="150dp"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/My_Container_1_ID"
          android:background="@color/Orange" &gt;
      &lt;/FrameLayout&gt;
  
      &lt;FrameLayout
          android:id="@+id/My_Container_3_ID"
          android:layout_width="fill_parent"
          android:layout_height="150dp"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/My_Container_2_ID"
          android:background="@color/purple" &gt;
      &lt;/FrameLayout&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>my_fragment1.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="@color/green" &gt;
  
      &lt;EditText
          android:id="@+id/editText1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          android:ems="10"
          android:textColor="#000000"
          android:singleLine="true" &gt;
  
          &lt;requestFocus /&gt;
      &lt;/EditText&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>my_fragment2.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" 
      android:background="@color/pink"&gt;
  
      &lt;Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          android:background="@color/black"
          android:text="Button"
          android:textColor="#FFFFFF" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>my_fragment3.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
      &lt;TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          android:text="TextView"
          android:textColor="#000000"
          android:textSize="30dp" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>My output is Like below</strong> ::</p>
  
  <p><img src="https://i.stack.imgur.com/RrNjz.png" alt="enter image description here"></p>
  
  <hr>
  
  <p><strong>What I am having problem in achieving</strong> ::</p>
  
  <ul>
  <li>I am not able to set the value obtained from <code>edit text</code> to
  <code>textview</code> on click of the <code>button</code></li>
  </ul>
  
  <p><strong>Any Ideas?</strong></p>