I'd like to have both type of clicks on a listView - onClick and LongClick.
I've implemented it like this:
this.listViewSub = (ListView) this.findViewById(R.id.listsub);
this.listViewSub.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(final AdapterView parent, final View view, final int position,
final long id) { ... } });
// listen to long click - to share texts
this.listViewSub.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) { ... } });
But it does't fire the Long Click.
Anyone has any idea why?
You have to enable the LongClickable
list.setLongClickable(true);
and
list.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
}
});
#Vadim, are your listview's adapter is extends from BaseAdapter? if yes, then also need to set convertView.setLongClickable(true); in the getView().
For me, I had to set android:longClickable="true" in the XML file that contains my ListView row layout (not ListView layout) for the item to be long-clickable.
onLongClick returns true if the callback consumed the long click, false otherwise. So if the event is handled by this method, return true.
Related
I have list view with two actions one is for new activity and other one is when I long press the list item delete but the item will not delete mean while when I long press the new activity will be opened automatically..how to solve this issue
You have to set setOnItemLongClickListener() and setOnItemClickListener() in the ListView:
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
Log.v("long clicked","pos: " + pos);
return true;
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("Hello!", "Y u no see me?");
}
});
Make sure your onLongClick retrun true ,true means that the event is consumed. It is handled. No other click events will be notified.
#Override
public boolean onLongClick(View view) {
return true; // or false
}
or add in xml
<ListView android:longClickable="true">
or in java class
listView.setLongClickable(true)
You can also manage multiple touch my implementing your custom touch listener. Please refers this link for detail.
Elapsed time between first and second ACTION_DOWN
I want to perform some actions on the appearance of a DropDown spinner.
I cannot find any way to get "onShowListener"
Is there such a listener ?
You can achieve this implementing your own SpinnerAdapter.
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// Hide the keyboard here
}
This method is only invoked when the spinner appears.
You can find an example of a custom SPinnerAdapter here
Easiest solution would be to use the OnTouchListener. But maybe it gives you some unwanted side effects, because it gets also called, when you click on a list item.
BTW You can't set an OnClickListener, because it gives this Exception:
"java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead"
spinner.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Hide your Keyboard
return false;
}
});
setOnItemSelectedListener is available which will give you which item is selected
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Have you tried adding setOnClickListener and then check if spinner isShown or not
I have an Android application. In one of my Activities which is derived from ListActivity, I've implemented the OnItemLongClickListener. I want to enable a delete button within the relevant list item where the ListItem has been LongClicked. How can I do this?
OnItemLongClickListener listener = new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
Account a = null;
a = (Account) av.getItemAtPosition(position);
Toast.makeText(AccountActivity.this, "Long Clicked : " + a.getAccountName(), Toast.LENGTH_LONG).show();
//instead of the toast, I need to show/enable a button here...
}
};
getListView().setOnItemLongClickListener(listener);
.xml
<Button
android:id="#+id/imgdelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
.java
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
view.findViewById(R.id.imgdelete).setVisibility(View.INVISIBLE);
return false;
}
});
in your get view method of adapter set unique id to your button
btn.setId(position);
then on your click listener
OnItemLongClickListener listener = new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
Button btn = (Button) v.findViewById(position);
btn.setEnabled(true);
}
};`
Suppose you had a Button inside ListView's row layout then you can make it visible true`
OnItemLongClickListener listener = new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
Account a = null;
a = (Account) av.getItemAtPosition(position);
v.findViewById(R.id.btnid).setVisiBility(View.VISIBLE);
}
};`
You can add boolean flag isDeleteVisible to Account with default false value.
Then in OnItemLongClickListener set it to true and call adapter.notifyDataSetChanged()
In adapter's getView check isDeleteVisible and show or hide delete button.
how can i use both OnItemClickListener and OnItemLongClickListener or just disable longclick on a list view?
i've override OnItemLongClickListener and when i return true on onItemLongClick the longclick will disable but OnItemClickListener wont respond anymore.
DailyReportList.setOnItemLongClickListener(new OnItemLongClickListener(){
#Override
public boolean onItemLongClick(AdapterView parentView, View childView, int position, long id) {
return true;}});
registerForContextMenu(DailyReportList);
DailyReportList.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
arg1.showContextMenu();
FillTxtWithListItems(arg1);
}});
If you want to disable onClick or onLongClick you just need to uncheck/check the feature in your layout xml properties. Find the image on how it can be disabled..
If you do not want long click listener nor context menu just do not set any and do not call registerForContextMenu(). Lists do not have these by themselves.
How do I listen to click event on a ListView?
This is what I have now
ListView list = (ListView)findViewById(R.id.ListView01);
...
list.setAdapter(adapter);
When I do the following
list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parentView, View childView,
int position, long id)
{
setDetail(position);
}
public void onNothingSelected(AdapterView parentView) {
}
});
That doesn't seem to do anything on click.
And all those code live within a class that extends Activity.
On your list view, use setOnItemClickListener
Suppose ListView object is lv, do the following-
lv.setClickable(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object o = lv.getItemAtPosition(position);
/* write you handling code like...
String st = "sdcard/";
File f = new File(st+o.toString());
// do whatever u want to do with 'f' File object
*/
}
});
You need to set the inflated view "Clickable" and "able to listen to click events" in your adapter class getView() method.
convertView = mInflater.inflate(R.layout.list_item_text, null);
convertView.setClickable(true);
convertView.setOnClickListener(myClickListener);
and declare the click listener in your ListActivity as follows,
public OnClickListener myClickListener = new OnClickListener() {
public void onClick(View v) {
//code to be written to handle the click event
}
};
This holds true only when you are customizing the Adapter by extending BaseAdapter.
Refer the ANDROID_SDK/samples/ApiDemos/src/com/example/android/apis/view/List14.java for more details
The two answers before mine are correct - you can use OnItemClickListener.
It's good to note that the difference between OnItemClickListener and OnItemSelectedListener, while sounding subtle, is in fact significant, as item selection and focus are related with the touch mode of your AdapterView.
By default, in touch mode, there is no selection and focus.
You can take a look here for further info on the subject.
This solution is really minimalistic and doesn't mess up your code.
In your list_item.xml (NOT listView!) assign the attribute android:onClick like this:
<RelativeLayout android:onClick="onClickDoSomething">
and then in your activity call this method:
public void onClickDoSomething(View view) {
// the view is the line you have clicked on
}
You have to use setOnItemClickListener someone said.
The code should be like this:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text or do whatever you need.
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
First, the class must implements the click listenener :
implements OnItemClickListener
Then set a listener to the ListView
yourList.setOnItemclickListener(this);
And finally, create the clic method:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +countries[+ position], Toast.LENGTH_SHORT).show();
}
you can take a look and download code here
Use setOnItemClickListener() api in your activity. Following is the sample.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<> parent, View view, int position, long id)
{
// your code here.
}
});
In Kotlin, add a listener to your listView as simple as java
your_listview.setOnItemClickListener { parent, view, position, id ->
Toast.makeText(this, position, Toast.LENGTH_SHORT).show()
}