Reputation: 6067
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?
Upvotes: 26
Views: 144771
Reputation: 25
Put this in your MainActivity:
{
public EditText bizname, storeno, rcpt, item, price, tax, total;
public Button click, click2;
int contentView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.main_activity );
bizname = (EditText) findViewById( R.id.editBizName );
item = (EditText) findViewById( R.id.editItem );
price = (EditText) findViewById( R.id.editPrice );
tax = (EditText) findViewById( R.id.editTax );
total = (EditText) findViewById( R.id.editTotal );
click = (Button) findViewById( R.id.button );
}
}
Put this under a button or something
public void clickBusiness(View view) {
checkPermsOfStorage( this );
bizname = (EditText) findViewById( R.id.editBizName );
item = (EditText) findViewById( R.id.editItem );
price = (EditText) findViewById( R.id.editPrice );
tax = (EditText) findViewById( R.id.editTax );
total = (EditText) findViewById( R.id.editTotal );
String x = ("\nItem/Price: " + item.getText() + price.getText() + "\nTax/Total" + tax.getText() + total.getText());
Toast.makeText( this, x, Toast.LENGTH_SHORT ).show();
try {
this.WriteBusiness(bizname,storeno,rcpt,item,price,tax,total);
String vv = tax.getText().toString();
System.console().printf( "%s", vv );
//new XMLDivisionWriter(getString(R.string.SDDoc) + "/tax_div_business.xml");
} catch (ReflectiveOperationException e) {
e.printStackTrace();
}
}
There! The debate is settled!
Upvotes: 1
Reputation: 419
in Kotlin 1.3
val readTextFromUser = (findViewById(R.id.inputedText) as EditText).text.toString()
This will read the current text that the user has typed on the UI screen
Upvotes: 0
Reputation: 291
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
Upvotes: 29
Reputation: 1254
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);
Upvotes: 4
Reputation: 5298
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.
Upvotes: 20
Reputation: 39
String fname = ((EditText)findViewById(R.id.txtFirstName)).getText().toString();
String lname = ((EditText)findViewById(R.id.txtLastName)).getText().toString();
((EditText)findViewById(R.id.txtFullName)).setText(fname + " "+lname);
Upvotes: 3
Reputation: 7594
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();
Upvotes: 13