Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set long click listener for listview

I have below codes:

public class MainActivity extends ListActivity { 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}   
protected void onListItemClick(ListView l, View v, final int position, long id) {
    super.onListItemClick(l, v, position, id);
}}

I need to change this to onListItemLongClick() but how? Is it possible?

like image 830
Tokenizer Avatar asked Jul 05 '15 05:07

Tokenizer


4 Answers

Your question is very similar to this one, but it looks like it's not an exact duplicate.

What you've noticed is that the ListActivity class does not have a method override specifically for this case.

In order to add this functionality as a method override, your class should implement the AdapterView.OnItemLongClickListener interface, and then you can add the onItemLongClick() method override, which acts just as the onListItemClick() method override you already have, but responds to long clicks.

Just make sure that you follow instructions from this answer, you must use android:longClickable="true" in the layout xml, or call listview.setLongClickable(true);

Example:

public class MainActivity extends ListActivity implements AdapterView.OnItemLongClickListener {

    ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listview = (ListView) findViewById(R.id.list);

        listview.setLongClickable(true);

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> l, View v,
                                   final int position, long id) {

        Toast.makeText(this, "long clicked pos: " + position, Toast.LENGTH_LONG).show();

        return true;
    }

    protected void onListItemClick(ListView l, View v, final int position, long id) {
        super.onListItemClick(l, v, position, id);

        Toast.makeText(this, "short clicked pos: " + position, Toast.LENGTH_LONG).show();  

    }

 //....................
like image 140
Daniel Nugent Avatar answered Nov 06 '22 01:11

Daniel Nugent


you can simply do it with setOnItemLongClickListener

listview.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            Toast.makeText(ClassName.class, "Long Clicked Trigger: ", Toast.LENGTH_LONG).show();
            return true;
        }
});
like image 42
Inzimam Tariq IT Avatar answered Nov 06 '22 01:11

Inzimam Tariq IT


try this

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View v,
                int index, long arg3) {

             // write your code

            return false;
        }}); 
like image 2
SUNIL GOWROJI Avatar answered Nov 06 '22 02:11

SUNIL GOWROJI


You could implement the listener, then the callback would appear as a function (method) in your class:

public class MainActivity extends ListActivity implements View.OnLongClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View listView = findViewById(R.id.list_view);
        listView.setOnLongClickListener(this);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
    }

    @Override
    public boolean onLongClick(View v) {
        // Do your work here
        return false;
    }

}
like image 1
Simas Avatar answered Nov 06 '22 03:11

Simas