How to handle long click in Android
I am new to Android dev. The way I was handling clicks is to set an attribute android:onClick
in the manifest file for the buttons. I am wondering how best to handle long clicks in general. I've read about implementation onLongClick()
, but is there a way to use handlers (like above) instead of extending the view? It would be very helpful since I wouldn't have to rebuild the whole project with the extended View class.
EDIT
I have to clarify. I have ListView
, and I want to set what happens when I long press on an item in the list. Each item in the list is TextView
. According to one of the answers, I added the code below and now I have the power:
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;
}
});
//...
}
}
source to share
For ListActivity, if you want to respond to long clicks on list items, follow these steps:
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 regular activities, you can 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
}
}
source to share
Of course it's pretty 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!
source to share
In most cases, you don't need to extend the class View
. View
has a method setOnLongClickListener
that can be used directly since all derived classes like Button
or TextView
etc. will also be there.
source to share
The only event handler that has an XML attribute is android:onClick
. All other event handlers are registered at runtime from Java code. It is technically even android:onClick
registered at runtime from Java code, but you don't need to write the corresponding Java code.
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);
source to share