title: How to add admob banner in a fragment view?
body: <p>I am new to android development and I am confused about how to add admob banner to a fragment activity(I think its calling fragment). I am modifying a source code.</p>
  
  <p>I have a layout file called fragment_ftp.xml and its as below </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"
      xmlns:ads="http://schemas.android.com/apk/res-auto"&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:text=""
          android:id="@+id/statusText"
          android:layout_above="@+id/startStopButton"
          android:layout_centerHorizontal="true"
          android:layout_marginBottom="99dp" /&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:text=""
          android:id="@+id/ftpAddressText"
          android:layout_below="@+id/startStopButton"
          android:layout_centerHorizontal="true" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/start_ftp"
          android:id="@+id/startStopButton"
          android:layout_centerVertical="true"
          android:layout_centerHorizontal="true" /&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:textColor="@color/primary_red"
          android:text=""
          android:id="@+id/warningText"
          android:layout_above="@+id/startStopButton"
          android:layout_centerHorizontal="true" /&gt;
  
      &lt;ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/ftp_image"
          android:layout_above="@+id/warningText"
          android:layout_centerHorizontal="true" /&gt;
      &lt;RelativeLayout
          android:id="@+id/relativeLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"&gt;
  
          &lt;com.google.android.gms.ads.AdView
              android:id="@+id/adView"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              ads:adSize="SMART_BANNER"
              ads:adUnitId="@string/banner_home_footer" /&gt;
      &lt;/RelativeLayout&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>I had added the admob banner and in the preview, I can see that. Please check the below screenshot.
  <a href="https://i.stack.imgur.com/xtuyf.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xtuyf.jpg" alt="enter image description here"></a></p>
  
  <p>But the issues is that, Where should I write the java code for this admob banner. I don't have a corresponding activity for this fragment_ftp.xml.</p>
  
  <p>Searching for fragment_ftp, I could see only a java file as follows.
  FTPServerFragment.java</p>
  
  <pre><code>package com.filename.fragments;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.content.SharedPreferences;
  import android.net.ConnectivityManager;
  
  
  public class FTPServerFragment extends Fragment {
  
      TextView statusText,warningText,ftpAddrText;
      Button ftpBtn;
      Futils utils = new Futils();
      private MainActivity mainActivity;
      private View rootView;
      private BroadcastReceiver mWifiReceiver = new  BroadcastReceiver() {
  
          @Override
          public void onReceive(Context context, Intent intent) {
              ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
              NetworkInfo netInfo = conMan.getActiveNetworkInfo();
              if (netInfo != null &amp;&amp; netInfo.getType() == ConnectivityManager.TYPE_WIFI){
                  warningText.setText("");
              }
              else{
                  stopServer();
                  statusText.setText(utils.getString(getContext(),R.string.ftp_status_not_running));
                  warningText.setText(utils.getString(getContext(),R.string.ftp_no_wifi));
                  ftpAddrText.setText("");
                  ftpBtn.setText(utils.getString(getContext(),R.string.start_ftp));
              }
          }
      };
      private BroadcastReceiver ftpReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              String action = intent.getAction();
              if(action == FTPService.ACTION_STARTED) {
                  statusText.setText(utils.getString(getContext(), R.string.ftp_status_running));
                  warningText.setText("");
                  ftpAddrText.setText(getFTPAddressString());
                  ftpBtn.setText(utils.getString(getContext(),R.string.stop_ftp));
              }
              else if(action == FTPService.ACTION_FAILEDTOSTART){
                  statusText.setText(utils.getString(getContext(),R.string.ftp_status_not_running));
                  warningText.setText("Oops! Something went wrong");
                  ftpAddrText.setText("");
                  ftpBtn.setText(utils.getString(getContext(),R.string.start_ftp));
              }
              else if(action == FTPService.ACTION_STOPPED){
                  statusText.setText(utils.getString(getContext(),R.string.ftp_status_not_running));
                  ftpAddrText.setText("");
                  ftpBtn.setText(utils.getString(getContext(),R.string.start_ftp));
              }
          }
      };
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setHasOptionsMenu(false);
  
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          rootView = inflater.inflate(R.layout.fragment_ftp,container,false);
  //        return inflater.inflate(R.layout.article_view, container, false);
          statusText =(TextView) rootView.findViewById(R.id.statusText);
          warningText = (TextView) rootView.findViewById(R.id.warningText);
          ftpAddrText = (TextView) rootView.findViewById(R.id.ftpAddressText);
          ftpBtn = (Button) rootView.findViewById(R.id.startStopButton);
  
          SharedPreferences Sp = PreferenceManager.getDefaultSharedPreferences(getContext());
          int th = Integer.parseInt(Sp.getString("theme", "0"));
          // checking if theme should be set light/dark or automatic
          int theme1 = th == 2 ? PreferenceUtils.hourOfDay() : th;
          ImageView ftpImage = (ImageView)rootView.findViewById(R.id.ftp_image);
  
          //light theme
          if(theme1 == 0){
              ftpImage.setImageResource(R.drawable.ic_ftp_light);
          }else{
              //dark
              ftpImage.setImageResource(R.drawable.ic_ftp_dark);
          }
          ftpBtn.setOnClickListener(new View.OnClickListener(){
  
              @Override
              public void onClick(View v) {
                  if(!FTPService.isRunning()){
                      if(FTPService.isConnectedToWifi(getContext()))
                          startServer();
                      else
                          warningText.setText(utils.getString(getContext(),R.string.ftp_no_wifi));
                  }
                  else{
                      stopServer();
                  }
              }
          });
  
          return rootView;
      }
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          setRetainInstance(true);
          mainActivity=(MainActivity)getActivity();
          mainActivity.setActionBarTitle(utils.getString(getActivity(), R.string.ftp));
          mainActivity.floatingActionButton.hideMenuButton(true);
          mainActivity.buttonBarFrame.setVisibility(View.GONE);
          mainActivity.supportInvalidateOptionsMenu();
      }
      @Override
      public  void onDestroy(){
          super.onDestroy();
      }
  
      private void startServer() {
          getContext().sendBroadcast(new Intent(FTPService.ACTION_START_FTPSERVER));
      }
  
      private void stopServer() {
          getContext().sendBroadcast(new Intent(FTPService.ACTION_STOP_FTPSERVER));
      }
  
      @Override
      public void onResume(){
          super.onResume();
          updateStatus();
          IntentFilter wifiFilter = new IntentFilter();
          wifiFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
          getContext().registerReceiver(mWifiReceiver,wifiFilter);
          IntentFilter ftpFilter = new IntentFilter();
          ftpFilter.addAction(FTPService.ACTION_STARTED);
          ftpFilter.addAction(FTPService.ACTION_STOPPED);
          ftpFilter.addAction(FTPService.ACTION_FAILEDTOSTART);
          getContext().registerReceiver(ftpReceiver,ftpFilter);
      }
  
      @Override
      public void onPause(){
          super.onPause();
          getContext().unregisterReceiver(mWifiReceiver);
          getContext().unregisterReceiver(ftpReceiver);
      }
      private void updateStatus(){
          if(FTPService.isRunning()){
              statusText.setText(utils.getString(getContext(),R.string.ftp_status_running));
              ftpBtn.setText(utils.getString(getContext(),R.string.stop_ftp));
              ftpAddrText.setText(getFTPAddressString());
          }
          else{
              statusText.setText(utils.getString(getContext(),R.string.ftp_status_not_running));
              ftpBtn.setText(utils.getString(getContext(),R.string.start_ftp));
          }
      }
      private String getFTPAddressString(){
          return "ftp://"+FTPService.getLocalInetAddress(getContext()).getHostAddress()+":"+FTPService.getPort();
      }
  }
  </code></pre>
  
  <p>I this the java page where I shall write the admob call? </p>