title: how to change view to fragment programmatically (when internet not available)
body: <p>It's been all day, I'm trying to do what I thought it was a simple task: implementing a fragment that display different views based on internet availability.</p>
  
  <p>To be clear: try to start youtube app without connectivity, you will see a beautiful error screen with a 'Retry' button. When you click the button the fragment (of just the view, or something else) reloads and, if internet is now available, display the correct items.</p>
  
  <p>I have two fragments inside a ViewPager. I want the first fragment to display an error message and a 'Retry' button. When the button is pressed, just like the youtube app does, I want to reload the content of the fragments and display the correct stuff, if the network is available.</p>
  
  <p>I've tried replacing the entire fragment from inside itself and from the main activity, changing the view programmatically and other stuff by I hadn't any success.</p>
  
  <p>Please help!</p>
  
  <p><img src="https://i.stack.imgur.com/7En8h.png" alt="Youtube error screen"></p>
  
  <p>EDIT:</p>
  
  <p>I can display the error view, I just check the internet availability inside the OnCreateView method:</p>
  
  <pre><code>@Override
  public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
  
      final View view;
  
      if(CheckNetwork.isInternetAvailable(getActivity())){
  
          view = inflater.inflate(R.layout.fragment_with_connection, container, false);
      }
      else{
          view = inflater.inflate(R.layout.layout_no_connectivity, container, false);
          Button button = (Button)view.findViewById(R.id.button_retry);
          button.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View mview) {
  
  
                  //DUNNO WAT TO DO
              }
          });
  </code></pre>
  
  <p>}</p>
  
  <p>EDIT:
  I have used the answer by @daro2189 to solve my problem, who was slightly different: in this case I only need to change the view of ONE of two fragments inside a view pager.</p>
  
  <pre><code>@Override
  public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
  
      View view = inflater.inflate(R.layout.fragment_with_connection, container, false);
  
      initNoInternetView(view);
  
      if (!CheckNetwork.isInternetAvailable(getActivity())) {
  
          if (noInternetView != null) {
              View child = view.findViewById(R.id.child );
              child.setVisibility(View.GONE);
              noInternetView.setVisibility(View.VISIBLE);
          }
      } else {
  
          getInfoFromInternet();
      }
  
      return view;
  }
  
  private void initNoInternetView(final View view) {
      if(view == null)
          return;
  
  
      RelativeLayout parentView = (RelativeLayout)view.findViewById(R.id.layout_no_connectivity);
  
      LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
      noInternetView = inflater.inflate(R.layout.layout_no_connectivity, null);
      noInternetView.setVisibility(View.GONE);
      parentView.addView(noInternetView);
  
      final Button button = (Button)noInternetView.findViewById(R.id.button_retry);
      button.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View mview) {
              if(!CheckNetwork.isInternetAvailable(getActivity())){
                  if(noInternetView != null){
                      noInternetView.setVisibility(View.VISIBLE);
                      noInternetView.bringToFront();
                  }
              } else {
                  if(noInternetView != null){
                      noInternetView.setVisibility(View.GONE);
                      View child = view.findViewById(R.id.child);
                      child.setVisibility(View.VISIBLE);
                      getInfoFromInternet();
                  }
              }
          }
      });
  }
  </code></pre>