Android - Refresh Options Menu without calling invalidateOptionsMenu() - java

Hello Android Developers,
I have seen a lot of question regarding the update of Options Menu. However, every answer says I have to call invalidateOptionsMenu().
My question is, is there any other way of updating the Options Menu without invoking the method invalidateOptionsMenu()?

#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear(); // Clear the menu first
/* Add the menu items */
return super.onPrepareOptionsMenu(menu);
}
This solved the problem on updating the OptionsMenu without invoking the invalidateOptionsMenu()

Related

can not click action bar app icon Android

I'm trying to make the action bar app icon (top left image) clickable but it just doesn't work, I've already searched for some answers but nothing works. I've already tried to getSupportActionBar().setHomeButtonEnabled(true); and I can see the icon, but i still can't click on it. I know I can handle the event in the method onOptionsItemSelected(MenuItem item) but the event will never get triggered with my situation. I also don't get why I have to use getSupportActionBar(); instead of getActionBar()...the second one is always null. The minimum sdk is 16 and the maximum is 22. I read this answer -> ActionBarCompat - App icon action (click) not working on 4.0 devices but I don't know how to get in the ActionBarHelperICS.java class or if it apply to my case.
You haven't really posted any code. But there may be 2 problems.
You have to specify which is the parent activity in your manifest file.
Under the activity's tag in manifest, you'll have to specify which activity your home button will point to. Something like this:
android:parentActivityName="com.example.app.MainActivity"
Or Override your onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//add what you want to do here...
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Reference to class file not working

I'm trying to make a simple RSS feed reader app. I've created a class file with the name "IotdHandler.java" and a public class with the same name. When I try and create a new instance of the class I get the following error: "Unreachable code".
If anyone can point me in the direction of a good tutorial about working with multiple class files I would be very grateful.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.nasa_daily_image, menu);
return true;
IotdHandler handler = new IotdHandler();
handler.processFeed();
}
Thats because you have a return statement before.
Unreachable code has nothing to do with the class/library, is a compiling error because there's no way the code below the "return true" will be executed in anyway, you should get a good Java Book before diving into Android.
Regards!

Setting splitActionBarWhenNarrow from Java on Android

I'm wondering if it would be possible to tell android to split the ActionBar only when I want it to, but on the same Activity. My use case is that by default, the actions I have on the bar are OK to be collapsed, but on a long click on an item, I enter an "Edit Task" mode, where the action bar is used to provide some shorthands to edit a task. I'd like this "edit mode" to use the split action bar, as it has icon's that are better off to be visible right away, and keep the "not split" action bar for the general view - where it's just "settings" etc.
So the question is, can I set android:uiOptions="splitActionBarWhenNarrow" from the code, instead of hardcode it in the Manifest?
PS: I'm using ActionBar Sherlock.
The native action bar can be set into split mode by calling getWindow().setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW).
Window UI options cannot be read after they are set so with ActionBarSherlock you have to call getSherlock().setUiOptions(...). You don't have to call both. ABS will automatically call the above when appropriate.
This must be done before the decor view has been created. The safest place to put this call to ensure that always happens is in your activity onCreate method before you call super.onCreate.
Take a look at the ActionBarSherlockSamples, SplitActionModes.java. In this example, when the button 'Start' is pressed, a split action bar shows up in the bottom of the screen:
Call this to show the split actionbar:
mMode = startActionMode(new AnActionModeOfEpicProportions());
mMode is type of ActionMode and you need to call 'finish()' on it when you want the action bar to go away.
AnActionModeOfEpicPropotions is an implementation of ActionMode.Callback:
private final class AnActionModeOfEpicProportions implements ActionMode.Callback {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// add your menu here...
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// handles your action menu clicked event
return true;
}
}
I was just trying if I could have an ActionBar in one Activity and a Split one in another.
I added the action bar in onCreateOptionsMenu in both the activities and added
getWindow().setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW);
right before onCreate in the activity I wanted the split action and to my surprise it worked. :)
Then I used getActionBar().hide() to hide the split action bar on button click and it worked too.
I guess if you could try this, you can get this working and customized to your need. Hiding it initially and onLongClick showing it up and after the necessary actions are performed hiding it again.
(The only thing I missed is checking this with support libraries. Will do and update you)
Hope, this might help you in some way. Happy coding :)
private final class AnActionModeOfEpicProportions implements ActionMode.Callback
{
#Override
public boolean onCreateActionMode(ActionMode mode,Menu menu)
{
// add your menu here...
}
#Override
public boolean onActionItemClicked(ActionMode mode,MenuItem item)
{
// handles your action menu clicked event
returntrue;
}
}

ActionBarSherlock - Share Content icon issue

I am using ActionBarSherlock and have implemented ShareActionProvider.
When a user selects an app to share content with, eg Twitter, the actionbar displays a Twitter icon next to the Share button icon. This stays there forever.
Does anybody know how to disable the application icon from appearing next to the Share button?
Found the answer:
Implement OnShareTargetSelectedListener and set it on the
ShareActionProvider
public void onCreateOptionsMenu(Menu menu){
....
actionProvider.setOnShareTargetSelectedListener(this);
....
#Override
public boolean onShareTargetSelected(ShareActionProvider source,
Intent intent) {
context.startActivity(intent);
// started activity ourself to prevent search history
return true;
}
The top target is featured in the action bar. This is the behavior of the widget as it exists in Android.
If you do not want this behavior copy the sources into your app and modify its behavior to never display the top target icon.

How do you write an extra class for often used code and call it from everywhere?

In my little app users can have a look at 'more information' or send me feedback when they click on the menu button (of the device) at every xml file of the app.
This is my code (when you click on the menu button):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.impressum:
startActivity(new Intent (this, MoreInformation.class));
break;
case R.id.feedback:
/*
*
*/
break;
}
return super.onOptionsItemSelected(item);
}
Now I put the code into all java classes. This obviously works but I'd like to create a class (Let's call it isMenuButtonPressed.class) which contains the code above and I'd like to remove the code above from all other java classes and just call isMenuButtonPressed.class (in these java classes when the menu button was pressed). How can you do that? How do you pass the information on that the menu button was pressed? How do you receive it in the isMenuButtonPressed.class?
Thanks!
How do you write an extra class for often used code and call it from everywhere?
By letting a custom class define the actual behaviour for your Menu.
Read my Asynchronous programming best practices answer for more information regarding this subject.

Categories