5

I'm having one EditText in android in which I want the user to enter the text I'll store the db. But, here I am not able to get the value from EditText.

Here is my code,

EditText etUserInfoNewValue = (EditText)findViewById(R.id.etUserInfoNewVal);    
String newValue = etUserInfoNewValue.getText().toString().trim();

How can I get the value from this EditText?

Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
Lavanya
  • 621
  • 4
  • 15
  • 28

5 Answers5

8

Put

String newValue = etUserInfoNewValue.getText().toString().trim(); 

Inside the button's onClicklistener().

You have put this code in onCreate() just after declaring it, it won't have any value at all. So you will get null value.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
4

Works fine for me,Check on your case..

Button   buttonTest;  
EditText editText;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    buttonTest = (Button)findViewById(R.id.button);
    editText   = (EditText)findViewById(R.id.edittext); //check this point carefully on your program

    buttonTest.setOnClickListener(
        new View.OnClickListener()
        {
            public void onClick(View view)
            {
                Log.v("EditText..", editText.getText().toString().trim());
            }
        });
}
ridoy
  • 6,274
  • 2
  • 29
  • 60
2

May be you need TextView.OnEditorActionListener. BluetoothChat http://developer.android.com/tools/samples/index.html

//BluetoothChat.java:

// Initialize the compose field with a listener for the return key
EditText mOutEditText = (EditText) findViewById(R.id.edit_text_out);
mOutEditText.setOnEditorActionListener(mWriteListener);

// The action listener for the EditText widget, to listen for the return key
private TextView.OnEditorActionListener mWriteListener =
new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    // If the action is a key-up event on the return key, send the message
    if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
        String message = view.getText().toString();
        sendMessage(message);
    }
    if(D) Log.i(TAG, "END onEditorAction");
    return true;
}

};

Kiryl Ivanou
  • 1,015
  • 2
  • 12
  • 22
0

When you will store the EditText String to Database. By seeing at your code it looks that you are immediately storing the Text in String newValue and then storing it in the Database. Change the code of String declaration to onClick() of your button or anything else.

TNR
  • 5,839
  • 3
  • 33
  • 62
0

Get value from editText,

String getValueEditText = "null";
if (etUserInfoNewValue.getText().toString().trim().length() > 0) {
    getValueEditText = etUserInfoNewValue.getText().toString().trim();
}
Najib.Nj
  • 3,706
  • 1
  • 25
  • 39