Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openSearch() in Android beginners app not defined

Tags:

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 options calling the openSearch() and openSettings() methods. These methods however, are not defined yet (duh) which thus gives me an error.

The tutorial doesn't say anything however, about how to define them. Can anybody give me a pointer on what and where I should define thess methods? Should they be in the same file, and if so, what should they contain?

like image 730
kramer65 Avatar asked Sep 10 '13 19:09

kramer65


2 Answers

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

private void openSearch() {     // 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

private void openSearch() {     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.

like image 135
dsrees Avatar answered Sep 19 '22 20:09

dsrees


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

private void openSearch(){     startActivity(new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH)); }  private void openSettings(){     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

like image 37
César Torres Avatar answered Sep 21 '22 20:09

César Torres