I am using autocomplete textview with arraylist value [aaa0,aaa1,...aaa100,aab0,aab1,...,aab100], I enter value 'aa' and scroll down till next 10 value ie.'aa10'and then i enter value 'aab' the suggestion list starts from 10th value i.e 'aab10'.
I have to scroll up to see the first value i.e 'aab0'.I want first value of suggestion list to appear when i change the search text.suggestion list screenshot
You need to extend the AutoCompleteTextView class
and override the onFilterComplete method
where you have to dismiss and show the drop down again to refresh the list view.
Also I would suggest instead of extending AutoCompleteTextView
extend the android.support.v7.widget.AppCompatAutoCompleteTextView
for obviously better support
here's the code
package your.package.name;
import android.content.Context;
import android.util.AttributeSet;
import android.support.v7.widget.*;
public class CustomAutoCompleteTextView extends AppCompatAutoCompleteTextView {
public CustomAutoCompleteTextView(Context context) {
super(context);
}
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void onFilterComplete(int count) {
super.onFilterComplete(count);
//show and dismiss the drop down to refresh the list in it
this.dismissDropDown();
this.showDropDown();
}
}
EDIT
Also you can use the AutoCompleteTextView.setListSelection() method to scroll to the top.
Instead of dismissing and showing the DropDown on every filter
which is more efficient.
#Override
public void onFilterComplete(int count) {
super.onFilterComplete(count);
//set list selection to 0
if(getListSelection() != 0)
this.setListSelection(0);
}
Related
When a user want to paste text in MultiAutoCompleteTextView I want to overwrite it.
Means whatever he copies it must paste in my MultiAutoCompleteTextView as "Java is rocket".
I may achieve it from previous question here but I don't know how to hook the class MonitoringEditText to my MultiAutoCompleteTextView.
Can I achieve it or it is impossible.
You can do this using the same concept provided in the code from the link you provided. Extend the MultiAutoCompleteTextView and override the onTextContextMenuItem method.
Something along the lines of:
public class MonitoringMultiAutoCompleteTextView extends MultiAutoCompleteTextView {
public MonitoringMultiAutoCompleteTextView(Context context) {
super(context);
}
public MonitoringMultiAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MonitoringMultiAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//based on http://stackoverflow.com/a/14981376/1566836
#Override
public boolean onTextContextMenuItem(int id) {
// Do your thing:
boolean consumed = super.onTextContextMenuItem(id);
// React:
if (android.R.id.paste == id) {
setText("Java is rocket");
}
return consumed;
}
}
Then change your MultiAutoCompleteTextView in your layout file to whatever.your.full.package.is.MonitoringMultiAutoCompleteTextView.
After that, any attempt to paste into your MultiAutoCompleteTextView will result in the text being changed to "Java is rocket"
I am quite new in android. I want to create a listview with a togglebutton on each item. I want to open a new activity when the Text on the list item is clicked. And change the state od the toggle button when the toggle button is clicked.
The UI would look something like below.
Item 1 |True
Item 2 |False
Item 3 |True
I know how to open a new activity when clciked on a listview. I know how to change the state of a toggle button. But i am not able to find out, how to have toggle button on the listview and the click on the listview not to effect the toggle button and the click on toggle button not to effect the listview.
Maybe both should have separate listeners. But i am not being able to figure out how.
For people looking for an answer. This post really helped me to achieve what was desired.
The Basic idea is to do something as below.
public class DontPressWithParentButton extends Button {
public DontPressWithParentButton(Context context) {
super(context);
}
public DontPressWithParentButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DontPressWithParentButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void setPressed(boolean pressed) {
if (pressed && getParent() instanceof View && ((View) getParent()).isPressed()) {
return;
}
super.setPressed(pressed);
}
}
How do I show some default suggestions for AutoCompleteTextView before the user type anything? I cannot find a way to do this even with creating a custom class that extends AutoCompleteTextView.
I want to show suggestions for common input values to save the user from typing.
Any suggestions?
You should subclass AutoCompleteTextView and override enoughToFilter() to return true all the time. After that you can call performFiltering("",0) (it's a protected function, so you can export this call via a public function in your class).
Something like that:
public class ContactsAutoCompleteTextView extends AutoCompleteTextView {
public ContactsAutoCompleteTextView(Context context) {
super(context);
}
public ContactsAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ContactsAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean enoughToFilter() {
return true;
}
public void temp() {
performFiltering("",0);
}
}
Itay Kahana's answer is indeed correct. The only thing I would add is that instead of creating a temp() function, to override the onFocusChanged function. Personally I used the following:
#Override
protected void onFocusChanged (boolean focused, int direction, Rect previouslyFocusedRect) {
if(focused)
performFiltering("", 0);
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
If you dont need it to be dynamic I would go by having a string array in the resources, and then just load the array when the AutoCompleteTextView is about to be viewed. Like:
public class CountriesActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
}
Which can be found on http://developer.android.com/reference/android/widget/AutoCompleteTextView.html
Another way which I have done a couple of times which allows it to learn from the user is o use a database connection with IE a simple cursor. When you create the db you could insert some default values.
HereĀ“s an example with using simple cursor adapter: http://androidcommunity.com/forums/f4/how-to-use-autocompletetextview-with-simplecursoradapter-15875/
Edit 1:
One idea to show the list before the user starts type is to have a simple listview below the EditText. Not sure if you could call the autocompletetextview to show the suggestions, should be possible somehow. Perhaps you need to create your own autocompletetextiew class.
I have a couple of custom DialogPreference implementations floating around, such as this one:
package apt.tutorial;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.TimePicker;
public class TimePreference extends DialogPreference {
private int lastHour=0;
private int lastMinute=0;
private TimePicker picker=null;
public static int getHour(String time) {
String[] pieces=time.split(":");
return(Integer.parseInt(pieces[0]));
}
public static int getMinute(String time) {
String[] pieces=time.split(":");
return(Integer.parseInt(pieces[1]));
}
public TimePreference(Context ctxt) {
this(ctxt, null);
}
public TimePreference(Context ctxt, AttributeSet attrs) {
this(ctxt, attrs, 0);
}
public TimePreference(Context ctxt, AttributeSet attrs, int defStyle) {
super(ctxt, attrs, defStyle);
setPositiveButtonText("Set");
setNegativeButtonText("Cancel");
}
#Override
protected View onCreateDialogView() {
picker=new TimePicker(getContext());
return(picker);
}
#Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
picker.setCurrentHour(lastHour);
picker.setCurrentMinute(lastMinute);
}
#Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
lastHour=picker.getCurrentHour();
lastMinute=picker.getCurrentMinute();
String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute);
if (callChangeListener(time)) {
persistString(time);
}
}
}
#Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return(a.getString(index));
}
#Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
String time=null;
if (restoreValue) {
if (defaultValue==null) {
time=getPersistedString("00:00");
}
else {
time=getPersistedString(defaultValue.toString());
}
}
else {
time=defaultValue.toString();
}
lastHour=getHour(time);
lastMinute=getMinute(time);
}
}
They work just fine. However, in an application with android:targetSdkVersion="11" defined, on a XOOM, they show up missing the indent when in the PreferenceActivity:
Also, the font size appears a smidge bigger, at least for the title.
There's nothing in DialogPreference where I am really overriding any formatting behavior for that stuff, AFAIK. The preference XML is unremarkable, other than referring to the above class:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="sort_order"
android:title="Sort Order"
android:summary="Choose the order the list uses"
android:entries="#array/sort_names"
android:entryValues="#array/sort_clauses"
android:dialogTitle="Choose a sort order" />
<CheckBoxPreference
android:key="alarm"
android:title="Sound a Lunch Alarm"
android:summary="Check if you want to know when it is time for lunch" />
<apt.tutorial.TimePreference
android:key="alarm_time"
android:title="Lunch Alarm Time"
android:defaultValue="12:00"
android:summary="Set your desired time for the lunch alarm"
android:dependency="alarm" />
<CheckBoxPreference
android:key="use_notification"
android:title="Use a Notification"
android:defaultValue="true"
android:summary="Check if you want a status bar icon at lunchtime, or uncheck for a full-screen notice"
android:dependency="alarm" />
</PreferenceScreen>
Anyone know where I'm going wrong?
Thanks!
UPDATE
Here is a link to a project that contains this custom preference and a simple preference XML file that demonstrates the problem. Even with just two Java classes, the preference XML, and an arrays.xml file, I get this phenomenon. Here is a compiled APK from this project.
(cross-posting from the associated android-developers thread)
OK, I figured it out.
There are three possible constructors on a Preference:
MyPreference(Context ctxt)
MyPreference(Context ctxt, AttributeSet attrs)
MyPreference(Context ctxt, AttributeSet attrs, int defStyle)
Somewhere along the line, I picked up the pattern of having the
one-parameter constructor chain to the two-parameter constructor
(passing null for the 2nd parameter), and having the two-parameter
constructor chain to the three-parameter constructor (passing 0 for
the 3rd parameter).
And that's not the right answer.
I am hoping that the right answer is to only implement the second
constructor, because the correct default style is internal to Android
(com.android.internal.R.attr.dialogPreferenceStyle). The second
constructor is the one used with inflating preference XML.
Thanks to all for the help!
You can dance with void Preference.setWidgetLayoutResource(int widgetLayoutResId) method, although I prefer to override View Preference.onCreateView(ViewGroup parent) method in my custom Preference class and hack it by adding custom views just below #android:id/summary (use hierarchyviewer utility for details).
The complete method is:
#Override
protected View onCreateView(ViewGroup parent)
{
View ret = super.onCreateView(parent);
View summary = ret.findViewById(android.R.id.summary);
if (summary != null)
{
ViewParent summaryParent = summary.getParent();
if (summaryParent instanceof ViewGroup)
{
final LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup summaryParent2 = (ViewGroup) summaryParent;
layoutInflater.inflate(R.layout.seek_bar_preference, summaryParent2);
seekBar = (SeekBar) summaryParent2.findViewById(R.id.seekBar);
seekBar.setMax(maxValue - minValue);
seekBar.setOnSeekBarChangeListener(this);
statusText = (TextView) summaryParent2.findViewById(R.id.seekBarPrefValue);
unitsRightView = (TextView) summaryParent2.findViewById(R.id.seekBarPrefUnitsRight);
unitsLeftView = (TextView) summaryParent2.findViewById(R.id.seekBarPrefUnitsLeft);
}
}
return ret;
}
Source code of my SeekBarPreference class based on code from http://robobunny.com can be downloaded here
I tried your code on the emulator. There is no problem with the code that you have given, and all the lines have the same formatting; but they all look more similar (in format) to the third preference (Lunch Alarm Time) than the others.
It looks like the other three preferences are getting indented more than required. So, maybe you have some global formatting style that is used, but not picked up by the TimePreference preference.
EDIT: OK. So, the above is not (completely) true. There is definitely a problem when I tried with the target sdk set to HoneyComb. But on setting the theme for the PreferenceActivity class as android:theme="#android:style/Theme.Black", there is a consistency in the look of all the preferences as shown below.
This style looks similar to Froyo, but not the HoneyComb; in the latter, the title font is smaller and there is more indentation. Probably, the default theme is not being assigned to Custom Preferences - just a guess :) A workaround would be to assign the default theme to your preference activity explicitly, but I don't know what the default theme in HoneyComb is (and whether it can be set).
The solution which helped me:
I have replaced
public TimePreference(Context ctxt, AttributeSet attrs) {
this(ctxt, attrs, 0);
}
with
public TimePreference(Context ctxt, AttributeSet attrs) {
this(ctxt, attrs, ctxt.getResources().getSystem().getIdentifier("dialogPreferenceStyle", "attr", "android"));
}
As you can see, I replaced third argument 0 with ctxt.getResources().getSystem().getIdentifier("dialogPreferenceStyle", "attr", "android") in the second constructor of custom preference class.
To make the accepted answer more clear. You only need this constructor:
public TimePreference(Context ctxt, AttributeSet attrs) {
// this(ctxt, attrs, 0); // wrong
super(ctxt, attrs);
}
The Android autocomplete only starts after two letters. How can I make it so the list appears when the field is just selected?
To get the autocomplete to show on focus add focus listener and show the drop down when the field gets focus, like this:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus){
editText.showDropDown();
}
}
});
Or just call editText.showDropDown() if you don't need the focus part.
Have a look to setThreshold method:
public void setThreshold (int
threshold)
Since: API Level 1
Specifies the minimum number of
characters the user has to type in the
edit box before the drop down list is
shown.
When threshold is less than or equals 0, a threshold of 1 is applied.
Extend the AutoCompleteTextView, overriding the enoughToFilter() methods and the threshold methods so that it doesn't replace the 0 threshold with a 1 threshold:
public class MyAutoCompleteTextView extends AutoCompleteTextView {
private int myThreshold;
public MyAutoCompleteTextView(Context context) {
super(context);
}
public MyAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void setThreshold(int threshold) {
if (threshold < 0) {
threshold = 0;
}
myThreshold = threshold;
}
#Override
public boolean enoughToFilter() {
return getText().length() >= myThreshold;
}
#Override
public int getThreshold() {
return myThreshold;
}
}
For people who want to change threshold using SearchView you have to use:
SearchView.SearchAutoComplete complete = (SearchView.SearchAutoComplete)search.findViewById(R.id.search_src_text);
complete.setThreshold(0);
Pad your adapter with one/two white character on left depending on the threshold setting.
Alternate method of changing the setting in your XML:
As mentioned by others you need to set your 'Auto Completion Threshold' to 1
Other than what #systempuntoout mentioned.
You can also do that in your XML file as shown
<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/edittext_id"
android:inputType="textAutoComplete"
android:completionThreshold="1"
/>
Note the line : android:completionThreshold="1"