I have to get a EditText object which is nothing but a search bar in app with text visible as Current Location, however if I've already made a search query with myText, there is no Current Location text visible and search bar shows myText.
I am writing the test cases using Robotium solo object.
How can i write a conditional statement to get the EditText despite of what text it shows. Something like
if !found solo.getText("Current Location")
search solo.getText("myText");
This is what I am doing currently
EditText text = (EditText) solo.getText("Current Location");
if(text == null){
text = (EditText) solo.getText("myText");
//my rest of the code goes here....
But this throws exception if Current Location is not present in the search bar.
junit.framework.AssertionFailedError: TextView with text: 'Current Location' is not found!
Please suggest the correct way.
Try with this code:
if(!solo.searchText("Current Location"))
assertTrue(solo.searchText("my Text"))
else
assertTrue(solo.searchText("Current Location"));
EditText view = (EditText) solo.getView(view1);
if(view == null){
view = (EditText) solo.getView(view2)
}
view.getText().toString();
Related
So I have 6 edit texts and a button as shown below:
My question is how do I use the input from the EditTexts (which I have stored in content_main.xml) to do mathematical operations like calculating an average which I want to show up in a toast when the calculate button is pressed. I have already written some code in the MainActivity.java file that brings up a toast when the calculate button is pressed (also in content_main.xml), I just need to figure out how to use the inputs from the EditTexts in the toast.
EditText myText // = findViewById...
String text = myText.getText().toString();
What you should do first is to give each of its elements ID to also recognize from the Activity.
Then you should use the click event of the button
//Here it is referring to the id that gave his element in its layout
Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
And finally, like the button, get their input values EditText
//Here it is referring to the id that gave his element in its layout
EditText text = (EditText)findViewById(R.id.editText01);
And in order to do math, parse the string value remaining on a double (double for decimals can give the exact calculation if you want something, if you want to be an int approximately)
try{
Double value = Double.parseDouble(text);
}catch(NumberFormatException e){
//Message for error parse
}
I use setError() to show validation errors. In one case, the field is close to the left side of the screen, when the error message showed up, it visually pointed to the wrong EditText field - instead of pointing to the field showing 1234, it should really point to the blank field on the left, see attached screenshot. Does anyone know a solution to this? Thanks!
The screenshot was taken on a Galaxy 3 phone running Android 4.4.2.
I got one workaround, add \n to format the message and reduce the width of each line:
text1.setError("this field must\nbe 1 character\nin length");
This is the solution for your problem..
Try this..
EditText edt1=(EditText) findViewById(R.id.editText2);
EditText edt2=(EditText) findViewById(R.id.editText3);
EditText edt3=(EditText) findViewById(R.id.editText1);
// Reset errors.
edt1.setError(null);
edt2.setError(null);
edt3.setError(null);
String txt3 = edt3.getText().toString().trim();
String txt1 = edt1.getText().toString().trim();
String txt2=edt2.getText().toString().trim();
boolean cancel = false;
View focusView = null;
if (TextUtils.isEmpty(txt3)) {
edt3.setError(getString(R.string.error_field_required));
focusView = edt3;
cancel = true;
}
if (TextUtils.isEmpty(txt2)) {
edt2.setError(getString(R.string.error_field_required));
focusView = edt2;
cancel = true;
}
if (TextUtils.isEmpty(txt1)) {
edt1.setError(getString(R.string.error_field_required));
focusView = edt1;
cancel = true;
}
if (cancel) {
focusView.requestFocus();
} else {
//do the operations you want do here
}
I'm developing an app in Android and need to implement a common app function- accepting numeric input from the user via a EditText. After typing in this value, I want the user to be able to be select a 'go' button. A result should then appear below the first TextView. What should I use to display the result (TextView etc?) and how can I go about implementing this process. Thanks
You are new to this so First check d link http://www.tutorialspoint.com/android/android_event_handling.htm .
Here You have to define 1 TextView , 1 EditText and 1 Button
like
Button _go = (Button) findViewById(R.id._btngo);
than
_go.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView _getdata= (TextView) findViewById(R.id._tvresult);
EditText _result= (EditText) findViewById(R.id._etgetinput);
// get the input value and store as a string
String result = _result.getText().toString();
// display the result in textview
getdata.setText(result);
}
});
Hope you will find the solution .. :)
I am trying to get a message to appear when a button is clicked to tell the user to fill in the blank field. Currently, if the field is blank, it crashes/force closes the app. I tried to do the following code and had zero success. Originally I didn't have the if/else in there, I just ran the calculator(); method and the following imm code.
Could someone point me into the right direction?
public void onClick(View v)
{
if ((EditText)findViewById(R.id.amount1)== null)
{
Context context = getApplicationContext();
CharSequence text = "Enter a number";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else
{
calculator();
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
Im pretty sure this is the bad code:
if ((EditText)findViewById(R.id.amount1)== null)
Just dont know how to word it the way I want.
Try checking the length of the text in the EditText widget
EditText e = (EditText)findViewById(R.id.amount1));
if(e.getText().length == 0){
//Show Toast
}else{
//continue your code
}
Use this code.
EditText et = (EditText)findViewById(R.id.amount1));
if(et1.getText().length() == 0){
//Display toast here
} else{
//Your code
}
EditText text = (EditText)findViewById(R.id.amount1);
if(TextUtils.isEmpty(text.toString())) {
// show toast
}
Even if the field is blank, the edittext is not null. Use:
EditText editText = (EditText)findViewById(R.id.amount1);
String text = new String(editText.getText());
if (test.equals("")) {
//...
((EditText)findViewById(R.id.amount1)== null is just getting a reference to the EditText with the id amount1, it is not checking to see if that EditText has a valid entry.
To see if the EditText has text, you can get the String it holds by via EditText#getText().toString()
To make this work, first store the reference to the EditText in a var, then perform your checks on the String:
EditText et = (EditText)findViewById(R.id.amount1);
String amount1 = et.getText().toString();
if (amount1.equals("")) {
// Do your stuff here
}
I'm using local variables and just assuming you want the string to have content. You will likely need to do other checks to handle all the cases (like malformed input). Some of this you can reduce by setting the inputType on the EditText. For example, you might set it to numberDecimal if you are trying to handle only decimal numbers.
You actually want to check if the contents of the EditText are null or an empty string.
The line in question should look something like this:
if("".equals(((EditText)findViewById(R.id.amount1)).getText().toString()))
Of course you may want to break that statement up into more lines to make it a bit more readable!
i am trying to get the text input/values from an EditText widget as its being typed. please anybody know how or which method i should use? can't seem to find one that will help. thanks for your consideration.
i tried this but not working:
Entry = (EditText)findViewById(R.id.typeEntryId);
viewEntry = (TextView)findViewById(R.id.viewEntryId);
if(Entry != null){
//viewEntry.setText(Entry.getEditableText().toString());
viewEntry.setText(Entry.getText().toString());
}
Use a TextWatcher.
EditText field = (EditText) findViewById(R.id.myField);
if ( field != null ) {
Log.d ("myField",field.getText().toString());
}
you should be able to just do that. The getText() method returns an Editable type which implements CharSequence