title: ViewStub is Not Inflating and Layout Remains Blank - Android
body: <p>I am working through the sample code that is generated when you start a <code>Navigation Drawer Activity</code> in Android Studio.  What I am trying to do is change an included view programatically and my research has shown that ViewStub is the best way to do this.  However I cannot get it working.  For the main layout in question there are 3 files involved:</p>
  
  <p><strong>activity_main.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v4.widget.DrawerLayout
      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/drawer_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      tools:openDrawer="start"&gt;
  
      &lt;include
          layout="@layout/app_bar_main"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  
      &lt;android.support.design.widget.NavigationView
          android:id="@+id/nav_view"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:fitsSystemWindows="true"
          app:headerLayout="@layout/nav_header_main"
          app:menu="@menu/activity_main_drawer" /&gt;
  
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>
  
  <p><strong>app_bar_main.xml:</strong></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.MY_APP.android.MainActivity"&gt;
  
      &lt;android.support.design.widget.AppBarLayout
          android:layout_height="wrap_content"
          android:layout_width="match_parent"
          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;ViewStub
          android:id="@+id/main_content"
          android:inflatedId="@id/main_content"
          android:layout="@layout/content_main"
          android:layout_height="match_parent"
          android:layout_width="match_parent"/&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"
          android:src="@android:drawable/ic_dialog_email" /&gt;
  
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>
  
  <p><strong>content_main.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"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:showIn="@layout/app_bar_main"
      tools:context="com.MY_APP.android.MainActivity"&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/content_textview"
          android:text="//TODO Main Feed"/&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Following is my MainActivity.java. Nothing seems to be happening and content_main never seems to show up.  Could it be a visibility issue?</p>
  
  <pre><code>package com.myapp.android;
  
  
  import android.app.Dialog;
  import android.content.Context;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.support.design.widget.NavigationView;
  import android.support.v4.view.GravityCompat;
  import android.support.v4.widget.DrawerLayout;
  import android.support.v7.app.ActionBarDrawerToggle;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.MenuItem;
  import android.view.ViewStub;
  import android.widget.Toast;
  
  import com.facebook.FacebookSdk;
  import com.facebook.login.LoginManager;
  import com.google.android.gms.common.ConnectionResult;
  import com.google.android.gms.common.GooglePlayServicesUtil;
  
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.util.ArrayList;
  
  public class MainActivity
          extends AppCompatActivity
          implements NavigationView.OnNavigationItemSelectedListener {
  
  
      NetworkHelpers NetworkHelper = new NetworkHelpers();
      SharedPreferences sharedpreferences;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
  
          //Check if user is connected
          if(!NetworkHelper.checkConnectivity(this)){
              NetworkHelper.showNotConnectedAlert(this);
          }
  
          //Check for Google Play Services
          checkGooglePlayServicesAvailable();
  
          //Get User Info From Database
          sharedpreferences = getSharedPreferences("com.myapp.android.prefs", Context.MODE_PRIVATE);
          String login_token = sharedpreferences.getString("login_token", "");
          String first_name = sharedpreferences.getString("first_name", "");
          String last_name = sharedpreferences.getString("last_name", "");
          String email = sharedpreferences.getString("email", "");
  
  
          if (login_token.isEmpty()){
              //Start LoginActivity
              Intent intent = new Intent(this, LoginActivity.class);
              intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
              finish();
              startActivity(intent);
          }else{
              //Update user info
              ArrayList&lt;String&gt; params = new ArrayList&lt;String&gt;();
              params.add(login_token);
              new getUserInfo().execute(params);
          }
  
          //Create View
          super.onCreate(savedInstanceState);
          setContentView(com.myapp.android.R.layout.activity_main);
  
          //Set Toolbar and Navigation Drawer
          Toolbar toolbar = (Toolbar) findViewById(com.myapp.android.R.id.toolbar);
          setSupportActionBar(toolbar);
          DrawerLayout drawer = (DrawerLayout) findViewById(com.myapp.android.R.id.drawer_layout);
          ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
              this, drawer, toolbar, com.myapp.android.R.string.navigation_drawer_open, com.myapp.android.R.string.navigation_drawer_close);
          drawer.setDrawerListener(toggle);
          toggle.syncState();
          NavigationView navigationView = (NavigationView) findViewById(com.myapp.android.R.id.nav_view);
          navigationView.setNavigationItemSelectedListener(this);
  
          ViewStub stub = (ViewStub) findViewById(R.id.main_content);
          stub.setLayoutResource(R.layout.content_main);
          stub.inflate();
  
      }
  
      @Override
      public void onBackPressed() {
          DrawerLayout drawer = (DrawerLayout) findViewById(com.myapp.android.R.id.drawer_layout);
          if (drawer.isDrawerOpen(GravityCompat.START)) {
              drawer.closeDrawer(GravityCompat.START);
          } else {
              super.onBackPressed();
          }
      }
  
      @SuppressWarnings("StatementWithEmptyBody")
      @Override
      public boolean onNavigationItemSelected(MenuItem item) {
          int id = item.getItemId();
  
          if (id == R.id.nav_home) {
              // Start Home Screen
          } else if (id == R.id.nav_videos) {
              // Start Videos Screen
          } else if (id == R.id.nav_photos) {
              // Start Photos Screen
          } else if (id == R.id.nav_galleries) {
              // Start Galleries Screen
          } else if (id == R.id.nav_map) {
              // Start Map Screen
          } else if (id == com.myapp.android.R.id.nav_share) {
              //Open share app dialog
              Intent intent = new Intent(Intent.ACTION_SEND);
              intent.setType("text/plain");
              intent.putExtra(Intent.EXTRA_SUBJECT, "My App");
              intent.putExtra(Intent.EXTRA_TEXT, "Check out My App: http://myapp.com/app");
              Intent chooser = Intent.createChooser(intent, "Tell a friend about My App");
              startActivity(chooser);
          } else if (id == com.myapp.android.R.id.nav_logout){
              logout();
          }
  
          DrawerLayout drawer = (DrawerLayout) findViewById(com.myapp.android.R.id.drawer_layout);
          drawer.closeDrawer(GravityCompat.START);
          return true;
      }
  
      //AsyncTask to get user info from api
      class getUserInfo extends AsyncTask&lt;ArrayList&lt;String&gt;, Void, String&gt; {
          @Override
          protected String doInBackground(ArrayList&lt;String&gt;... params) {
              String token = params[0].get(0);
              String response = HttpRequest.post("http://api.myapp.com/users/get-info/").send("api_key=API_KEY&amp;token="+token).body();
              return response;
          }
  
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
          }
  
          @Override
          protected void onPostExecute(String result) {
              try {
                  // Do JSON Stuff
                  JSONObject jsonobject = new JSONObject(result);
                  String status = jsonobject.get("status").toString();
                  String message = jsonobject.get("message").toString();
                  JSONObject userinfo = jsonobject.getJSONObject("user_info");
  
                  //Set up user variables
                  String first_name = userinfo.get("first_name").toString();
                  String last_name = userinfo.get("last_name").toString();
                  String email = userinfo.get("email").toString();
                  String display_name = userinfo.get("display_name").toString();
                  String about = userinfo.get("about").toString();
                  String newsletter = userinfo.get("newsletter").toString();
  
  
                  if (status.equals("success")){
                      updateSharedPreferences(first_name, last_name, email, display_name, about, newsletter);
                  }else if (status.equals("error")){
                      logout();
                  } else{
                      logout();
                  }
  
              } catch (JSONException e) {
                  Toast.makeText(getApplicationContext(),"An unknown error occurred.", Toast.LENGTH_SHORT).show();
                  logout();
              }
          }
      }
  
      private boolean checkGooglePlayServicesAvailable() {
          final int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
          if (status == ConnectionResult.SUCCESS) {
              return true;
          }
  
          if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
              final Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(status, this, 1);
              if (errorDialog != null)
              {
                  errorDialog.show();
              }
          }
          return false;
      }
  
      private void updateSharedPreferences(String first_name, String last_name, String email, String display_name, String about, String newsletter){
          //Put user info into sharedpreferences
          SharedPreferences sharedpreferences = getSharedPreferences("com.myapp.android.prefs", Context.MODE_PRIVATE);
          SharedPreferences.Editor editor = sharedpreferences.edit();
          editor.putString("first_name", first_name);
          editor.putString("last_name", last_name);
          editor.putString("email", email);
          editor.putString("display_name", display_name);
          editor.putString("about", about);
          editor.putString("newsletter", newsletter);
          editor.commit();
      }
  
      private void logout(){
          //Logout of Facebook
          FacebookSdk.sdkInitialize(this);
          LoginManager.getInstance().logOut();
  
          //Unset shared preferences login token
          SharedPreferences sharedpreferences = getSharedPreferences("com.myapp.android.prefs", Context.MODE_PRIVATE);
          SharedPreferences.Editor editor = sharedpreferences.edit();
          editor.putString("login_token", "");
          editor.putString("facebook_token", "");
          editor.putString("google_token", "");
          editor.commit();
  
          //Open login screen
          Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
          finish();
          startActivity(intent);
      }
  }
  </code></pre>