I hate asking questions unless I absolutely have to, but I can't seem to find a clear answer to this problem.
I'm working on a practice app and one of the activities should take user input via an EditText view, and then return that input to the activity's TextView view, i.e. it should say hello back to the user after the user has typed their name in.
Instead of returning the user input, it gives me back this strange string of characters for some reason. Here's my code for reference:
text = (TextView) findViewById(R.id.mTextView);
input = (EditText) findViewById(R.id.mEditText);
enter = (Button) findViewById(R.id.mButton);
enter.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { text.setText("Hello " + input);
}
});
}
So rather than returning "Hello (user input)", it outputs something like:
Hello android.support.v7.widget.AppCompatEditText{3f5e428a .. So I'm confused as all hell right now.
Help!
P.S. - I would've posted screenshots but apparently I need 10 reputation points before I can do that, so hopefully this is enough..
If you want the string value from an EditText:
String s = input.getText().toString()
You can put that into the TextView.
input is an EditText, not the actual text thats been typed into the view.
You need to call input.getText() to get the text in it.
The value that you are getting is the default behavior of toString on the EditText.
Related
I want the user to type in his or her Facebook account-link (don't have a better solution atm).
Now, when the user clicks the edittext it is supposed to say : "www.facebook.com/". Now the cursor is supposed to be at the END of the edittext (after the "/") and the user is not supposed to delete the first letters so that the "www.facebook.com/" stays exactly where it is. This will have the user to ONLY type in his or her facebook name and therefore connect the profile.
Is there a way of doing this?
Thank you :)
You can do that by using the event "TextChanged" and in your code validate if the size is big than your string, like that:
if (((EditText)sender).Text.Length >= 17)
{
((EditText)sender).Text = e.NewTextValue;
}
else
{
((EditText)sender).Text = "www.facebook.com/";
}
So if the value is bigger than your string you will replace that for the value, if not you just set the value with your string
I am trying to write some automated tests with Appium for Android for Wordpress Mobile (https://github.com/wordpress-mobile/WordPress-Android).
The first thing I'm trying to do is type the username in the main login screen to be able to login to my Wordpress site, and I have a problem with SendKeys on the "username" field.
Here is how the element is seen in the uiautomatorviewer:
Here is what I have tried so far:
List<WebElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText");
WebElement login = textFieldsList.get(0);
login.sendKeys("username");
And:
driver.findElementsByClassName("android.widget.EditText").get(0).sendKeys("username");
And:
driver.findElement(By.xpath("//android.widget.EditText[#text='Editing. Username or email. ']")).sendKeys("username");
With all 3 versions of trying to send "username" as the username, when I run the test, what is actually typed in the field is: "Editing. Username or email. username"
So, it seems that the placeholder text is also kept, then my username is added.
NOTE that the text that is added when I send the username with appium is not there in the first place (see screenshot), but in the UI tree view, that appears to be the text in the EditText. When Appium is running the test, it is actually writing the "Editing. Username or email" text before adding my own username.
I have also tried, as suggested in one of the answers for a different question here: Appium : Clear a field
the following code, where the sendKeyEvent(67) should clear the field:
List<WebElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText");
WebElement login = textFieldsList.get(0);
login.click();
driver.sendKeyEvent(67);
login.sendKeys("username");
Using .clear() crashes and I have noticed that others suggested avoiding it if possible.
Of course, if I try to do this manually, the placeholder text is not added, I can just add my username in the field by typing it.
I can also use the driver.sendKeyEvent() function and send the characters one my one, but I would like to send the username as a parameter and be able to type any username in the field.
Because the extra text is typed every time I try to type the username, to workaround this I have to first type "username" - in the app, the actual text that is typed is "Editing. Username or email. username" - then move the cursor left in front of the word "username" and start deleting the rest - but this is EXTREMELY slow. Here is the code that does work this way:
String setUsername = "username";
login.click();
login.sendKeys(setUsername);
// hack to delete extra text that gets typed
int stringLength = login.getText().length() - setUsername.length();
for (int i = 0; i < setUsername.length(); i++) {
driver.sendKeyEvent(21); //KEYCODE_DPAD_LEFT
}
for (int i = 0; i < stringLength; i++) {
driver.sendKeyEvent(67); // "KEYCODE_DEL
}
What am I missing? Any help would be greatly appreciated. I am trying to understand why the extra text gets typed.
After a lot of searching, I found that this is really a bug in Appium v 1.2.2:
https://discuss.appium.io/t/android-appium-1-2-2-sendkeys-issue-with-hinted-edit-text/309
Hopefully, as it is said there, it will be fixed in version 1.2.3.
Thanks for all your help!
I see that you have used a List and there are 3 text fields on your screenshot - why not use an index of those to pass your value? This way it won't add extra text to it.
List<WebElement> tfl=driver.findElements(By.className("android.widget.EditText"));
tfl.get(0).sendKeys("username"); // tfl=text field list
tfl.get(1).sendKeys("password");
Problem is you are using sendKeyEvent(67) only one time and that too at no proper place. What you have to do is first set your cursor at the end of the Text in Login field and then keep deleting each alphabet one by one until no more text is left in the field.
Eg: Try something like this
WebElement login = textFieldsList.get(0);// Get the webelement where text is to be entered
string str = login.Text;// it will store the Text already written in login field
int textLength = str.Length;// find the lenght of the text that is to be removed
int x = login.Location.X +(textLength * 40); //
int y = login.Location.Y;//
TouchAction action = new TouchAction(driver);// TouchAction class is defined in appium library
action.Tap(testObject, x, y);//
for (int delete = 0; delete < str.Length; delete++)
{
rm.KeyEvent(67);
}
}
Hope it helps..
You're looking for
MobileElement textfield = driver.findElement(By.however(value))
textfield.setValue('foo')
I haven't tested that function on Android, but it was built because textfield.sendKeys is unreliable on iOS/Instruments.
You could also try textfield.click(), textfield.clear(), and then textfield.sendKeys('foo')
Example usage
You'll need the Appium java-client
You might want to downgrade one version as I think this bug doesn't exist in the last version.
i know highlighting in text-view is possible
Highlight Textview Using EditText
and scrolling text-view is also possible
(got the scroll code from here and is successfully scrolling too)
textView scroll at first line
now the question is, i am searching and i want to highlight that text and navigate to it, when someone presses the search button, the highlighting part is perfect, now i can get the index of the word in the string, but not line number of the string in the text-view,
point is if i want to find a position of certain text in text-view, i.e. which line number is that on, how to do it ?
i found an answer for this, but later i realized its for iOS
Search occurrences of a string in a TextView
Correct if I am wrong, you want to know the position where a particular text is in a String? If so then you can do it by using the following
String text = "0123hello9012hello8901hello7890";
String word = "hello";
Log.d("startOfWordPosition",text.indexOf(word)); // prints "4"
Log.d("endOfWordPosition",text.lastIndexOf(word)); // prints "22"
As you see that it will tell you the position as to where the word is located but you have to think about case where a word may come more than once. If you are sure that word will occur only once then above code is perfect for you. If not, then you will have to somehow tackle the problem.
This is working for loading a file into a textview so that the user's last selection or cursor position is at the top of the screen.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
page.setSelection(pos, pos2);
Layout layout = page.getLayout();
scroll.scrollTo(0, layout.getLineTop(layout.getLineForOffset(pos2))); }
}, 500);
page is a TextView, pos and pos2 are ints and the two ends of the last selection by the user (they are the same int if it's just the cursor), scroll is a scrollview containing the textview. It is all in a Handler because of delay issues internal to Android's loading all these objects. Th filename, pos and pos2 are saved as settings on exit.
we are doing android chat application and want to save the values in the edit text(name of the user) to our database.When he updates it,the update should reflect in the database also.Any help in this regard will be really appreciable.
Thanks in advance.
Your question isn't very clear, but hopefully this will help:
Getting text from an EditText:
Get a handle on your EditText, then call the EditTexts .getText() method when the user presses the send button, or however your app works. So at the start of your activity:
EditText message;
protected void onCreate(Bundle savedInstanceState) {
//set layout
setContentView(R.layout.your_layout);
//Get EditText from id
message = (EditText) findViewById(R.id.edit_text_id);
}
Then in your send button's onclick method:
String my_message = message.getText().toString()
Store data in a database:
Read http://developer.android.com/guide/topics/data/data-storage.html#db The notepad tutorial it mentions is very easy to follow and will show you how to set up a simple database. That example covers adding new records to the database and modifying existing ones, which should be sufficient for you.
If you clarify what exactly you were having difficulty with, I'll be able to make my answer more specific.
Currently I have an edittext field that when the user presses enter it does mostly what I want it to, validate an IP Address format and inform the user if it is wrong. How do I make it so when the user presses enter it checks it like it is supposed to be does NOT enter the newline character?
Here is my code for it.
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
if(validateIPaddress(m_etIPAddress.getText().toString()))
{
ConfigData.m_IPAddress = m_etIPAddress.getText().toString();
}
else
{
showAlertDialog("Invalid IP Address\n Example: \n255.255.255.255\n0.0.0.0","Error: ");
m_etIPAddress.setText(ConfigData.m_IPAddress);
m_etIPAddress.requestFocus();
}
return false;
}
Another problem I have is that in the false condition of the validation, that it will not bring up the soft keyboard to allow the user to reedit that text field. If the user clicks on another edit text the window gives it focus, and allows the user to edit the second text field while still maintaining the 'green outline' around the original edittext. Is there a way to fix this?
EDIT:
Thanks for the response. The EditText still creates a newline. I tried calling that when I create the EditText and it shows the dialog then inserts a newline character at the beginning.. which is weird because the
m_etIPAddress.setText(ConfigData.m_IPAddress);
should automatically overwrite anything in that field to the static IP saved within ConfigData. (my settings class) and I think the focus might work, the problem is that after requestFocus, that EditText shows it has focus but is unresponsive.
I can click on other EditText's and modify them, while it still shows the focus outline on the IP EditText. If I click on the IP EditText it doesn't actually do anything. Its kind of strange.
I think for your EditText creating a new line, you can do that by replacing the enter button by a done button like that :
yourEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
And to go with that, you can put this to your xml file describing your EditText :
android:maxLines="1"