Skip to content Skip to sidebar Skip to footer

Opensearch() In Android Beginners App Not Defined

I'm just started the Android beginners tutotial and I now face a problem. On this page under 'Respond to Action Buttons' it tells me to define a switch statement with some of the o

Solution 1:

These methods are just examples that Google put in to show how you would use a switch statement. You can put anything you want in there, but I think the point is to make function calls from a switch statement, instead of putting the code of a function in the statement, to keep code clean. The functions would probably be declared in the same .java file in some fashion like

privatevoidopenSearch() {
    // start or show the search activity/fragment
}

They can technically contain anything you want them to, depending on what you want the action bar button to do. If you simply want to see that the buttons work, you can splash a Toast notification to see something appear

privatevoidopenSearch() {
    Toast.makeText(this, "Search button pressed", Toast.LENGTH_SHORT).show();
}

You'll have to import the Toast package which can be done by Ctrl+Shift+O. (Or Cmd+Shift+O for Mac)

Hope this helps clear up confusion.

Solution 2:

This is the code that you have to use in those methods:

privatevoidopenSearch(){
    startActivity(new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH));
}

privatevoidopenSettings(){
    startActivity(new Intent(Settings.ACTION_SETTINGS));
}

The openSearch() method execute the google global search of the cellphone. The openSettings() method open the global configuration of the cellphone.

I'm also a beginner in android,hope this helps with the question. Good Luck

Solution 3:

drees (thanks for Toast) answered the question for the satisfaction of the beginning android programmer (which I am), but to actually answer the question asked, you need to follow the instructions in the Setting Up The Search Interfacearticle, however you need to preemptively understand how to place the commented material here into the switch statement's openSearch() method.

I guess the real question is, though: Why does the android tutorial use this methodology instead of having a nonlinear explanation to add real content as the situation requires OR at least link to it to follow it out OR at least let you know that you're going to be obtain an end product that is at best broken code.

Post a Comment for "Opensearch() In Android Beginners App Not Defined"