How to get text from EditText?
If you are doing it before the setContentView()
method call, then the values will be null.
This will result in null:
super.onCreate(savedInstanceState);
Button btn = (Button)findViewById(R.id.btnAddContacts);
String text = (String) btn.getText();
setContentView(R.layout.main_contacts);
while this will work fine:
super.onCreate(savedInstanceState);
setContentView(R.layout.main_contacts);
Button btn = (Button)findViewById(R.id.btnAddContacts);
String text = (String) btn.getText();
The quickest solution to your problem I believe is that you simply are missing parentheses on your getText
. Simply add ()
to edit.getText().toString()
and that should solve it
Place the following after the setContentView() method.
final EditText edit = (EditText) findViewById(R.id.Your_Edit_ID);
String emailString = (String) edit.getText().toString();
Log.d("email",emailString);
Well, it depends on your needs. Very often I keep my references to widgets in activity (as a class fields) - and set them in onCreate
method. I think that is a good idea
Probably the reason for your nulls is that you are trying to call findViewById()
before you set contentView()
in your onCreate()
method - please check that.