3

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

    //...
    }
}
ewok
  • 20,148
  • 51
  • 149
  • 254

5 Answers5

6

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
   }

}
Matt Wolfe
  • 8,924
  • 8
  • 60
  • 77
2

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
    }
});
jsimpson
  • 391
  • 1
  • 5
  • I tried this, but it did not work. I am using a ListView and have each element of the List as a TextView. My code is in an edit above. – ewok Mar 06 '12 at 19:02
  • I see that you are using R.layout.layout, which does not look correct. You must specify the id of the TextView you are using (which should be in R.id.) – jsimpson Mar 06 '12 at 19:07
  • Still force closing even when I change to `R.id.ListTemplate` – ewok Mar 06 '12 at 19:09
  • What is the exception? It's probably a nullpointer exception. Make sure that view is non-null. – jsimpson Mar 06 '12 at 19:12
  • `03-06 14:08:31.349: E/AndroidRuntime(393): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.mitre.ewortzman/org.mitre.ewortzman.TwitterActivity}: java.lang.NullPointerException` How do I make sure it's non-null? I'll post my activity class in a minute – ewok Mar 06 '12 at 19:14
  • Are you sure that the element in the listview is purely a textview? If so,then make sure you are calling this code every time you inflate your view (make sure each textview in the listview has the onlongclicklistener set). If not, then make sure you cast it as that type. – jsimpson Mar 06 '12 at 19:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8597/discussion-between-ewok-and-jeremys) – ewok Mar 06 '12 at 19:53
1

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!

ByteMe
  • 1,436
  • 12
  • 15
1

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.

kabuko
  • 36,028
  • 10
  • 80
  • 93
0

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);
Caner
  • 57,267
  • 35
  • 174
  • 180