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?
This question is related to
android
user-interface
android-edittext
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
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();
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
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);
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);
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!
Source: Stackoverflow.com