Android Action Bar Responding to users - android

I am having issues with some methods with my app in android. I'm trying to respond to a button pressed by a user. Here is the method:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onContextItemSelected(item);
}
}
I was looking at the docs provided from google about this and it says those methods should be called depending on the user's action. Am I missing something?
The error messages area:
Error:(42, 17) error: cannot find symbol method openSearch()
Error:(46, 17) error: cannot find symbol method openSettings()
Any help would be appreciated!
Thanks

You have not defined the methods openSettings() and openSearch() inside the Activity where you define onOptionsItemSelected.
The result of this is that the compiler will tell you that it cannot find symbol method openSearch() and cannot find symbol method openSettings()
You simply have to add the method declaration inside the Activity:
private void openSettings(){
//Execute relevant code
}
private void openSearch(){
//Execute relevant code
}

The above function doesn't get executed on Button pressed event. It is executed when user selected an item from menu.
At the moment, compiler doesn't know if such method signatures exist in the class. You would need to define the functions inside the class, then use them. I guess it will work fine.

Related

Crashlytics not being called?

Trying to call from static function? Its initialized because it calls from the onCreate of the activity. Wondering how crashlytics works.. does it require reference to some context that is somehow not present. Here is some code:
Calling from the activities menu override:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.explore:
ListFragment.injectNewList(ListActivity.this, Stuff.getRandOffset());
break;
default:
break;
}
return true;
}
Calling function is a static function within a fragment:
public static void injectNewList(FragmentActivity activity, Integer offset)
{
ListFragment fragment = (ListFragment) activity.getSupportFragmentManager()
.findFragmentByTag(BaseFragmentActivity.LIST_FRAGMENT_TAG);
if(fragment != null)
{
fragment.nextOffset = offset;
FFData.getInstance().clearList();
fragment.mListAdapter.notifyDataSetInvalidated();
fragment.loadItems();
}
else
{
Crashlytics.log(Log.ERROR, "Log this error", "bad stuff happened!");
}
}
The activity and fragment are fully running when the menu button is clicked. I also see that the code is run in the debugger. Running on genymotion(will try actual device), SDK 19, Nexus5
Make sure Crashlytics is initialized first by calling Crashlytics.start(this);
Crashlytics.log will message will be visible in your dashboard, associated with crash (Meaning if no crash/exception happens, log will not be sent...Crashlytics is a crash tracking service, if you need to track custom messages there are other tools for that).

MenuItem alpha value lost after orientation change

The Problem
On Android versions < 4.1, the alpha value of the MenuItem is getting reset after an orientation change, BUT it remains disabled.
The code I'm using
DetailsFragment.java
public class DetailsFragment extends SherlockFragment {
private MenuItem miEmail;
...
#Override
public void onPrepareOptionsMenu(Menu menu) {
miEmail= menu.findItem(R.id.menu_email);
super.onPrepareOptionsMenu(menu);
}
private void populateDetails(final Detail oDetail) {
//disable email button if dealer doesn't have option
if(!oDetail.bShowSAM){
miEmail.setEnabled(false);
miEmail.getIcon().setAlpha(50);
}
...
}
}
MyManifest.xml
<activity
android:name=".activities.DetailsActivity"
android:uiOptions="splitActionBarWhenNarrow"
android:configChanges="keyboardHidden|screenSize|orientation">
</activity>
What I expect to happen
When the orientation changes, miEmail is still disabled and the alpha value is still at 50.
What is actually happening
When testing on older devices(2.3,4.0), the MenuItem is remaining disabled but the alpha value is getting reset to the default value. When testing with my devices that are >4.1, it is working as expected.
What I've tried
Googling the problem.......
I've tried to avoid using the android:configChanges="..." and handling the data through savedInstanceState, but I've learned you can't make the MenuItem serializable/parciable, thus not allowing me to pass it through outState bundle object.
I'm fairly new to Android development and I feel as though there is a trivial way of handling this MenuItem, but I cannot figure how else to handle it.
What do you think is the issue?
Any feedback will be greatly appreciated.
Dont set the icon alpha on your custom function, instead, set it on OnPrepareOptionsMenu (with a suitable conditional). You can pass a boolean on savedinstancestate saying whether it should be grayed or not.
In your populateDetails function, you would call invalidateOptionsMenu() to make android remake the action bar icons. Example:
private boolean buttonEnabled;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem miEmail= menu.findItem(R.id.menu_email);
if (buttonEnabled) {
miEmail.setEnabled(true);
miEmail.getIcon().setAlpha(255);
}else{
miEmail.setEnabled(true);
miEmail.getIcon().setAlpha(50);
}
return super.onPrepareOptionsMenu(menu);
}
private void populateDetails(final Detail oDetail) {
//disable email button if dealer doesn't have option
if(!oDetail.bShowSAM){
buttonEnabled = false;
InvalidateOptionsMenu();
}
...
}
}
If you are using the support library for compatibility, use supportInvalidateOptionsMenu instead.
By the way, never use the orientation tag to "fix" the problem, the issue will still appear if you quit the app for a long time and then try to open it. (android pauses the activity initially and will stop it after a while)

Android Development: Undefined Method

Hi I´m new to Android and Eclipse. I have just following the tutorial from developer.android.com. Right now I´m in adding ActionBar
Right now I´m at this part
#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);
}
}
I have received an error for openSearch() and openSettings(). It said that The method openSettings() is undefined for the type DisplayMessageActivity. What shoud I do now?
Thanks
openSearch() and openSettings() are methods that the author of the tutorial created in order to perform other operations. Search well into the code, there must be somewhere the declaration of those methods, if the author made them visible.
They should look something like this:
public void openSearch() {
//Do something here.
}
public void openSettings() {
//Do something here.
}
Replacing the //Do something here with the code implementation present in the tutorial.
Im up to the same section as you, they haven't provided the methods but you have to implement them as stated above.
However I found code to open up the device settings using this code in the switch;
case R.id.action_settings:
startActivity(new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS));
return true;
define them.
You're basing your code on an incomplete snippet. That snippet makes no expectation of what it means to search or create settings in your app... that's your job to implement. This snippet is only concerned about showing you how to establish the action bar, not the whole application.
The methods openSearch() and openSettings() should be defined. Use the following code. It'd help..
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id){
case R.id.action_search :
startActivity(new Intent(Settings.ACTION_SEARCH_SETTINGS));
return true;
case R.id.action_settings :
startActivity(new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS));
return true;
default :
return super.onOptionsItemSelected(item);
}
}
Maybe you should code those methods?
private void openSearch(){
//your code here
}
private void openSettings(){
//your code here
}
Those two methods are just examples how selecting an option can start an action. The implementation was not provided because it was irrelevant to the example. Note that it is not a tutorial, but a single and un-compile-able example of how to add behavior to an options item.

android usage of "controller.query(activity);" in scoreloop

i am attempting to implement a built in controller that is part of the scoreloop library. the documentation states:
Basic Usage:
To invoke the TOS dialog if it was not accepted previously, the following code may be used:
final TermsOfServiceController controller = new TermsOfServiceController(new TermsOfServiceControllerObserver() {
#Override
public void termsOfServiceControllerDidFinish(final TermsOfServiceController controller, final Boolean accepted) {
if(accepted != null) {
// we have conclusive result.
if(accepted) {
// user did accept
}
else {
// user did reject
}
}
}
});
controller.query(activity);
but when i paste this into my code i get the following syntax errors:
am i using this incorrectly? how and where would this be used any ideas?
EDIT: after moving the statement to the method where i want to show the dialog i now get the following error:
You seem to be calling controller.query(activity) in a class body where a declaration is expected. Move the statement controller.query(activity) to a method where you would like to show the dialog.

Menu item IDs in an Android library project?

The Android app uses a library project to contain most of the app code, as there are two versions of the app built from the core source. Since an IntelliJ IDEA update (to v11) I'm getting this warning on each of the case statements below:
Resource IDs cannot be used in a switch statement in Android library modules
Here's the code:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_one: // Build error here
// Do stuff
return true;
case R.id.menu_item_two: // Build error here
// Do stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
OK, so if I can't reference them via their ID, how DO I reference them?
Substitute the switch with an if/else if construct.
int id = item.getItemId();
if(id == R.id.menu_item_one) {
// ...
}
else if(id == R.id.menu_item_two) {
// ...
}
This is neccessary since ADT 14 because the final modifier was removed from id's in the R class.
See Non-constant Fields in Case Labels

Categories

Resources