ewok
ewok

Reputation: 21443

how to handle long click in Android

I am new to Android dev. The way I have been handling clicks has been by setting the android:onClick attribute in the manifest file for buttons. What I am wondering is the best way to handle long clicks in general. I have read about implementing onLongClick(), but is there a way to use handlers (like above), rather than having to extend View? It would be very helpful, as I would rather not have to rebuild my entire project with an extended View class.

EDIT

I should clarify. I have a ListView and I want to set what will happen when I long click on an element in the list. Each element in the list is a TextView. As per one of the answers, I have added the below code, and now I get a force close:

public class TwitterActivity extends ListActivity {
    List<String> tweets = new LinkedList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setListAdapter(new ArrayAdapter<String>(this, R.layout.layout, tweets));

            TextView view = (TextView) findViewById(R.id.ListTemplate);
            view.setOnLongClickListener(new OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    Toast toast = new Toast(TwitterActivity.this);
                    toast.setText("LongClick");
                    toast.show();

                    return true;
                }
            });

    //...
    }
}

Upvotes: 3

Views: 18869

Answers (5)

Matt Wolfe
Matt Wolfe

Reputation: 9284

For a ListActivity if you want to respond to long clicks on the list elements do this:

public class TwitterActivity extends ListActivity {
    List<String> tweets = new LinkedList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setListAdapter(new ArrayAdapter<String>(this, R.layout.layout, tweets));
            ListView lv = getListView();
            lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){ 
                   @Override 
                   public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) 
                  { 
                       Toast.makeText(TwitterActivity.this, "LongClick", Toast.LENGTH_LONG).show();
                  } 
             }); 

    }
}

For a regular activity you could do something like this:

public class MyActivity extends Activity implements View.onLongClickListener {

   View myView = null;


   public void onCreate(Bundle state) {
      super.onCreate(state);
      setContentView(R.layout.my_activity);
      myView = findViewById(r.id.my_view);
      myView.setOnLongClickListener(this);
   }

   @Override
   public void onLongClick(View v) {
    //long clicked
   }

}

Upvotes: 6

kabuko
kabuko

Reputation: 36302

You don't have to extend the View class in most cases. View has a method called setOnLongClickListener which you can use directly as all derived classes like Button or TextView, etc. will also have.

Upvotes: 1

ByteMe
ByteMe

Reputation: 1476

Sure this is fairly simple:

ImageButton i = (ImageButton) findViewById(R.id.myButton);
i.setOnLongClickListener(new myLongListener());

private class myLongListener implements View.OnLongClickListener {
    @Override
    public void onClick(View v) {
        //your code here
    }
}

hope this helps!

Upvotes: 1

Caner
Caner

Reputation: 59158

The only event handler that has an XML attribute is android:onClick. All other event handlers are registered at runtime from Java code. Technically, even android:onClick is registered at runtime from Java code, but you do not have to write the Java code in question.

So you need to do something like this:

View.OnLongClickListenerhandler = View.OnLongClickListener() {
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.myButton: // doStuff
                break;
            case R.id.myOtherButton: // doStuff
                break;
        }
    }
}

findViewById(R.id.myButton).setOnLongClickListener(handler);
findViewById(R.id.myOtherButton).setOnLongClickListener(handler);

Upvotes: 0

jsimpson
jsimpson

Reputation: 391

get a handle to the button using findViewByID, then call setOnLongClickListener.

Button b = (Button)findViewByID (R.id.button1);
b.setOnLongClickListener(new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        //to do
    }
});

Upvotes: 2

Related Questions