2

I am making an android application where I am using the AutoCompleteTextView and trying to set the onClickListener for reset the entered value on click on AutoCompleteTextView.

Issue is that I have two AutoCompleteTextView so when I entered text in first AutoCompleteTextView then second AutoCompleteTextView then click on first AutoCompleteTextView then it is not calling OnClickListener on the first AutoCompleteTextView

Any idea why this is behaving such weird ?

<AutoCompleteTextView
    android:id="@+id/fromStationEdit"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/TextView1"
    android:layout_marginLeft="12dp"
    android:layout_marginTop="3dp"
    android:background="@drawable/apptheme_edit_text_holo_light"
    android:drawableRight="@drawable/ic_rail"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:imeOptions="actionNext"
    android:paddingLeft="8dp"
    android:singleLine="true"
    android:textColor="@color/black"
    android:textColorHighlight="@android:color/black"
    android:textCursorDrawable="@null"
    android:textSize="16sp" >

    <requestFocus />
</AutoCompleteTextView>

ClickListener on AutoCompleteTextView.

// reset the value when user click on this view
source_stn_txt.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        source_stn_txt.setText("");
    }
});

// reset the value when user click on this view
dest_stn_txt.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {
        dest_stn_txt.setText("");
    }
});

source_stn_txt.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        source = source_stn_txt.getText().toString().trim();
        dest_stn_txt.requestFocus();
    }
});

Thanks in advance.

N Sharma
  • 33,489
  • 95
  • 256
  • 444

3 Answers3

2

The link that helped @Williams is: onClick event is not triggering | Android. Also posted above in the comments. I'm glad you found your solution!

When a user interacts with a UI element the various listeners are called in a top down order. (For example: OnTouch -> OnFocusChange -> OnClick.) If a listener has been defined (with setOn...Listener) and it consumes this event: the lower priority listeners will not be called. By its nature the first time you touch an EditText it receives focus with OnFocusChangeListener so that the user can type. The action is consumed here therefor OnClick is not called. Each successive touch doesn't change the focus so the event trickles down to the OnClickListener.

Community
  • 1
  • 1
SoundsDangerous
  • 708
  • 6
  • 13
1

You need to use AdapterView.OnItemClickListener() not OnClickListener.

    AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.fromStationEdit);

    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2", "Ubuntu"};

    ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < values.length; ++i) {
        list.add(values[i]);
    }
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, list);
    actv.setAdapter(adapter);

    actv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Toast.makeText(MainActivity.this,
                    adapter.getItem(position).toString(),
                    Toast.LENGTH_SHORT).show();
        }
    });
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
0

Have you declared items to complete in your Strings.xml. If not, do something like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">AutoComplete</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="auto_complete">AutoComplete</string>
<string name="multi_auto_complete">Multi AutoComplete</string>
<string-array name="list_of_countries">
  <item >USA</item>
  <item >Uk</item>
  <item >Canada</item>
  <item >Australia</item>
  <item >France</item>
  <item >Italy</item>
  <item >China</item>
  <item >Japan</item>
  <item >Spain</item>
  </string-array>

</resources>

Or declare in your class Activity:

AutoCompleteTextView myAutoComplete;

String item[]={
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
};
Umit Kaya
  • 5,771
  • 3
  • 38
  • 52