List View with two actions in android - android

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

Related

Short/Long press in a item of ListView

I have a ListView with some items and I want to have different responses when I make an itemClick (short press) and an itemLongClick (long press):
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//play file
}
});
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//Message
Toast.makeText(getBaseContext(), "FILE: "+itemsFiles.get(position).getName(), Toast.LENGTH_LONG).show();
return false;
}
});
When I make a short press (onItemClick()) my app works well and it plays the file.
My problem is when I make a long press (onItemLongClick()) because it appears the message with Toast but it plays the file too, and I do not want to play the file in this case ... How I can solve it and correctly distinguish the two cases?
Thanks a lot.
you may want to have look upon this tutorial : http://android.konreu.com/developer-how-to/click-long-press-event-listeners-list-activity/

Spinner on dropdown appears event

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

android: onListItemClick doesn't fire when onItemLongClick occurred

In have implemented the OnItemLongClickListener. When LongClicked the list item, I enable a button of the relevant listItem. After LongClicked the button enables successfully but onListItemClick doesn't get fired. If I onListItemClick without LongClicked, it fires successfully. Why does onListItemClick doesn't fire if LongClicked fired before that?
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();
v.findViewById(R.id.btn_delete).setVisibility(View.VISIBLE);
return false;
}
};
getListView().setOnItemLongClickListener(listener);
public void onListItemClick(ListView l, View v, int position, long id) {
// Do something when a list item is clicked
Account a = null;
a = (Account) l.getItemAtPosition(position);
Toast.makeText(AccountActivity.this, a.getAccountName(), Toast.LENGTH_SHORT).show();
}
Try to implement like below,
setOnItemLongClickListener
setOnItemClickListener
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
Account a = null;
a = (Account) av.getItemAtPosition(position);
Toast.makeText(AccountActivity.this, "Long Clicked : " + a.getAccountName(), Toast.LENGTH_LONG).show();
v.findViewById(R.id.btn_delete).setVisibility(View.VISIBLE);
return false;
}
});
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
Account a = null;
a = (Account) l.getItemAtPosition(position);
Toast.makeText(AccountActivity.this, a.getAccountName(), Toast.LENGTH_SHORT).show();
};
});
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();
v.findViewById(R.id.btn_delete).setVisibility(View.VISIBLE);
return false;
}
};
getListView().setOnItemLongClickListener(listener);
I just found the cause for the problem. It's the line of code I used to enable the delete button when OnItemLongClick is clicked.
v.findViewById(R.id.btn_delete).setVisibility(View.VISIBLE);
When I remove that line the code works perfectly. But still I ain't got a solutions to the problem. I need to have that line of code to enable the delete button when the OnItemLongClick is clicked.
I just replaced the button with an ImageView. Then there was no issue. The two events are working smoothly. So I realized there is something wrong with the button. I think we have to set some of the properties in the button to function properly in this situation. You can simulate the issue.
1. User a Listview.
2. Customize the list view by adding a TextView and an ImageButton or a Button as a ListItem. Make the visibility of the button to invisible by default.
3. Implement the onItemLongClick event as above and make the button when ItemLongClicked.
4. Once you ItemLongClicked a ListItem and make the button in the ListItem Visible, onListItemClick event of the particular ListItem does not work.

How to show/enable a button on ListView onItemLongClick

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.

ListView doesnt fire setOnLongClickListener, only setOnItemClickListener

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.

Categories

Resources