Long and short clicks
I want to set up a clickable list in my application. It needs to respond to both long and short clicks. Because there are a few ways to deal with events in Android this was a bit tricky, but I have it working now and seeing it here might simplify it a bit for other people.
Displaying the contents of an array in a ListView
This was the first thing I needed to do. Otherwise I have nothing to long and short click.
Doing this involves 3 steps.
Straightforward enough. The my_layout.xml file just contains a single TextView. There is no layout element wrapping the view. I could have used one of the standard android layouts (android.R.layout.simple_list_item_1 for example), but I am trying to get an understanding of what is happing, so just rolled my own.
Next there are 2 event listener setters which you have to call. One for normal click and the other for long click:
the listToast method just shows a piece of toast when the event is fired. If the long click has been used the value shown is in caps.
Note that the long click method returns a boolean. This is set to false by default, meaning that the method has not 'consumed' the event. In this case it will propagate to the default event handler which matches the event type. Changing this to true is the way to go if you are happy that you have dealt with the event and don't want it to propagate.
So this is what it looks like. Clicking on an item shows the text of the item as Toast, long clicking shows the toast in upper.
Displaying the contents of an array in a ListView
This was the first thing I needed to do. Otherwise I have nothing to long and short click.
Doing this involves 3 steps.
- Create a suitable Adapter object which will contain the data and know how to display it
- Get a reference to a ListView which you have placed in your Activity via the XML layout file
- Call the setAdapter method on that view to link the 2 together
Here is the code:
String[] vals = { "Anthony", "Emma", "Aoife" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getApplicationContext(), R.layout.my_layout, vals);
ListView lv = (ListView) findViewById(R.id.ListView01);
lv.setAdapter(adapter);
Straightforward enough. The my_layout.xml file just contains a single TextView. There is no layout element wrapping the view. I could have used one of the standard android layouts (android.R.layout.simple_list_item_1 for example), but I am trying to get an understanding of what is happing, so just rolled my own.
Next there are 2 event listener setters which you have to call. One for normal click and the other for long click:
lv
.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent,
View view, int position, long id) {
listToast(view, true);
return true;
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
listToast(view, false);
}
});
the listToast method just shows a piece of toast when the event is fired. If the long click has been used the value shown is in caps.
Note that the long click method returns a boolean. This is set to false by default, meaning that the method has not 'consumed' the event. In this case it will propagate to the default event handler which matches the event type. Changing this to true is the way to go if you are happy that you have dealt with the event and don't want it to propagate.
So this is what it looks like. Clicking on an item shows the text of the item as Toast, long clicking shows the toast in upper.
Comments
Post a Comment