title: android username in action bar not displayed when page is loaded the first time
body: <p>Hi I am very new to Android and I am having some issues with putting the username (retrieved by using the web services) into the Action bar.</p>
  
  <p>The problem is that the username is not displayed and is shown as 'null' when I first enter the page that I want to show the username in. It is only displayed when the page is loaded the second time (i.e. it is only displayed when I go to another page and then come back to the page that I want to display the username in).</p>
  
  <p>I would like the username to be displayed in the action bar even when the page is loaded the first time, but I have no idea what the problem is.</p>
  
  <p>Can someone please help me to solve this issue?
  Thank you very much in advance.  </p>
  
  <p>Here is my code:</p>
  
  <p>In the 'menu', I have added in an item with <em>android:actionViewClass="android.widget.TextView"</em>:</p>
  
  <pre><code>&lt;item 
      android:id="@+id/viewusername"
      android:orderInCategory="1"
      android:title="@+id/viewusername"
      android:showAsAction="always"
      android:actionViewClass="android.widget.TextView"/&gt;
  </code></pre>
  
  <p>To put the username inside the actionbar, I have used the <em>onPrepareOptionsMenu</em> method:</p>
  
  <pre><code>@Override
      public boolean onPrepareOptionsMenu(Menu menu) 
      {
  
          MenuItem menuItem = menu.findItem(R.id.viewusername);
          TextView showusername1 = (TextView) menuItem.getActionView();
          showusername1.setText(String.valueOf(username) );
  
          return super.onPrepareOptionsMenu(menu);
      }
  </code></pre>
  
  <p>This is the method that I used to retrieve the username from the web services (I have used an EventHandler):</p>
  
  <pre><code>@Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
          setContentView(R.layout.fragment_dashboard);
          Button incident = (Button) findViewById(R.id.CreateNewIncidentButton);
          SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
          Editor editor = pref.edit();
          Bundle extras = getIntent().getExtras();
          if (extras != null) {
              name = extras.getString("ID");
          }
  
          incident.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {          
  
          // Switching to Register screen         
           Intent i = new Intent(getApplicationContext(), CreateIncident.class);          
           startActivity(i);          
           }          
           });            
           Button request = (Button) findViewById(R.id.CreateNewRequestButton);           
           request.setOnClickListener(new View.OnClickListener() {            
           public void onClick(View v) {
  
           // Switching to Create new request screen          
           Intent i = new Intent(getApplicationContext(), CreateRequest.class);       
           startActivity(i);          
           }          
           });            
           Button queue = (Button) findViewById(R.id.ViewMyTicketsButton);            
           queue.setOnClickListener(new View.OnClickListener() {          
           public void onClick(View v) {
  
           // Switching to Register screen        
           Intent i = new Intent(getApplicationContext(), ViewMyTickets.class);       
           startActivity(i);
           }      
           });
  
  
  
  
           getUsersName();
          }
  
          public void getUsersName() {        
                      methodName ="getUsersCombo";
               VectorString getUsersCombo = new VectorString(); //creates a new instance of VectorString
               String[] usersCom = { "combo_name"}; //array of values 
  
                   for (String s : usersCom) { //adds strings to getTicketInfo            
                   getUsersCombo.add(s);
  
                   }
  
          EventHandlerDashboard handler2 = new EventHandlerDashboard(this);   
          USD_WebService usdw2 = new USD_WebService(handler2,"http://dsl-116040f.datacom.co.nz:8080/axis/services/USD_R11_WebService?WSDL",60);
  
          SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
          Editor editor = pref.edit();
          int usersSid = pref.getInt("sid",0);
          String username = pref.getString("username","");
  
          try {
  
           //adds all the values to the web service call
           usdw2.doSelectAsync(usersSid, "cnt", "userid="+"'"+username+"'", -1, getUsersCombo);
  
          } catch (Exception e) {     
  
           e.printStackTrace();                       
          }                       
          }
  
  
          public void receiveResults(String result7){
              try {
                  DocumentBuilderFactory dbf =
                      DocumentBuilderFactory.newInstance();
                  DocumentBuilder db = dbf.newDocumentBuilder();
                  InputSource is = new InputSource();
                  is.setCharacterStream(new StringReader(result7));
  
                  Document doc = db.parse(is);
                  NodeList nodes = doc.getElementsByTagName("UDSObject");
  
                  // iterate the employees
                  for (int i = 0; i &lt; nodes.getLength(); i++) {
                     Element element = (Element) nodes.item(i);
  
                     NodeList name = element.getElementsByTagName("AttrName");
                     Element line = (Element) name.item(0);
                     System.out.println("ID: " + getCharacterDataFromElement(line));
  
                     NodeList title = element.getElementsByTagName("AttrValue");
                     line = (Element) title.item(0);
                     System.out.println("ID Value: " + getCharacterDataFromElement(line));
                     userID = getCharacterDataFromElement(line);
               username= "Welcome, "+userID;
  
                  }
              }
              catch (Exception e) {
                  e.printStackTrace();
              }
  
          System.out.println("users comboname"+result7);
  
          }
          public static String getCharacterDataFromElement(Element e) {
              Node child = e.getFirstChild();
              if (child instanceof CharacterData) {
                 CharacterData cd = (CharacterData) child;
                 return cd.getData();
              }
              return "?";
          }
  </code></pre>
  
  <p>Note: this method works perfectly fine when it is used in the TextView of the layout but it has issues when I try to use it in the action bar.</p>
  
  <p>Update: 
  It works now:</p>
  
  <pre><code>@Override
      public boolean onPrepareOptionsMenu(Menu menu) 
      {
  
          MenuItem menuItem = menu.findItem(R.id.viewusername);
          TextView showusername1 = (TextView) menuItem.getActionView();
          showusername1.setText(usersname==null?"":String.valueOf(usersname) );
  
          return super.onPrepareOptionsMenu(menu);
  
      }
  </code></pre>
  
  <p>And</p>
  
  <pre><code>         getUsersName();
          }
  
          public void getUsersName() {        
                      methodName ="getUsersCombo";
               VectorString getUsersCombo = new VectorString(); //creates a new instance of VectorString
               String[] usersCom = { "combo_name"}; //array of values 
  
                   for (String s : usersCom) { //adds strings to getTicketInfo            
                   getUsersCombo.add(s);
  
                   }
  
          EventHandlerDashboard handler2 = new EventHandlerDashboard(this);   
          USD_WebService usdw2 = new USD_WebService(handler2,"http://dsl-116040f.datacom.co.nz:8080/axis/services/USD_R11_WebService?WSDL",60);
  
          SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
          Editor editor = pref.edit();
          int usersSid = pref.getInt("sid",0);
           username = pref.getString("username","");
  
  
          try {
  
           //adds all the values to the web service call
           usdw2.doSelectAsync(usersSid, "cnt", "userid="+"'"+username+"'", -1, getUsersCombo);
  
          } catch (Exception e) {     
  
           e.printStackTrace();                       
          }                       
  
          }
  
  
  
  
  
  
          public void receiveResults(String result7){
              try {
                  DocumentBuilderFactory dbf =
                      DocumentBuilderFactory.newInstance();
                  DocumentBuilder db = dbf.newDocumentBuilder();
                  InputSource is = new InputSource();
                  is.setCharacterStream(new StringReader(result7));
  
                  Document doc = db.parse(is);
                  NodeList nodes = doc.getElementsByTagName("UDSObject");
  
                  // iterate the employees
                  for (int i = 0; i &lt; nodes.getLength(); i++) {
                     Element element = (Element) nodes.item(i);
  
                     NodeList name = element.getElementsByTagName("AttrName");
                     Element line = (Element) name.item(0);
                     System.out.println("ID: " + getCharacterDataFromElement(line));
  
                     NodeList title = element.getElementsByTagName("AttrValue");
                     line = (Element) title.item(0);
                     System.out.println("ID Value: " + getCharacterDataFromElement(line));
                     userID = getCharacterDataFromElement(line);
                     usersname = "Welcome, "+userID;
  
                  }
              }
              catch (Exception e) {
                  e.printStackTrace();
              }
  
          System.out.println("users comboname"+result7);
  
          invalidateOptionsMenu(); // This works on Android 3.x devices only
  
  
          }
          public static String getCharacterDataFromElement(Element e) {
              Node child = e.getFirstChild();
              if (child instanceof CharacterData) {
                 CharacterData cd = (CharacterData) child;
                 return cd.getData();
              }
              return "?";
          }
  </code></pre>