title: Android : Activity button shows up on fragment
body: <p>I have a simple application, with an Activity calling a Fragment.</p>
  
  <p>Question I have is .. why is the Activity's button showing up on the Fragment ?</p>
  
  <p><strong>Seems to be a very simple problem .. just not able to pin point the issue !!</strong></p>
  
  <p>Activity screenshot :</p>
  
  <p><a href="https://i.stack.imgur.com/Zvftm.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Zvftm.png" alt="enter image description here"></a></p>
  
  <p>Fragment Screenshot : </p>
  
  <p><a href="https://i.stack.imgur.com/upP3Z.png" rel="noreferrer"><img src="https://i.stack.imgur.com/upP3Z.png" alt="enter image description here"></a></p>
  
  <p>Notice that Activity's SUBMIT button shows up on the Fragment, but the TextView and EditText get hidden. <strong>Why ??</strong></p>
  
  <p>Activity :</p>
  
  <pre><code>package com.example.deep_kulshreshtha.toddsyndrome;
  
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.support.design.widget.FloatingActionButton;
  import android.support.design.widget.Snackbar;
  import android.support.design.widget.TextInputEditText;
  import android.support.v4.app.FragmentTransaction;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.View;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.widget.Button;
  import android.widget.EditText;
  
  public class MainActivity extends AppCompatActivity {
  
      private TextInputEditText inputEditText;
      private EditText editText;
      private ToddSyndromeDBHelper dbHelper;
      private SQLiteDatabase db;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          getSupportActionBar().setDisplayShowHomeEnabled(true);
  //        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  
  //        inputEditText = (TextInputEditText) findViewById(R.id.lastNameEditText);
          editText = (EditText) findViewById(R.id.editText);
  
          Button submitButton = (Button) findViewById(R.id.submitButton);
  
          FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
          fab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  
                  CreateReportFragment fragment = CreateReportFragment.newInstance();
  
                  FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                  transaction.replace(R.id.content_main, fragment, "CreateFragment");
                  transaction.addToBackStack("CreateBackStack");
                  transaction.commit();
              }
          });
  
          dbHelper = new ToddSyndromeDBHelper(this);
  
      }
  
      @Override
      protected void onStop() {
          super.onStop();
          if(db != null) {db.close();}
      }
  
      public SQLiteDatabase getDb(){
  
          if(db == null) {
  //            new ConnectionHelper().execute();
              db = dbHelper.getWritableDatabase();
          }
          return db;
      }
  
      public void viewPatientReport(View view){
  
          PatientReportFragment fragment = PatientReportFragment.
                  newInstance(editText.getText().toString());
  
          FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
          transaction.replace(R.id.content_main, fragment, "SearchFragment");
          transaction.addToBackStack("SearchBackStack");
          transaction.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.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
      private class ConnectionHelper extends AsyncTask&lt;Void, Void, SQLiteDatabase&gt;{
  
          @Override
          protected SQLiteDatabase doInBackground(Void... params) {
              db = dbHelper.getWritableDatabase();
              return db;
          }
      }
  }
  </code></pre>
  
  <p>Activity xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.design.widget.CoordinatorLayout 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"
      android:fitsSystemWindows="true"
      tools:context="com.example.deep_kulshreshtha.toddsyndrome.MainActivity"&gt;
  
      &lt;android.support.design.widget.AppBarLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:theme="@style/AppTheme.AppBarOverlay"&gt;
  
          &lt;android.support.v7.widget.Toolbar
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:background="?attr/colorPrimary"
              app:popupTheme="@style/AppTheme.PopupOverlay" /&gt;
  
      &lt;/android.support.design.widget.AppBarLayout&gt;
  
      &lt;include layout="@layout/content_main" /&gt;
  
      &lt;android.support.design.widget.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="bottom|end"
          android:layout_margin="@dimen/fab_margin"
          app:srcCompat="@android:drawable/ic_dialog_email" /&gt;
  
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>
  
  <p>Content xml : </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout 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/content_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:context="com.example.deep_kulshreshtha.toddsyndrome.MainActivity"
      tools:showIn="@layout/activity_main"&gt;
  
      &lt;TextView
          android:id="@+id/headline"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:textSize="32dp"
          android:fontFamily="cursive"
          android:text="@string/todd_syndrome" /&gt;
  
  &lt;!--    &lt;android.support.design.widget.TextInputLayout
          android:id="@+id/layout_last_name"
          android:layout_below="@id/headline"
          android:layout_centerHorizontal="true"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"&gt;
  
          &lt;android.support.design.widget.TextInputEditText
              android:id="@+id/lastNameEditText"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:hint="@string/edit_text_hint" /&gt;
      &lt;/android.support.design.widget.TextInputLayout&gt;--&gt;
  
      &lt;EditText
          android:id="@+id/editText"
          android:layout_below="@id/headline"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:hint="@string/edit_text_hint"/&gt;
  
      &lt;Button
          android:id="@+id/submitButton"
          android:layout_centerHorizontal="true"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/submit"
          android:layout_below="@id/editText"
          android:onClick="viewPatientReport"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Fragment : </p>
  
  <pre><code>package com.example.deep_kulshreshtha.toddsyndrome;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.sqlite.SQLiteDatabase;
  import android.net.Uri;
  import android.os.Bundle;
  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.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.CompoundButton;
  import android.widget.EditText;
  import android.widget.RadioButton;
  import android.widget.RadioGroup;
  import android.widget.Spinner;
  import android.widget.Switch;
  import android.widget.Toast;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class CreateReportFragment extends Fragment
          implements View.OnClickListener{
      // TODO: Rename parameter arguments, choose names that match
      // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
      private static final String ARG_PARAM1 = "param1";
      private static final String ARG_PARAM2 = "param2";
  
      // TODO: Rename and change types of parameters
      private String mParam1;
      private String mParam2;
  
  //    private OnFragmentInteractionListener mListener;
      String name;
      boolean hallucegenicDrugs = false;
      int age;
      String gender;
      boolean migraine = false;
  
      private EditText editText;
      private Switch switchMigraine;
      private Spinner ageSpinner;
      private RadioGroup group;
      private Switch switchHall;
      private Button saveButton;
  
      private View.OnClickListener radioListener = new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              boolean checked = ((RadioButton)v).isChecked();
              gender = ((RadioButton)v).getText().toString();
          }
      };
  
      public CreateReportFragment() {
          // Required empty public constructor
      }
  
      // TODO: Rename and change types and number of parameters
      public static CreateReportFragment newInstance(/*String param1, String param2*/) {
          CreateReportFragment fragment = new CreateReportFragment();
  //        Bundle args = new Bundle();
  //        args.putString(ARG_PARAM1, param1);
  //        args.putString(ARG_PARAM2, param2);
  //        fragment.setArguments(args);
          return fragment;
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          if (getArguments() != null) {
  //            mParam1 = getArguments().getString(ARG_PARAM1);
  //            mParam2 = getArguments().getString(ARG_PARAM2);
          }
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          View view = inflater.inflate(R.layout.fragment_create_report, container, false);
  
          editText = (EditText) view.findViewById(R.id.createPersonName);
          name = editText.getText().toString();
  
          switchMigraine = (Switch) view.findViewById(R.id.migraineToggle);
          switchMigraine.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  migraine = false;
              }
          });
  
          ageSpinner = (Spinner) view.findViewById(R.id.ageSpinner);
  
          List&lt;Integer&gt; list = new ArrayList&lt;&gt;();
          for (int i = 1; i &lt;= 100; i++){ list.add(i); }
          ArrayAdapter&lt;Integer&gt; adapter = new ArrayAdapter&lt;Integer&gt;(getContext(),
                  android.R.layout.simple_spinner_item, list);
          adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  
          ageSpinner.setAdapter(adapter);
          ageSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
              @Override
              public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                  age = (int) parent.getItemAtPosition(position);
              }
  
              @Override
              public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  
              }
          });
  
          group = (RadioGroup) view.findViewById(R.id.genderButton);
  
          RadioButton maleRadio = (RadioButton) view.findViewById(R.id.radioMale);
          maleRadio.setOnClickListener(radioListener);
          RadioButton femaleRadio = (RadioButton) view.findViewById(R.id.radioFemale);
          femaleRadio.setOnClickListener(radioListener);
  
          switchHall = (Switch) view.findViewById(R.id.switchButton);
          switchHall.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  hallucegenicDrugs = isChecked;
              }
          });
  
          saveButton = (Button) view.findViewById(R.id.saveButton);
          saveButton.setOnClickListener(this);
  
          return view;
      }
  
      @Override
      public void onClick(View v) {
          int syndromePercentage = 0;
  
          Log.v("Deep", "Patient name : " + name);
          Log.v("Deep", "Has migraine : " + migraine);
          Log.v("Deep", "Patient age : " + age);
          Log.v("Deep", "Patient gender : " + gender);
          Log.v("Deep", "Drugs ? : " + hallucegenicDrugs);
  
          if(migraine == true){ syndromePercentage += 25; }
          if(age &lt;= 15){ syndromePercentage += 25; }
          if(gender.equals("Male")){ syndromePercentage += 25; }
          if(hallucegenicDrugs == true){ syndromePercentage += 25; }
  
          SQLiteDatabase db = ((MainActivity)getActivity()).getDb();
  
          ContentValues values = new ContentValues();
          values.put(PatientTableContract.FeedEntry.COL_NAME_PATIENT_NAME, name);
          values.put(PatientTableContract.FeedEntry.COL_NAME_RISK, syndromePercentage);
  
          db.insert(PatientTableContract.FeedEntry.TABLE_NAME, null, values);
  
          Toast.makeText(getContext(), "Data saved successfully !", Toast.LENGTH_SHORT).show();
  
          editText.setText("");
          switchMigraine.setChecked(false);
          ageSpinner.setSelection(0);
          group.clearCheck();
          switchHall.setChecked(false);
      }
  
  }
  </code></pre>
  
  <p>Fragment xml:</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="com.example.deep_kulshreshtha.toddsyndrome.CreateReportFragment"
      android:background="@android:color/white"&gt;
  
      &lt;!-- TODO: Update blank fragment layout --&gt;
      &lt;android.support.design.widget.TextInputLayout
          android:id="@+id/nameLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"&gt;
  
          &lt;android.support.design.widget.TextInputEditText
              android:id="@+id/createPersonName"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:hint="@string/edit_text_hint" /&gt;
  
      &lt;/android.support.design.widget.TextInputLayout&gt;
  
  &lt;!--    &lt;TextView
          android:id="@+id/migraineText"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@id/nameLayout"
          android:text="@string/hint1"/&gt;--&gt;
  
      &lt;Switch
          android:id="@+id/migraineToggle"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/hint1"
          android:layout_below="@id/nameLayout"
          android:checked="false"
          android:layout_margin="10dp"
          android:padding="10dp" /&gt;
  
      &lt;TextView
          android:id="@+id/ageText"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@id/migraineToggle"
          android:text="@string/hint2"
          android:layout_margin="10dp"/&gt;
  
      &lt;Spinner
          android:id="@+id/ageSpinner"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:prompt="@string/hint2"
          android:layout_below="@id/migraineToggle"
          android:layout_toRightOf="@id/ageText"
          android:layout_margin="10dp"
          android:layout_marginLeft="20dp"/&gt;
  
  &lt;!--    &lt;TextView
          android:id="@+id/genderText"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@id/ageText"
          android:text="@string/hint3"/&gt;--&gt;
  
      &lt;RadioGroup
          android:id="@+id/genderButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:layout_below="@id/ageSpinner"
          android:checkedButton="@+id/radioMale"
          android:layout_margin="10dp"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Gender ?"
              android:layout_margin="10dp"/&gt;
  
          &lt;RadioButton
              android:id="@+id/radioMale"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Male"
              android:layout_margin="10dp"/&gt;
  
          &lt;RadioButton
              android:id="@+id/radioFemale"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Female"
              android:layout_margin="10dp"/&gt;
      &lt;/RadioGroup&gt;
  
  &lt;!--    &lt;TextView
          android:id="@+id/hallucinogenicText"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@id/genderText"
          android:text="@string/hint4"/&gt;
  
      &lt;ToggleButton
          android:id="@+id/hallucinogenicToggle"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_toRightOf="@id/hallucinogenicText"
          android:hint="@string/hint4" /&gt;--&gt;
  
      &lt;Switch
          android:id="@+id/switchButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@id/genderButton"
          android:text="@string/hint4"
          android:checked="false"
          android:layout_margin="10dp"/&gt;
  
      &lt;Button
          android:id="@+id/saveButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@id/switchButton"
          android:text="Submit"
          android:layout_margin="10dp"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Second fragment 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.example.deep_kulshreshtha.toddsyndrome.PatientReportFragment"
      android:background="@android:color/white"&gt;
  
      &lt;!-- TODO: Update blank fragment layout --&gt;
      &lt;TextView
          android:id="@+id/patientReport"
          android:layout_gravity="center_horizontal"
          android:layout_marginTop="40dp"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" /&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>Second Fragment : </p>
  
  <pre><code>package com.example.deep_kulshreshtha.toddsyndrome;
  
  import android.app.Activity;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.net.Uri;
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  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.TextView;
  import com.example.deep_kulshreshtha.toddsyndrome.PatientTableContract.FeedEntry;
  
  
  public class PatientReportFragment extends Fragment {
  
      private static final String ARG_PARAM1 = "param1";
      private String mParam1;
      private MainActivity activity;
  
      private String[] projection = {FeedEntry._ID, FeedEntry.COL_NAME_PATIENT_NAME,
              FeedEntry.COL_NAME_RISK};
      private String selection = FeedEntry.COL_NAME_PATIENT_NAME + " = ?";
  
  //    private OnFragmentInteractionListener mListener;
  
      public PatientReportFragment() {
          // Required empty public constructor
      }
  
      public static PatientReportFragment newInstance(String param1) {
          PatientReportFragment fragment = new PatientReportFragment();
          Bundle args = new Bundle();
          args.putString(ARG_PARAM1, param1);
          fragment.setArguments(args);
          return fragment;
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          if (getArguments() != null) {
              mParam1 = getArguments().getString(ARG_PARAM1);
          }
      }
  
      @Override
      public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
  
          TextView textView = (TextView) view.findViewById(R.id.patientReport);
  
          SQLiteDatabase db = ((MainActivity)getActivity()).getDb();
          Cursor cursor = db.query(FeedEntry.TABLE_NAME,
                  projection,
                  selection,
                  new String[]{mParam1},
                  null,
                  null,
                  null);
  
          if(cursor.getCount() == 0){
              textView.setText("No data found");
              return /*view*/;
          } else {
              cursor.moveToFirst();
              int riskPercent = cursor.getInt(cursor.getColumnIndex(FeedEntry.COL_NAME_RISK));
  
              textView.setText("Risk percentage : " + riskPercent );
              return /*view*/;
          }
  
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          ((MainActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  
          // Inflate the layout for this fragment
          View view =  inflater.inflate(R.layout.fragment_patient_report, container, false);
          return view;
      }
  
      @Override
      public void onAttach(Context context) {
          super.onAttach(context);
          activity = (MainActivity) context;
      }
  
  }
  </code></pre>