title: How to extract text from an EditText view - Android
body: <p>So I guess It was a simple as using a getText() to retrieve the information :)</p>
  
  <p>Here is how it ended: </p>
  
  <pre><code>public void getToast(View v) {
      EditText et = (EditText) findViewById(R.id.userText);
      String toastText = et.getText().toString();
      if (toastText == "" || toastText == null) {
          Toast.makeText(this, "This is a nice toast!", Toast.LENGTH_SHORT).show();   
      }
      else {
      Toast.makeText(this, toastText, Toast.LENGTH_SHORT).show();
      }
  }
  </code></pre>
  
  <p>I have created an <em>EditText</em> View on the main layout file. This is referenced to with the <em>userText</em> identifier. As it is an <em>EditText</em> field, the user is able to modify the text within it at any moment; what I am trying to accomplish is to retrieve the text the user has entered at the moment they tap on the button identified as <em>getToast</em>, and then display it as a Toast. </p>
  
  <p>I am using the Resources class at the moment (my first guess?) to retrieve the String stored under <em>toastText</em> but this is useless, as it is extracting the text stored in the main.xml - which is empty, as I declared no "android:text" attribute for that view, instead I used an "android:hint" to tell the user to enter the text. </p>
  
  <p>I had read about intents, but that kind of makes sense if I was to send the String to another activity, not within the same one. I had thought of it as a simple task, but it is consuming more time that I had hope :P</p>
  
  <p>BTW: </p>
  
  <p>The <em>getToast</em> method is defined as an "android:OnClickMethod" for a button created on the XML. It will work with any other string of text. </p>
  
  <p>Any ideas?</p>
  
  <pre><code>package com.testlabs.one;
  
  import android.app.Activity;
  import android.content.res.Resources;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Toast;
  
  public class initialui extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main); 
  }
  
  public void getToast(View v) {
      Resources myResources = getResources();
      String toastText = myResources.getString(R.string.toast); 
      if (toastText == "" || toastText == null) {
          Toast.makeText(this, "This is a nice toast!", Toast.LENGTH_SHORT).show();   
      }
      else {
      Toast.makeText(this, toastText, Toast.LENGTH_SHORT).show();
      }
  }
  }
  </code></pre>