I'm having a weird issue with AutoCompleteTextView.
I have a AutoCompleteTextView that shows suggestions of cities when typing in it.
The list of cities is retrieved from a remote server via JSON. When I use the soft keyboard or the Mic Button on the soft keyboard, the suggestions work fine. AutoCompleteTextView does show the suggested cities.
But, I have a problem when I try to set the text using myAutoCompleteTextView.setText("Chi") , the auto complete does not show..
I have also tried myAutoCompleteTextView.append("Chi") but still no luck..
The adapter is there, its just that the suggestions don't show.
Any tips?
Thanks.
Yes you are right there is a bug in AutocompleteTextview to show default suggestion using setText(""); method.
But you can achieve this by adding some more lines of code as below.
autoText.postDelayed(new Runnable() {
#Override
public void run() {
autoText.showDropDown();
}
},500);
autoText.setText("chi");
autoText.setSelection(autoText.getText().length());
It is due to filtering,
No Need to any extra code for manage it, I found it in very easy and working way.
Google Dev. Reference link
autoText.setText("Default Value here",false)
autoText.setSelection(autoText.text.count()) // kotlin
as per documentation second parameter you can pass for filtering.
boolean: If false, no filtering will be performed as a result of this call.
Biraj Zalavadia's answer work, but you must write to "settext" in Runnable.
Like this:
mACTextViewEmail.postDelayed(new Runnable() {
#Override
public void run() {
mACTextViewEmail.showDropDown();
mACTextViewEmail.setText("My text is here");
mACTextViewEmail.setSelection(mACTextViewEmail.getText().length());
}
},500);
I searched for it and just found this solution that worked so well
Look at this issue
fun AutoCompleteTextView.showDropdown(adapter: ArrayAdapter<String>?) {
if(!TextUtils.isEmpty(this.text.toString())){
adapter?.filter?.filter(null)
}
}
In kotlin language, you can use this extension function.
Related
Hello I'm new to android developing.
Is there a method in java that equals to #.gotFocus?
Is there in java an events list that I can watch and select like in c# visual studio?
I tried to do #.Focus or something similar but had no success.
I want to reproduce the following scheme:
1- EditText has a certain hint => "Enter a value"
2- The user clicks the edit text and the hint disappears => ""
3- The user fills a certain value => "certain value"
Thank's for helpers :)
Ron Yamin, If I understand your doubt correctly what you want is:
1- Have a field of text for the user to type words/numbers etc --> It is called EditText in android
2- Have an hint so the user knows what to type --> Eg. "Type your name"
3- And react to focus in some way.
The first one you will achieve either through XML or by code. If you have a main.xml in your layouts folder (assuming you are using eclipse/android studio to develop), you can use the interface to drag an edit text to the android screen.
The second one you will achieve still through the XML. If you right click on it, right side of the screen there will be a little window called Proprieties that you can change things like height and width and a hint. Type there your hint.
Finally the last one you need to go to your code in .java and get a reference of your edit text (findViewById).
Either through setOnClickListener or setOnFocusChangeListener.
More info you can checkout here:
http://developer.android.com/guide/topics/ui/controls/text.html
I have googled a tutorial you can check with more detailed information and step by step guide.
Hope it helps:
http://examples.javacodegeeks.com/android/core/widget/edittext/android-edittext-example/
It seems that you changed your question quite a bit, and my C# ignorance got the best of me.
It seems that what you really want is an EditText, the example text you are looking for is the hint.
You can set the hint in the xml file or by code with .setHint(string) method.
Here's where to start:http://developer.android.com/guide/topics/ui/controls/text.html
edit 3 - events in android are dealt with by using listeners. You can use an onClickListener to achieve what you want.
textView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(){
//dostuff
}
}
Assuming your textfield is an instance of EditText (which it probably should be), you can do the following:
textfield.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// this is where you would put your equivalent #.gotFocus logic
}
}
});
It's worth noting that the behavior you've described can be achieved by using textfield.setHint. The hint is text that is cleared automatically when the user selects the EditText. It's designed specifically for the case you describe, e.g. textfield.setHint("Enter a Value")
I'm not familiar with c# but I'm guessing you want event fired when edittext get focus. Try this
EditText txtEdit= (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// do the job here when edittext get focus
}
}
});
I'm attempting to update an EditText as part of an Espresso test with:
onView(allOf(withClassName(endsWith("EditText")), withText(is("Test")))).perform(clearText())
.perform(click())
.perform(typeText("Another test"));
However I receive the following error:
com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException: No views in hierarchy found matching: (with class name: a string ending with "EditText" and with text: is "Test")
By breaking down the test line I can see that this occurs after performing clearText(), so I assume that the matchers are being re-run prior to each perform and fail the prior to the second action. Although this makes sense, it leaves me somewhat confused as to how to update the EditText using Espresso. How should I do this?
Note that I cannot use a resource ID or similar in this scenario and have to use the combination as shown above to identify the correct view.
You can use the replaceText method.
onView(allOf(withClassName(endsWith("EditText")), withText(is("Test"))))
.perform(replaceText("Another test"));
Three things to try:
1. You can run performs in succession.
onView(...)
.perform(clearText(), typeText("Some Text"));
2. There is a recorded issue on the Espresso page which was marked as invalid (but is still very much a bug). A workaround for this is to pause the test in-between performs.
public void test01(){
onView(...).perform(clearText(), typeText("Some Text"));
pauseTestFor(500);
onView(...).perform(clearText(), typeText("Some Text"));
}
private void pauseTestFor(long milliseconds) {
try {
Thread.sleep(milliseconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
3. Are you absolutely sure that your EditText contains the text, "Test"?
To set value in EditText with Espresso
simple like this
onView(withId(R.id.yourIdEditText)).perform(typeText("Your Text"))
I was having a similar issue and solved it using the containsString matcher and Class.getSimpleName(). Like this:
onView(withClassName(containsString(PDFViewPagerIVZoom.class.getSimpleName()))).check(matches(isDisplayed()));
You can see the full code here
You could try two things. First I would try to use
onView(withId(<id>).perform...
This way you would always have access to the EditText field even when other EditText fields are on the screen.
If that's not an option, you could split up your perform calls.
onView(allOf(withClassName(endsWith("EditText")),withText(is("Test")))).perform(clearText());
onView(withClassName(endsWith("EditText"))).perform(click());
onView(withClassName(endsWith("EditText"))).perform(typeText("Another Test");
I have done with Gallery in my project. It is working very fine, i was trying to use setSelection method of gallery so that at the start up i can define which item to be selected rather than always selecting first item.
I have tried to use setSelection(5) so that selection goes to 5th child but the gallery always select the first item. Is there any clue to integrated it perfectly.
Please try this:
your_gallery_view.post(new Runnable() {
#Override
public void run() {
your_gallery_view.setSelection(5);
}
});
Hope this helps.
I got the solution. We need to put the set selection method after setAdapter method. I was doi ng it before setting the updater. It is working fine. Thanks to all who tried to help me out.
What is that little box at the bottom showing "d" called? How do I enable it in my filtered SearchView? Where else can it be used?
Could it be some kind of a Toast? I looked and looked in API sources and couldn't find how to define it.
It's a PopupWindow http://developer.android.com/reference/android/widget/PopupWindow.html. You do not need to enable it. Its used by default when you set your ListView to
listView.setTextFilterEnabled(true);
and the PopupWindow shows up when you set or clear the filter text
listView.setFilterText("d");
i found out how to get rid of that ugly popup window. The trick is to work with filter directly.The code below assumes you have implemented filterable in your customAdapter.
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
m_listView.clearTextFilter();
} else {
ContactsAdapter ca = (ContactsAdapter)lv.getAdapter();
ca.getFilter().filter(newText);
//following line was causing the ugly popup window.
//m_listView.setFilterText(newText);
}
return true;
}
As far as I know it just repeats what you have typed in as a search key. If you have a list without a text box to type your search key, its quite handy to know what you have typed.
Cliff
I have a textbox and search button, now all I need is the code to implement searching an array. The array I have is called Facts_Array (consists of strings). Comment if you need any more information.
Something like this:
EditText searchField = (EditText) findViewById(R.id.searchfield);
Button searchButton = (Button) findViewById(R.id.searchbutton);
searchButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
for (String s : Facts_Array) {
if (searchField.getText().toString().contains(s)) {
// Do stuff
}
}
}
};
Of course, you might want to refine the actual search bit some more (right now it's just using contains()), at least by ignoring case.
But, I do not think I understood exactly if I can use it just for the
search i need.
e.g.: to retrieve the typed text and return my search response,
provided from a web service.
So, basically i need just the design, not it's functionality.
Can someone tell me if i can use this bar only for my personal data,
not a general search from internet or the hole phone,
and i would appreciate link for a simple and clear tutorial, because i
couldn't found anything concrete till now.