- By implementing onOptionsItemSelected method.
- By implementing listeners to single menu items.
- By using intents.
Implement the onOptionsItemSelected method like this:
@OverrideNotice that it returns a Boolean which should be true to execute the method, if false it will not execute.
public boolean onOptionsItemSelected(MenuItem item)
{
TextView txt=(TextView)findViewById(R.id.txt);
txt.setText("you clicked on item "+item.getTitle());
return true;
}
You can switch between the menu items like this:
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
TextView txt=(TextView)findViewById(R.id.txt);
switch(item.getItemId())
{
case 1:
txt.setText("you clicked on item "+item.getTitle());
return true;
case 2:
txt.setText("you clicked on item "+item.getTitle());
return true;
case 3:
txt.setText("you clicked on item "+item.getTitle());
return true;
}
return super.onOptionsItemSelected(item);
}
Notice that we return true for every handled menu item. And for un handled menu items (outside switch block) we call the super class method.
Using listners:
We can handle options menu items click events by making the activity implement onMenuItemClickListner interface provide an implementation of onMenuItemClick method like this:
public class MenusDemo extends Activity implements OnMenuItemClickListenerthen implement the method:
public boolean onMenuItemClick(MenuItem item) {
TextView txt=(TextView)findViewById(R.id.txt);
txt.append("listner");
return false;
}
Notice that the method returns a Boolean. If it returns true no other callbacks will be executed. If returns false then onOptionsItemSelected callback will be executed directly after this callback.
Using intents:
You can specify an intent to be launched when an options menu item is clicked like this:
menu.add(1, dialItem, 1, "Dial").setIcon(R.drawable.dvd).setIntent(new Intent(Intent.ACTION_DIAL));
note that if you specify an intent for an item and at the same time override the onOptionsItemSelected method and handle the selection for that item, the precedence of execution is to the code in the onOptionsItemSelected.
meaning that the code in onOptionsItemSelected method will be executed first, if it returns true then the intent will not be launched, if returns false then the intent will launch.
So if you want to use intent for menu items dont handle it in onOptionsItemSelected method but invoke the parent onOptionsItemSelected.
Scenario 1:
@OverrideThis will execute the code of the onOptionsItemSelected method and the dialer intent will not be launched.
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(1, dialItem, 1, "Dial").setIcon(R.drawable.dvd).setIntent(new Intent(Intent.ACTION_DIAL)); return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
TextView txt=(TextView)findViewById(R.id.txt);
switch(item.getItemId())
{
case 1:
txt.append("you clicked on item "+item.getTitle());
return true;
case dialItem:
txt.setText("you clicked on item "+item.getTitle());
return true;
case 3:
txt.setText("you clicked on item "+item.getTitle());
return true;
}
return super.onOptionsItemSelected(item);
}
Scenario 2:
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(1, dialItem, 1, "Dial").setIcon(R.drawable.dvd).setIntent(new Intent(Intent.ACTION_DIAL)); return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
TextView txt=(TextView)findViewById(R.id.txt);
switch(item.getItemId())
{
case 1:
txt.append("you clicked on item "+item.getTitle());
return true;
case dialItem:
txt.setText("you clicked on item "+item.getTitle());
return false;
case 3:
txt.setText("you clicked on item "+item.getTitle());
return true;
}
return super.onOptionsItemSelected(item);
}
This will execute the code of the onOptionsItemSelected method and then dialer intent will be launched.
Scenario 3:
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(1, dialItem, 1, "Dial").setIcon(R.drawable.dvd).setIntent(new Intent(Intent.ACTION_DIAL)); return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
TextView txt=(TextView)findViewById(R.id.txt);
switch(item.getItemId())
{
case 1:
txt.append("you clicked on item "+item.getTitle());
return true;
case 3:
txt.setText("you clicked on item "+item.getTitle());
return true;
}
return super.onOptionsItemSelected(item);
}
the onOptionsItemSelected does not handle the "dialerItem" selection, so The dialer intent will be launched directly.
0 comments:
Post a Comment