Short/Long press in a item of ListView - android

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/

Related

List View with two actions in 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

How do I enable click on listview?

I'm currently working on a code which is written by someone else in my office. I need to enable selection of items in the listview. I know I have to use setOnClickListener. Can someone guide me through this? I've written:
Edit:
listview.setOnClickListener( (View.OnClickListener) this );
Now what do I do?
I need to select an int value and pass it on to another function, which is used to retrieve a certain set of values from a db.
listviewName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//variable position will give you the required element/object from the array list.
}
});
YourListView.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int pos, long l) {
try {
Toast.makeText(this,"Position is===>>"+pos , Toast.LENGTH_LONG).show();
}
catch(Exception e) {
System.out.println("Nay, cannot get the selected index");
}
}
});
Hope it helps.
ListView list = (ListView)findViewById(R.id.quoteX2);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// - -- - -Your Code here
}
});
Hope it helps!

Android - ListView multiple choice only selecting 2 items

Anybody know why i'm only able to select 2 items after activating my ListView to Multiple Choice.
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//Activate CHOICE_MODE_MULTIPLE
multipleActivated = true;
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setItemChecked(position, true);
Log.w("Multiple Selection", "Activated");
return true;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (multipleActivated == false) {
//Perform normal click and call new intent
.....
} else {
//set the clicked item to be checked since the Multiple Selection is activated
listView.setItemChecked(position, true);
Log.w("Selection", String.valueOf(position));
}
}
});
As you can see in my code, i only want to activate the Multiple Choice Mode once the LongPress is perform, this ok and working fine but im oly able to select 2 items, on the 3rd selection the first selected item just deselecting. i don't know why. anybody can help me here? Thank You!

How to prevent many clicks on a button that launches several sometimes a dialog by default? (android bug)

Events of default click (eg a list):
listviewAvailables.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,final int position, long id) {
showDialogStartStimulation();
}
});
If I double-click fast, it starts 2 times the dialog ...
I have fixed this by using a variable:
boolean oneClickDialog = false;
...
listviewAvailables.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,final int position, long id) {
if(!oneClickDialog){
showDialogStartStimulation();
oneClickDialog = true;
}
}
});
...
public void showDialogStartStimulation(){
..
pDialog.isShowing(){
oneClickDialog = false;
..
}
Is not there a default solution? Need more simple as a "return false" or something?
To correct this double click, really should I apply my solution to all events of the app to launch a dialog?

How to handle ListView click in Android

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()
}

Categories

Resources