1

How do I get the value of my EditText that the user typed in from the function that was called when the onClick was pressed. I tried this below but its not working. Thanks for the help.

 <EditText android:id="@+id/myEditT" />

<Button android:text="MY BUTTON" android:onClick="GetThis" />

public void GetThis(View view) {


    EditText x = (EditText)parentView.findViewById( R.id.myEditT);

    // alert the x variable

 }
Andre Amaral Braga
  • 321
  • 1
  • 7
  • 23
Hello-World
  • 9,277
  • 23
  • 88
  • 154

5 Answers5

1

In XML,

<EditText android:id="@+id/myEditT"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"/>
<Button android:id="@+id/myButton"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"/>

In Java,

public void onCreate(Bundle saved){
    super.onCreate(saved);
    setContentView(R.layout.your_xml);
    Button btn = (Button) findViewById(R.id.myButton);
    EditText edtText = (EditText) findViewById(R.id.myEdit);
    btn.setOnClickListener(new onClickListener(
        public void onClick(View v){
            String value = edtText.getText().toString();
        }
    )); 
}
Pang
  • 9,564
  • 146
  • 81
  • 122
No_Rulz
  • 2,679
  • 1
  • 20
  • 33
1
EditText x = (EditText) findViewById(R.id.myEditT);
String your_text = x.getText().toString();
nKn
  • 13,691
  • 9
  • 45
  • 62
0
public void GetThis(View view) {
    EditText x = (EditText)view.findViewById(R.id.myEditT);
    String edittextvalue = x.getText().toString();
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Looking Forward
  • 3,579
  • 8
  • 45
  • 65
0
EditText x;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_xml);
    x = (EditText)parentView.findViewById( R.id.myEditT);
    public void GetThis(View view) {
    String s=x.getText().toString();
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Gayathiri
  • 427
  • 1
  • 4
  • 9
  • refer this link http://stackoverflow.com/questions/18807186/null-pointer-exception-getting-edittext-value – Gayathiri Jan 22 '14 at 13:10
0
EditText x = (EditText)findViewById(R.id.myEditT);

public void GetThis(View view) {
    String getEditTextValue = x.getText().toString();
    Toast.makeText(getApplicationContext(), getEditTextValue, Toast.LENGTH_LONG).show();
}
Pang
  • 9,564
  • 146
  • 81
  • 122
user3021522
  • 61
  • 1
  • 3
  • 10