0

So I'm basically following this tutorial to learn the basics of programming, and at respond to action buttons they have this coding:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_search:
            openSearch();
            return true;
        case R.id.action_settings:
            openSettings();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

but they dont talk about the case part at all, and now I don't understand what to do. I think (?) i need to create a method for the opensearch() and opensettings(), but what do i put in here, and what does the case part mean? thanks for your help!

0

3 Answers 3

0

The switch | case structure is a conditional statement. The switch receives a variable that'll be compared to one or several values. I usually think of it as an elegant way of writing if statements.

Each case is a comparison to be made to the value stored in the variable provided to the switch. In this case, R.id.action_search and R.id.action_settings are both ID's (string or number. In this case I don't know which, since I haven't used the tutorial) that must be compared to the value of item.getItemId().

The default is the action that will be performed if none of the previous case statements are met.

So, depending on the value of item.getItemId(), possible actions are either the execution of openSearch() or openSettings() (both ending with return true). But, if neither condition is met, then no methods will be executed and the value returned will be super.onOptionsItemSelected(item)

So, if you cannot find, in that tutorial, the code for those two methods, then it's most likely an abstraction thrown at you to illustrate what actions would take place if conditions in the code were met.

0

Its just a template by Google to show you/world about how you can handle clicks on Menu Items (Action Bar) in android. If we talk about this particular case, then in this case they are handling the click for two menu items 1. Search 2.Settings.

To accomplish the above task they have used switch (you can also use if and else statements) to verify which item has been clicked.

 switch (item.getItemId()) {   // Here they are checking the Id of item been clicked
    case R.id.action_search:   // Here they are examining if search item is clicked
        //openSearch();          // if above case satisfies, then they gonna invoke the openSearch() method.
        Toast.makeText(getApplicationContext(), "Pit Bull", Toast.LENGTH_LONG).show();  
        return true;
    case R.id.action_settings: // Here they are examining if action item is clicked 
        //openSettings();        // if above case satisfies, then they have invoked the openSettings() method. 
        Toast.makeText(getApplicationContext(), "Eminem", Toast.LENGTH_LONG).show(); 
        return true;

You can do whatever you want to do inside these cases by replacing your own logic e.g : You can show here a Toast Like this

  Toast.makeText(getApplicationContext(), "Pit Bull", Toast.LENGTH_LONG).show();

Its good that you want to learn programming, but its necessary that you should have basic knowledge of java first otherwise it will be quite hard to understand/learn the Android.

Best of luck..

0

Put whatever other code you want, those are sample methods, in place of methods you can log something to logcat, as Log.w("Test", "search button clicked");

basically the case part contains the action to be performed on click of button, like you may start a new activity, print something, set a log, whatever code you want on click, you can put it in the case of that particular button.

Not the answer you're looking for? Browse other questions tagged or ask your own question.