Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set setOnClickListener for AutoCompleteTextView?

Tags:

I am selecting text for AutoCompleteTextView.After i want apply setonclicklistener to selected text.if any have idea.

ArrayAdapter<String> arrAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, sampleACTV);   AutoCompleteTextView ACTV = (AutoCompleteTextView) findViewById(R.id.spinner);  ACTV.setAdapter(arrAdapter);    }  private static final String[] sampleACTV = new String[]          { "android","androidpeople.com","iphone","blackberry" };  

in my example i am selecting one like android call an intent to go to nest Acitivity

like image 413
sai Avatar asked Dec 27 '11 12:12

sai


People also ask

How do I set autocomplete text on Android?

If you want to get suggestions , when you type in an editable text field , you can do this via AutoCompleteTextView. It provides suggestions automatically when the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.

How do you get the ID of the selected item of the AutoCompleteTextView in Android?

OnItemClickListener and set onItemClickListener() on the AutoCompleteTextView to get the user selected item value from the list. Notice that while using the ArrayAdapter , we have provided a layout object as argument android. R.

Can we set Onclicklistener on TextView?

In Android, TextView is a child class of View, and hence can use the method setOnClickListener() on the object of TextView.


2 Answers

There are different click listeners in AutoCompleteTextView.

The first way is in the layout xml, you can define the onCLick attribute, with the function that you want to be called, in the example below, clicked.

<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/spinner"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:onClick="clicked" /> 

Then, in your activity, you define the function clicked.

public void clicked(View v) {    // on click do .. }  

Or you can set it directly in your code:

ACTV.setOnClickListener(new OnClickListener() {     @Override     public void onClick(View v) {         finish();     } }); 

If you want to set the click listener when the user clicks in an item in the dropdown list there is another method, the setOnItemClickListener.

ACTV.setOnItemClickListener(new OnItemClickListener() {     @Override     public void onItemClick (AdapterView<?> parent, View view, int position, long id) {         //... your stuff     } }) 

And you have a last option, to set the click listener when the user actually selects an item in the dropdown list using setOnItemSelectedListener.

ACTV.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {     @Override     public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {         //... your stuff     }     @Override     public void onNothingSelected (AdapterView<?> parent) {         //... your stuff     } }) 

References:

http://developer.android.com/reference/android/widget/AutoCompleteTextView.html

Good luck!

like image 145
caiocpricci2 Avatar answered Oct 08 '22 05:10

caiocpricci2


You need to create Custom Adapter and assign OnClick event to the view in getView()

like image 40
Sunil Kumar Sahoo Avatar answered Oct 08 '22 05:10

Sunil Kumar Sahoo