enterText() in Robotium - android

i am new in android testing, I try to calculate two values using Edit Text. i tried in two ways. One way is solo.enterText(EditText edittext, String text); and another one is solo,enterText(int index, String text);
This one is working for me
//Access First value (editfiled) and putting firstNumber value in it
EditText FirsteditText = (EditText) solo.getView(R.id.EditText01);
solo.enterText(FirsteditText, String.valueOf("10"));
This one is not working for me
solo.enterText(0, "10");
whats the problem here?

From the documentation of Solo.enterText
index: the index of the EditText. 0 if only one is available
You'll need to use 1 for the fist EditText if there is more than one available in your current layout.

You can also try without creating first the EditText object :
solo.enterText((EditText) solo.getView(ID),value);
EG :
solo.enterText((EditText) solo.getView(R.id.number_one_value),"13");

You have two way to input Edit Text:
By EditText id.
EditText FirsteditText = (EditText) solo.getView(R.id.EditText01);
By Index. This index is the index of editText that displays in current activity. If there is only one editText, use 0. If there are more than 1 editText, use 1, 2...

use this statement:
String.valueOf(): in -- solo.enterText(0,String.valueOf("10"));
It worked for me.

Related

The char "\u200B" is automatically added to the EditText

I am working on an Android Application in which one I would like to compare some string values in EditText.
For example, in a first EditText, I start to entry "dav" and then select "David" from the keyboard suggestions. In a second EditText, I start to entry "dav", then select "David" from the keyboard suggestions and then correct the content to "Dav".
Every seems to be OK. If I retrieve the content of the EditText (with getEditableText().toString().trim()) the debugger tells me that "David" is a word composed by 5 characters and "Dav" a word composed by 3 characters.
If now I click on the EditText that contains "Dav" and I select "David" from the keyboard suggestions, the debugger tells me that the word "David" is composed by 6 characters. The last character is "\u200B".
Why this character is automatically add and how can I remove it in a generic way ?
Thank you for your help.
\u200B is a unicode character zero width space. It seems to me it's being added by the keyboard you are using. I assume if you change your keyboard it's possible you won't see that behavior.
One way to handle that is replacing that character and dealing with the actual String:
#Test
public void zero_space_character() {
String David = "David\u200B";
String theRealDavid = David.replace("\u200B", "");
assertNotEquals(David, theRealDavid);
assertEquals("David", theRealDavid);
}
It should be getText(). toString(). trim().

Is it possible to do TextView/EditView with permanent copy/paste cursors

I am designing an application, where I have fragment with text to selection and button. And I need to transmit selected text to next fragment, when I click on Button. Also, text must be always selectable ( with two cursors for selection). I try use TextView to do it: I make my TV selectable:
tvMain.setTextIsSelectable(true);
And now I can call cursors by long pressing on TV. But these is not, what I'm expecting. Cursors must be always visible and never hiding from text. Any ideas, how to do it?
You can use EditText with null background so that it look like TextView and do something like this:
tvMain.selectAll();
You can use
EditText text = (EditText) findViewById(R.id.EditText1);
int selectionStart = text.getSelectionStart();
int selectionEnd = text.getSelectionEnd();
And then get a substring from the start to the beginning
String selectedText = text.getText().toString().substring(selectionStart, selectionEnd);
You CAN use it with TextView too.

Android EditText - Capitalize first letter of sentences using setText()

Folks,
I need to capitalize first letter of every sentence. I followed the solution posted here
First letter capitalization for EditText
It works if I use the keyboard. However, if I use setText() to programatically add text to my EditText, first letter of sentences are not capitalized.
What am I missing? Is there a easy way to fix or do I need to write code to capitalize first letters in my string before setting it to EditText.
The only thing the inputType flag does is suggest to the input method (e.g. keyboard) what the user is attempting to enter. It has nothing to do with the internals of text editing in the EditText view itself, and input methods are not required to support this flag.
If you need to enforce sentence case, you'll need to write a method which does this for you, and run your text through this method before applying it.
You can use substring to make this
private String capSentences( final String text ) {
return text.substring( 0, 1 ).toUpperCase() + text.substring( 1 ).toLowerCase();
}
Setting inputType doesn't affect anything put into the field programmatically. Thankfully, programmatically capitalizing the first letter is pretty easy anyway.
public static String capFirstLetter(String input) {
return input.substring(0,1).toUpperCase() + input.substring(1,input.length());
}

How do i set equal characters in a line in a text view?

So basically by default the text view in android wraps contents because of which my text looks something like this
I'd like to disable the text wrapping property and set equal number of characters in the text view.
How do I do it?
Your question is not 100% clear but if you're talking about justification, Android doesn't support it. But here is a library which does.
If you literally don't want the text to wrap use:
android:singleline="true"
You have to use single line "true", and define the padding between the cells.
ps: to set a number of characters , you have get the refference from this textView and edit the content(just format the string).
Example:
char text[] = originalText.toCharArray();
String newText = "":
for(int i=0; i< text.length(); i++){
if(i<x){
newText =newText + text[i];
}
else{
break;
}
}
To achieve this, simply use the newline syntax "\n" where you want a new line to begin.
For example:
String text = "Who is the Boss? \n You are the Boss";
This can also be achieved programmatically of course in code if you're getting a string without one.
Simply write a method that checks for white space and insert the "\n" say after each successive 3 whitespaces have been detected. Then programmatically set the string to the TextView.
try adding this attribute to your textview and then try
android:gravity="center_horizontal

How To capitalize the letters in AutoCompleteTextView dynamically?

I am developing the application which consists of AutoCompleteTextView,here is my problem,How I can upper case the letters entering in AutoCompleteTextView.
I Don't want in xml: android:capitalize="characters"
I want to declare in Java code.
You can try like this..In your text watcher in ontextchanged change the text to upper case..and check if the new string in edittext is the old string which you converted to upper case...in order to avoid stackoverflow error..
String upper = mytextview.getText().toString().toUpperCase()
mytextview.setText(upper);
Try this in your code there are some other flags also which you can check and try.
tv.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);

Categories

Resources