Open main menu

CDOT Wiki β

Changes

MAP524/DPS924 Lecture 5

1,227 bytes added, 22:06, 17 July 2015
ListView
lv.setAdapter(aa);</pre>
* You can now run your app to see your scrollable list of colours.
* Now add an onClickListener to your listview list view like this:
<pre>lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
* Run your app again and this time select a colour by clicking it.
* You should see a Toast with the selected colour name pop up.
 
= Context Menu =
 
Add a Context Menu to the Scrollable List in the ListView example above. [http://developer.android.com/reference/android/view/ContextMenu.html Official documentation here].
 
* First let's register the list view for a Context Menu by adding this line to our activity :
<pre>registerForContextMenu(lv);</pre>
* Now add this method to create our context menu:
<pre>@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Make a Phone Call");
menu.add(0, v.getId(), 0, "Send an SMS Message");
}</pre>
* Now add the listener method like this:
<pre>@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Make a Phone Call") {
Toast.makeText(getApplicationContext(), "making a call", Toast.LENGTH_LONG).show();
} else if (item.getTitle() == "Send an SMS Message") {
Toast.makeText(getApplicationContext(), "sending a message", Toast.LENGTH_LONG).show();
} else {
return false;
}
return true;
}</pre>
* You can now run your app and test the context menu.