UPDATE
This project is no longer maintained today, but solutions are most welcomed.
Thanks.
I am creating an automation test project in which I have issue to select the item from the AutoCompleteTextView.
You can see the snap and the views[all expanded] it has. The dropdown of the AutoCompleteTextView does not appear in view tree nor I am able to select using mouse.
I have tried below approaches to select the item form from the AutoCompleteTextView adapter:
UiScrollable locationList = new UiScrollable(new UiSelector().scrollable(true)); locationList.scrollTextIntoView(location);
UiScrollable locationList = new UiScrollable(locationEditText.getSelector()); locationList.scrollTextIntoView(location); Here locationEditText is my AutoCompleteTextView
UiObject selectedLocation = locationList.getChild(new UiSelector().text(location)); selectedLocation.click(); from the locationList it does not select the item with the string passed.
editLocationResId = "android:id/text1"; UiObject selectedLocation = new UiObject(new UiSelector().resourceId(editLocationResId)); selectedLocation.click(); The id from the adpter textview does not work either.
Can anybody help me with selecting the item from the AutoCompleteTextView in uiautomator? Or the more approaches get the desire output.
Get the cordinates of text field :
int x = element.getLocation().getX();
int y = elmement.getLocation().getY();
Add values to get the right cordinates.
x = x+x1
y = y+y1
Use tap function to click on it.
TouchAction action = new TouchAction(driver);
action.tap(x,y).perform();
first open the pick-up list, then try to use UiDevice.pressKey(), to send a DPAD_DOWN OR UP event and press Enter to select the item you want.
If you need to press the first element from the dropdown list, I have found a useful workaround:
public void selectFirstElement() {
TouchAction touchAction = new TouchAction(getAppiumDriver());
int xTapped = autoCompleteTextView.getLocation().getX() + autoCompleteTextView.getSize().getWidth() / 2;
int yTapped = autoCompleteTextView.getLocation().getY() + autoCompleteTextView.getSize().getHeight() ;
touchAction.tap(xTapped, yTapped).perform(); }
Given an AutoCompleteTextView declared as autoCompleteTextView, you can get the current location on the screen and calculate the item which is gonna be located below it. Hope it helps!
Well, i found a turn around(this is for autocompletetextview, for spinner delete setText()):
UiObject edit = mDevice.findObject(new UiSelector().className(EditText.class).instance(0));
edit.click();
edit.setText(name);
Rect rect = edit.getBounds();
mDevice.waitForWindowUpdate(Package.DIGI_PACKAGE, 1000);
mDevice.click(rect.left + 64, rect.bottom + 72);
Get object, click it, insert text that you want,wait for dropdown to appear then click under it with some pixels
I had created and have been using the simplest and the most efficient way. Just locate, type and then select the desired value in autocomplete text view.
Hope it will work for you as well.
driver.findElement(By.id(“Element ID”)).clear();
driver.findElement(By.id(“Element ID”)).sendKeys("Type what you want to select");
driver.pressKeyCode(AndroidKeyCode.KEYCODE_PAGE_DOWN);
driver.pressKeyCode(AndroidKeyCode.ENTER);
You can find the element that you want to select using its text. Make sure that this line of code is executed after the suggestion list has been shown.
//Get a reference to the autocomplete element
UiObject2 autocomplete = device.findObject(By.res("yourpackage","autocomplete_id"));
//Put a text that will trigger the suggestion list
autocomplete.setText("68623 Lampertheim");
//Gain the focus
autocomplete.click();
//Finally, find the element that we want in the suggestion list and click it
device.findObject(By.text("68623 Lampertheim")).click();
Hope it helps.
try the code below it worked for me.
I am using the the AutoCompleteText to auto complete the location where the user is currently in, the locationList is nothing but an array that i wrote in the strings.xml file, so use your own string array here.
locationList = res.getStringArray(R.array.ticketLocation);
ArrayAdapter<String> locationAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, locationList);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtCountries);
textView.setThreshold(1);
textView.setAdapter(locationAdapter);
textView.setValidator(new Validator());
textView.setOnFocusChangeListener(new FocusListener());
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
TextView ticketLocation = (TextView) view;
getTicketLocation = ticketLocation.getText().toString();
}
});
and below is the code for validating the text input in the location field, the fixText() method prevent the user from typing text that does not exist in your string array, for instance: if the user type "germany" which does not exist in your string array list, it will be replaced by " " which is an empty string inside your edittext input field
class Validator implements AutoCompleteTextView.Validator {
#Override
public boolean isValid(CharSequence text) {
// Log.v("Test", "Checking if valid: " + text);
Arrays.sort(locationList);
if (Arrays.binarySearch(locationList, text.toString()) > 0) {
return true;
}
return false;
}
#Override
public CharSequence fixText(CharSequence invalidText) {
// Log.v("Test", "Returning fixed text");
/*
* I'm just returning an empty string here, so the field will be
* blanked, but you could put any kind of action here, like popping
* up a dialog?
*
* Whatever value you return here must be in the list of valid
* words.
*/
return "";
}
}
class FocusListener implements View.OnFocusChangeListener {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// Log.v("Test", "Focus changed");
if (v.getId() == R.id.txtCountries && !hasFocus) {
// Log.v("Test", "Performing validation");
((AutoCompleteTextView) v).performValidation();
}
}
}
Related
I'm developing an application that allows user to add his own word, it's category. I start with a spinner that has the words available before in sqlite db and Add new word. When he clicks on Add new word, the spinner changes into EditText box that allows him to enter a new word. Whenever I try to save the text written in Edittext to string it shows that string is defined as "Add new word" and not the value entered by the user. What should I do?
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(parent.getId()==R.id.spinword)
{
editword=parent.getItemAtPosition(position).toString();
if(editword.equals("Add new word"))
{
spinwrd.setVisibility(spinwrd.INVISIBLE);
edtwrd.setVisibility(edtwrd.VISIBLE);
flag = 1;
}
if(flag == 1)
{
editword.replace(editword,edtcat.getText().toString());
}
}
if(parent.getId() == R.id.spincat) {
editcategory = parent.getItemAtPosition(position).toString();
if (editcategory.equals("Add new category")) {
spincat.setVisibility(spincat.INVISIBLE);
edtcat.setVisibility(edtcat.VISIBLE);
editcategory = edtcat.getText().toString();
}
}
}
I tried equals method and replace method but in vain.
Not sure of what you want to do but i think you should try changing
editword.replace(editword,edtcat.getText().toString()
for
editword.replace(editword,edtwrd.getText().toString())
The answer to this question is as follows: First, replacing, clearing, or any change depends on the position of the line meaning that: In the code above, I was trying to capture the text written by the user but at that point of execution when user chooses "Add new word" the text becomes it. The solution to this problem is to place the line of code editword.replace(editword,edtcat.getText().toString()); when this function is finished to be sure that the user has entered some text.
Below is the method I've coded to input numbers for a calculator. The code is fully functional and is error-free.
I'm trying to figure out how I would write a separate method called backspace to remove one character at a time from the user input.
To give an example of how onClick1 works (just to make it crystal clear what I want to do), if I entered 2+4*6 (using buttons; their onClick action linked to onClick1), then textViewCalcHistExp1 would display the text 2+4*6, and arrayList would hold the following values: [2,+,4,*,6].
I want backspace to work so that if I clicked the button (linked with the backspace method), textViewCalcHistExp1's display would now be 2+4* and arrayList would now hold the following values: [2,+,4,*].
Here is the code:
ArrayList<String> arrayList = new ArrayList<String>();
String stringInput = "";
String stringInputWithOp = "";
public String prevCalc = "";
public void onClick1 (View view) {
TextView textViewCalcHistExp1 = (TextView) findViewById(R.id.textViewCalcHistExp1);
Button button = (Button) view;
stringInput = (String) button.getText().toString();
if (!stringInput.contains("+") && !stringInput.contains("-") && !stringInput.contains("×") && !stringInput.contains("÷")) {
stringInputWithOp = stringInputWithOp+stringInput;
if (arrayList.size()>0) {
arrayList.remove((arrayList.size()-1));
}
arrayList.add(stringInputWithOp);
}
else {
arrayList.add(stringInput);
arrayList.add(stringInput);
stringInputWithOp="";
}
//This version truncates array formatting i.e. entering "2+4*6" would display "2+4*6"
textViewCalcHistExp1.setText(textViewCalcHistExp1.getText().toString()+stringInput);
//This version leaves array formatting i.e. entering "2+4*6" would display [2,+,4,*,6] ;good for debugging
//textViewCalcHistExp1.setText(arrayList.toString());
}
You could do something like this:
private void backspace() {
if (!arrayList.isEmpty()) {
// removing the last item from the ArrayList
arrayList.remove(arrayList.size() - 1);
}
String string = textViewCalcHistExp1.getText().toString();
if (string.length() > 0) {
// removing the last character from the TextView
textViewCalcHistExp1.setText(string.substring(0, string.length() - 1));
}
}
You'd also need to make textViewCalcHistExp1 an instance variable (arrayList already is), by declaring it outside onClick1().
Is it somehow possible to achieve that?
In example: we have listView with 20 items, every item has some text. User want to select half of ending text from item 1. and the half of another item text (same behaviour like in webView or selectable textView). Did someone think about that feature? Where should I search the solution?
This topic will be updated when solution will be found.
ps. I know you will say "show us code first". I do not have it yet.
In your istview on item click listener code like this
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)
{
// TODO Auto-generated method stub
TextView tv;
String text_from_tv, finalText;
tv= (TextView) view.findViewById(R.id.textview_id);
text_from_tv = tv.getText();
if(!firstPartSelected)
finalText += text_from_tv.substring(0,text_from_tv.length()/2);
else
finalText += text_from_tv.substring(text_from_tv.length()/2,text_from_tv.length());
//Save this finalText String to any string array and use those values
}
});
and that string array contains the second halfs of the selected word, if it helpful up vote the answer!!
In your list_item.xml for your list view. You will want to set,android:textIsSelectable="true" and make android:clickable="true" for that same item.
I won't give any code, just how I would do it :
boolean firstPartSelected false;
String finalText ="";
// ListView Definition
// OnItemClickListener
OnItemClick(...){
if(!firstPartSelected)
finalText += TextFromItem.substring(0,TextFromItem.length()/2)
else
finalText += TextFromItem.substring(TextFromItem.length()/2,TextFromItem.length())
}
This is not some real code, just an idea of how to implement it. Is that what you wanted ?
Try handle OnFocusChangeListener for your EditText, when focus will be change, color selected text in EditText using spans (Android: Coloring part of a string using TextView.setText()?). If you have large count of EditText you can use view.setTag(etNumber) and (Integer)view.getTag() for each EditText and use this information while concat output string in loop (look for more info Android - get children inside a View?)
P.S. EditText is inheritor of TextView, what yo can do with TextView you will can do with EditText
i make a spinner in which i impliment when a select a specific item from spinner then edit text is visible other wise invisible.here all is fine
but there is a problem i want when user choose specific item then value of edit text is store in variable otherwise sppiner item value store means if user select "OTH-Other" then edit text value store in spinnerSelectedID otherwise move in else.
my elese part work nice but in if part i could not store the edit text value in spinnerSelectedID varible.
ArrayList<String> faulttypespinner = new ArrayList<String>();
faulttypespinner.add("XL-Cross Level");
faulttypespinner.add("AL-Alignment");
faulttypespinner.add("UN-Unevenness");
faulttypespinner.add("XL-Cross Level");
faulttypespinner.add("AL-Alignment");
faulttypespinner.add("UN-Unevenness");
faulttypespinner.add("BD-Ballast Deficiency");
faulttypespinner.add("SE-Super elevation on curve");
faulttypespinner.add("LP-Loose Packing");
faulttypespinner.add("LJ-Low Joint");
faulttypespinner.add("BA-Bridge");
faulttypespinner.add("LC-Level Crossing");
faulttypespinner.add("LJ-Low Joint");
faulttypespinner.add("P and C-Point n Xing");
faulttypespinner.add("OTH-Other");
faulttypespinner.add("SEJ-SEJ");
faulttypespinner.add("WEED-Weed on Cess");
ArrayAdapter<String> faultadapter = new ArrayAdapter<String>
(mConetxt,android.R.layout.simple_spinner_item, faulttypespinner);
faultadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mspinner.setAdapter(faultadapter);
mspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Log.d("TESTING", parent.getItemAtPosition(pos).toString());
if(parent.getItemAtPosition(pos).toString().equals("OTH-Other")) {
otherdefect.setVisibility(View.VISIBLE);
text = otherdefect.getText().toString();
spinnerSelectedID = String.valueOf(mspinner.getSelectedItem());
spinnerSelectedID = (otherdefect.getText().toString());
}
else
{
otherdefect.setVisibility(View.GONE);
spinnerSelectedID = String.valueOf(mspinner.getSelectedItem());
}
}
I have provided an AutoCompleteTextView to show suggestions to the user. Based on the item selected by the user, I am getting the ID of the item and using it in data base side. Now my problem is to force the user to make selection only from AutoCompleteTextView (i.e. user should not enter his own text). It is a client requirement. How to do this?
Here's a pretty straightforward solution:
You can create a variable to store the selected value by setting setOnItemClickListener in your AutoCompleteTextView. Then you can null that value whenever a user types in the field by adding a TextWatcher to it. Finally, you can validate your variable is not null before continuing.
String my_var; //keep track!
AutoCompleteTextView tv = (AutoCompleteTextView) layout.findViewById(R.id.tv);
tv.setAdapter(my_adapter);
tv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
my_var = my_adapter.getItem(position).toString();
}
});
/**
* Unset the var whenever the user types. Validation will
* then fail. This is how we enforce selecting from the list.
*/
tv.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
my_var = null;
}
#Override
public void afterTextChanged(Editable s) {}
});
I happened to need such a requirement for a project I am working on, and I though I'll share you guys the way I implemented the required.
I added a on focus change listener for the auto-complete text view and checked when the user has focus changed focus from the auto-complete, and handled the situation straight forward.
autoTextViewCountry.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(!b) {
// on focus off
String str = autoTextViewCountry.getText().toString();
ListAdapter listAdapter = autoTextViewCountry.getAdapter();
for(int i = 0; i < listAdapter.getCount(); i++) {
String temp = listAdapter.getItem(i).toString();
if(str.compareTo(temp) == 0) {
return;
}
}
autoTextViewCountry.setText("");
}
}
});
So my implementation is: if the typed text doesn't exist in the array adapter then on focus changed empty the text view, and later on when continuing to next stage of say registration, check if this text view is empty or not.
Hope this approach helps somebody.
Happy coding.
NiceAutoCompleteTextView will give you the ability to check whether a selection was made from the drop-down popup, by invoking isSelectionFromPopup()
Just add this property to your AutoCompleteTextView.
android:focusable="false"
My Code looks like:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/menu"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Mode">
<AutoCompleteTextView
android:id="#+id/mode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
/>
</com.google.android.material.textfield.TextInputLayout>
On Java Side:
AutoCompleteTextView mode = findViewById(R.id.mode);
final List<String> modeList = new ArrayList();
modeList.add("YEARLY");
modeList.add("HALF-YEARLY");
modeList.add("QUARTER-YEARLY");
modeList.add("MONTHLY");
mode.setAdapter(new ArrayAdapter(getApplicationContext(),R.layout.list_item,modeList));
To get the Text of AutoCompleteTextView:
mode.getText().toString()
Ok I assume you would like to limit the input of the user to the texts contained in the list of items listed in the suggest box.
For instance if you have:
One
Two
Three
then the user could only type for the first character "O" and "T".
And so on according to the text entered before.
To achieve this you can utilize the setFilters method of TextView:
editBox = (TextView) findViewById(R.id.editBox);
editBox.setFilters(getFilters());
editBox.addTextChangedListener(this);
editBox.setOnFocusChangeListener(this);
Additionally you would probably need the text change listener and focus listener to react and update the filtered list when a new character is entered ... plus to update the filter.
Here is an example of decimal number filter I have used on a project:
protected InputFilter[] getFilters()
{
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter()
{
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)
{
// limit input to digits and decimal / thousand separator only
// in case thousand separator is pressed, change it to decimal
// separator instead
String output = "";
if (number.isFocused())
{
for (int i = start; i < end; i++)
{
char c = source.charAt(i);
if (isDecimalOrThousandSeparator(c))
{
output = output + Character.toString(decimalSeparator);
}
else if (Character.isDigit(c))
{
output = output + Character.toString(c);
}
}
return output == "" ? null : output;
}
return null;
}
};
return filters;
}
A simple solution would be to just check if the current input is one of the items in the adapter. You can do it like this:
val AutoCompleteTextView.isValid: Boolean
get() {
for (i in 0 until adapter.count) {
if (adapter.getItem(i) == text.toString()) {
return true
}
}
return false
}
here is the another solution for this proAutoCompleteTextView.Validator to ensure the valid values. Validator is invoked when edit text looses focus.
autoCompleteTextView.validator = object : AutoCompleteTextView.Validator {
override fun isValid(text: CharSequence?): Boolean {
return optionsList.contains(text.toString())
}
override fun fixText(invalidText: CharSequence?): CharSequence {
return ""
}
}
Where optionsList is list of valid values.
I was having the same requirement, so here is my implementation:
autoCompleteTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
autoCompleteTextView.showDropDown();
}
});
In the xml set focusable=false and focusableInTouchMode=false.
Happy coding
In order to have a non editable variation of the AutoCompleteTextView, you should disable user input in the AutoCompleteTextView. That can be achieved by setting android:inputType="none" on the AutoCompleteTextView.