Pages

Friday, February 19, 2016

Android Menus part 2 Handling Menu Items Events

We can handle menu items events by three ways:
  1. By implementing onOptionsItemSelected method.
  2. By implementing listeners to single menu items.
  3. By using intents.
Using onOptionsItemSelected method:
Implement the onOptionsItemSelected method like this:
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
TextView txt=(TextView)findViewById(R.id.txt);
txt.setText("you clicked on item "+item.getTitle());
return true;
}

Notice that it returns a Boolean which should be true to execute the method, if false it will not execute.

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 OnMenuItemClickListener
then 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 don’t handle it in onOptionsItemSelected method but invoke the parent onOptionsItemSelected.

Scenario 1:
@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 true;
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 the dialer intent will not be launched.

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.

Related Posts by Categories

0 comments:

Post a Comment