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
  • 8,457
  • 22
  • 78
  • 151

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,073
  • 146
  • 84
  • 117
No_Rulz
  • 2,684
  • 1
  • 17
  • 32
1
EditText x = (EditText) findViewById(R.id.myEditT);
String your_text = x.getText().toString();
nKn
  • 13,590
  • 9
  • 42
  • 59
0
public void GetThis(View view) {
    EditText x = (EditText)view.findViewById(R.id.myEditT);
    String edittextvalue = x.getText().toString();
}
Pang
  • 9,073
  • 146
  • 84
  • 117
Looking Forward
  • 4,200
  • 8
  • 46
  • 66
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,073
  • 146
  • 84
  • 117
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,073
  • 146
  • 84
  • 117
user3021522
  • 61
  • 1
  • 3
  • 10