Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get text from EditText?

Tags:

The question is quite simple. But I want to know where exactly do we make our references to the gui elements? As in which is the best place to define:

final EditText edit =  (EditText) findViewById(R.id.text_xyz);  edit.getText.tostring(); 

When I try it doing inside the default oncreate() I get null values. So for best practice, do u recommend a separate class for referring these already defined gui elements in main.xml. From here we can call various methods of these elements like gettext or settext?

like image 346
Bohemian Avatar asked Dec 29 '09 12:12

Bohemian


People also ask

How do I get text from EditText?

Now, before we start we need to know some method of EditText which we use to get or fetch the text written in an EditText that is . getText() method and the other one is . setText() method that we use to set some pre-written text or string. EditText demo; demo=(EditText)findViewById(R.


2 Answers

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

like image 121
Jason Avatar answered Nov 10 '22 00:11

Jason


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.

like image 45
Ramps Avatar answered Nov 09 '22 22:11

Ramps