I wanted to search a word in a textview which contains a large amount of data. To accomplish it i added a search option in action bar and tried using setOnQueryTextListener. However i am not able to use method tv.getFilters().filter() on text view. This method works fine on array adapter.
Could you please suggest how to search text in text view
Thanks for your help!!
First call this method on onQueryTextChange(). See below.
#Override
public boolean onQueryTextChange(String s) {
filterListByPattern(s, List);// List is TextView Large amount of data convert each word as a list item.
return true;
}
In filterListByPattern(),
private void filterListByPattern(String s, ArrayList<String> mainList)
{
for(int i = 0; i < mainList.size(); i++)
{
String word = mainList.get(i);
if(word.toLowerCase().toString().contains(s.toLowerCase().toString()))// if search word found in list
{
Spannable wordtoSpan = new SpannableString(word);
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE));// change the color of the word in to blue.
mainList.set(i, wordtoSpan.toString())//color changed word update to the List.
break;
}
}
for(int i = 0; i < mainList.size(); i++)
{
String word = mainList.get(i);
tv.append(word);//append each word in List. search word will be blue color
}
}
Related
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().
I'm trying to implement search functionality in listview like text search in browser.
I'm succeeded to search input words in cursor data. But how can i change corresponding word's background color when match is found ?
This is search code in bindview
if (searchEnabled) {
cursor.moveToFirst();
while (cursor.moveToNext()) {
String cursorText = cursor.getString(indexMessage);
if (s.equals(searchText.toString())) {
//Change matched word's(textview) background color here
holder.textMessage.setBackgroundColor(Color.WHITE);
Log.i("Cursor Scan:", cursorText);
}
}
}
After calling notifyDataSetChanged(), it changes color of only last row's textview.
I tried search by getting listview text from getChildAt() and partially succeeded, but this view is not consistent and it comes to it's original state on scrolling (since, this listview is inflated from cursor).
I tried following code for searching in listview text:
private void updateListView(String textForSearch) {
LocalStoreDB localDB = new LocalStoreDB(getApplicationContext());
localMessageDB.openDB();
Cursor cursorForSearch = localDB.selectMessageDB();
cursorForSearch.moveToFirst();
while (cursorForSearch.moveToNext()) {
int position = cursorForSearch.getPosition();
View v = listview.getChildAt(position);
TextView tv = (TextView) v.findViewById(R.id.textMessage);
SpannableString cursorString = new SpannableString(tv.getText());
Pattern p = Pattern.compile(textForSearch);
Matcher m = p.matcher(cursorString);
while (m.find()) {
cursorString.setSpan(new BackgroundColorSpan(Color.WHITE),
m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.i("m.find()", cursorString.toString());
}
tv.setText(cursorString);
}
localDB.closeDB();
}
You can set background color of a textview like this
yourTextView.setBackgroundColor("0xff0000ff");
I cannot see rest of the code but maybe you have to notify the changes you made after setting color.
Hope it helps
In your case I would try something like this:
TextView tv;
for(int i=0;i<listview.get.getCount();i++){
tv =(TextView) listview.getChildAt(i).findViewById(R.id.textMessage);
if(tv.getText().equalsIgnoreCase(searchstring)){
tv.setBackgroundColor("0xff0000ff");
}
else{
tv.setBackgroundColor("0xffffffff");
}
}
listViewAdapter.notifyDataSetChanged();
Sorry guys for late answer.
I've answered this question in another thread. Please follow this link:
https://stackoverflow.com/a/34287029/4688535
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();
}
}
}
Do anyone know how to highlight a certain word in a TextView?
With highligt I mean to use an italic typeface for the word "Hua Hin" in the following string.
The sandy beaches of Hua Hin, popular resort town in Thailand.
Must I use a textView in another textView to acomplish this?
I set the text in the onPostExecute-method of AsyncTask:
protected void onPostExecute(Bitmap[] bitmap) {
for (int i = 0; i < nmbrOfImages; i++) {
imageView[i].setImageBitmap(bitmap[i]);
textView[i].setText(scrollText[i]);
textView[i].setVisibility(TextView.VISIBLE);
}
loadAlertDialog();
}
I have all my text in an xml-file
If only italic you want then you can go for
textView[i].setText(Html.fromHtml(<i>some part</i> some other text);
else you can go for Spnnable
Here is an example in this answer.
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.