title: how to access a fragment's button in main activity?
body: <p>i have a fragment that has a button in it. on click of the button, it should add a new edit text to the layout dynamically. how do i do that?</p>
  
  <p>main activity:</p>
  
  <pre><code>package comx.example.amithy.hw4;
  
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.EditText;
  
  public class MainActivity extends AppCompatActivity implements BlankFragment.addnew{
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          getSupportFragmentManager().beginTransaction()
                  .add(R.id.container,new BlankFragment(),"add")
                  .commit();
      }
  
      @Override
      public void add() {
          findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  EditText editText = new EditText(MainActivity.this);
                  editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
  
              }
          });
      }
  }
  </code></pre>
  
  <p>fragment:</p>
  
  <pre><code>    package comx.example.amithy.hw4;
  
  
      import android.os.Bundle;
      import android.support.annotation.Nullable;
      import android.support.constraint.ConstraintLayout;
      import android.support.v4.app.Fragment;
      import android.util.Log;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.EditText;
  
  
      public class BlankFragment extends Fragment {
      private addnew mListner;
  
          public BlankFragment() {
          }
  
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {
              // Inflate the layout for this fragment
              return inflater.inflate(R.layout.fragment_blank, container, false);
          }
  
          @Override
          public void onActivityCreated(@Nullable Bundle savedInstanceState) {
              super.onActivityCreated(savedInstanceState);
              getActivity().findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      mListner.add();
                  }
              });
          }
  
  
      public interface addnew{
              void add();
      }
      }
  </code></pre>
  
  <p>main activity.xml file:</p>
  
  <pre><code>   &lt;?xml version="1.0" encoding="utf-8"?&gt;
     &lt;android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity" /&gt;
  </code></pre>
  
  <p>fragment's xml file:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".BlankFragment"&gt;
  
          &lt;TextView
              android:id="@+id/textView"
              android:layout_width="92dp"
              android:layout_height="36dp"
              android:layout_marginStart="16dp"
              android:layout_marginLeft="16dp"
              android:layout_marginTop="16dp"
              android:layout_marginEnd="8dp"
              android:layout_marginRight="8dp"
              android:layout_marginBottom="8dp"
              android:text="Dish"
              android:textAppearance="@style/TextAppearance.AppCompat.Large"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintHorizontal_bias="0.0"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintVertical_bias="0.013" /&gt;
  
          &lt;EditText
              android:id="@+id/editText"
              android:layout_width="245dp"
              android:layout_height="wrap_content"
              android:layout_marginStart="8dp"
              android:layout_marginLeft="8dp"
              android:layout_marginTop="16dp"
              android:layout_marginEnd="8dp"
              android:layout_marginRight="8dp"
              android:layout_marginBottom="8dp"
              android:ems="10"
              android:inputType="textPersonName"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintHorizontal_bias="0.956"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintVertical_bias="0.0" /&gt;
  
          &lt;TextView
              android:id="@+id/textView2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginStart="16dp"
              android:layout_marginLeft="16dp"
              android:layout_marginTop="8dp"
              android:layout_marginEnd="8dp"
              android:layout_marginRight="8dp"
              android:layout_marginBottom="124dp"
              android:text="Add Ingredients"
              android:textAppearance="@style/TextAppearance.AppCompat.Large"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintHorizontal_bias="0.0"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintVertical_bias="0.133" /&gt;
  
          &lt;EditText
              android:id="@+id/editText2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginTop="8dp"
              android:layout_marginBottom="8dp"
              android:ems="10"
              android:inputType="textPersonName"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintHorizontal_bias="0.267"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintVertical_bias="0.241" /&gt;
  
          &lt;Button
              android:id="@+id/button"
              android:layout_width="75dp"
              android:layout_height="45dp"
              android:layout_marginStart="8dp"
              android:layout_marginLeft="8dp"
              android:layout_marginTop="8dp"
              android:layout_marginEnd="8dp"
              android:layout_marginRight="8dp"
              android:layout_marginBottom="8dp"
              android:text="Add"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintHorizontal_bias="0.918"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintVertical_bias="0.238" /&gt;
  
          &lt;Button
              android:id="@+id/button2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginStart="8dp"
              android:layout_marginLeft="8dp"
              android:layout_marginTop="8dp"
              android:layout_marginEnd="8dp"
              android:layout_marginRight="8dp"
              android:layout_marginBottom="8dp"
              android:text="Search"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintHorizontal_bias="0.469"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintVertical_bias="0.946" /&gt;
      &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>
  
  <p>can a button in fragment be accessed from the main activity? and add an edit text below the existing edit text on click of the button Add in fragment?</p>