How do you add item to ActionBarSherlock after onCreate? - java

Okay, when my app starts it has two tabs, and one action in the action bar, when I switch to the second tab, i change the layout, and I want to remove the action I have in the bar, and add a different one, any easy way to accomplish this?

I got it working!
All you have to do is add conditional statements to your onCreateOptionsMenu, and then simply invalidate the menu when you switch the tab! Hope this helps someone!
Conditional:
public boolean onCreateOptionsMenu(Menu menu) {
if (getSupportActionBar().getSelectedNavigationIndex() == 1) {
menu.add("Share")
.setIcon(android.R.drawable.ic_menu_share)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}
if (getSupportActionBar().getSelectedNavigationIndex() == 0)) {
menu.add("Settings")
.setIcon(android.R.drawable.ic_menu_manage)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}
return true;
}
And to invalidate:
invalidateOptionsMenu();

I presume your tabs are Fragments within a FragmentActivity. In that case, you should setHasOptionsMenu(true) in tab fragments' onCreate() methods. And override onCreateOptionsMenu and onOptionsItemSelected of the fragments.
The rest will be done for you (like invalidating actionItems after switching tabs etc.)

Related

How to temporarily block the ability to click a menu item

I have a menu when I click on a specific element of which the activity opens, but the user can quickly click menu iten 2 times, which will lead to the opening of the activity 2 times. How can I block the ability to press a button after the first press?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.create) {
// start activity
}
return super.onOptionsItemSelected(item);
}
I know about setEnabled, but when I return to enabled activity, it remains false, and it seems inconvenient to save MenuItem and return its state. Anyone have any ideas how to do this optimally?
I found the best solution to this problem. To do this, add the FLAG_ACTIVITY_CLEAR_TOP flag to the Intent. Thanks to this, it will be impossible to create 2 activities.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

How can i change Menu Item icon programmatically?

I need to change my menu icon based on a condition, however when I use Menu.getItem(index).setIcon() it doesn't change my icon at all. I need help how to do that please
Menu.getItem(index).setIcon()
if (menu != null) {
if (observedCount != 0)
menu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_delete_copy));
else
menu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_cart));
Your code looks correct, but maybe you are using it just once inside onCreateOptionsMenu() and missing the calls that are necessary to update the icon as observedCount updates.
Approach 1
You can try doing the following in your activity:
First, override onPrepareOptionsMenu() and apply the changes there:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (observedCount != 0)
menu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_delete_copy));
else
menu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_cart));
return super.onPrepareOptionsMenu(menu);
}
Then, whenever you want the icon to be updated (i.e., whenever observedCount changes), just invoke:
invalidateOptionsMenu(); // From a fragment, call activity.invalidateOptionsMenu();
Approach 2
Another option is to save a reference to the menu inside onCreateOptionsMenu():
private Menu mMenu;
...
#Override
public void onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.your_menu, menu);
mMenu = menu;
}
Then, you can use that reference to set the icon:
if (observedCount != 0)
mMenu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_delete_copy));
else
mMenu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_cart));
That should hopefully do it!
P.S. Make sure you are getting the correct item when doing menu.getItem(2) (the item number 2 is actually the third one because the count starts at 0). To avoid getting the wrong item, it would be a good idea to set the item you want to change with an ID and then replace menu.getItem(2) with menu.findItem(R.id.your_item_id).

How to add toolbar menu items programmatically and set onOptionsItemSelected

How can I add menu items to the menu of the DrawerLayout in Java and set onOptionsItemSelected to it. (I don't know how many items are needed; the user adds them)
I have an arraylist of custom objects that have a title that I want to display in the menu and when clicked on the item I want to change what is being displayed in the main Activity, by passing which one of the objects was clicked on.
I know that it is possible to add a menu item like this, but the problem is that I don't know how many there are going to be, so setting the onOptionsItemSelected to the right amount of items is what I am looking for
private static final int MENU_ITEM_ITEM1 = 1;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, MENU_ITEM_ITEM1, Menu.NONE, "Item name");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ITEM_ITEM1:
return true;
default:
return false;
}
}
Thanks for your help!
EDIT:
I see how how this might seem like a duplicate question, but the other questions and answers did not help. I am looking for a way to set onOptionsItemSelected to an unknown amount of items (the size of an ArrayList) and then display the selected Object in the main Activity
Thanks

Start an Activity from Item (ActionBar)?

I'm using ActionBarCompat to make an actionbar for devices under API 11.
It works great and was easy to setup, but I'm stuck.
I have some items on the Actionbar, and it looks great.
Some Items are behind the three dots (ifRoom) and some you always can see.
How do I make so when you click on one of these items so it starts an new Activity?
I tried with switch/case and other methods, but didnt work to send from one Activity to another through the Items. I know how to send from button, imagebutton to another Activity, but not from Items.
My main.xml looks like this:
<item
android:id="#+id/add"
android:title="Lägg till"
android:icon="#drawable/new"
android:orderInCategory="1"
budsnabben:showAsAction="always"/>
And the code in MainActivity looks like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.:
Intent intent = new Intent(this, MapActivity.class);
this.startActivity(intent);
break;
case R.id.menu_item2:
// another startActivity, this is for item with id "menu_item2"
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
}
The problem is on case R.id.... After Id, I dont get my class map or main, its not there.
Thank you.
SOLUTION:
Just want to thank you Gerard.
I created new Strings in strings.xml.
After that, I did change the title in my main.xml to this:
android:title="#string/add"
I did hard-coded that line like before, therefore it didn't work I think:
android:title="#+id/add"
Thank you once more.
Use public boolean onOptionsItemSelected(MenuItem item) { ... } in your activity using the actionbar and create a switch-case matching item.getItemId() with the IDs from the menu layout. After that creating the appropriate intent like you would on a regular button.

"Back" button on Action bar - Android . How to go "back"?

Action Bar
I'm talking about the (Number 1, in the pic), button with a little arrow and the app icon and the top left side of the screen. It is automatically defined when we select the "Black activity" template.
My app has a pretty huge hierarchy graph, got about 25 activities now.
I'm basically just showing a few tutorials and one can navigate to it according to the categories.
Now that "Back" (?) button thingy on action bar is on every screen I have, and I do want to keep it. The code shows no error, but when I actually press that button, the app stops working.
What I want, is to just replicate the actual back button function with that (Number 1) button, that I showed in the image.
When I press it, the top most screen should close, and the last one should open. Just close the screen.
What I tried :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
this is the the function that inflates that buggy button along with the action bar. I tried to replace the entire code, and call "Finish" function, but that failed miserably.
I was unable to find a function specifically made for that top left most button...
I want the top most screen on the stack(the one in the foreground) to close, when this button is touched.
How to do this ?
I think the easiest way out is follows:
I am assuming that from activity A you are starting the activity B. Now from activity B you want to go back to activity A on pressing the top left back button on action bar. simply call this.finish() or ActivityName.this.finish() from there:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
This should finish your current activity. However if you have lot of activities, then you might have to do this in all the activities. To save yourself from that effort, you can make a class lets call it AbstractActivity; that extends Activity. Then you can extend all your other activity classes to extend that class(AbstractActivity). Inside AbstractActivity you can put the above code. So now that piece of code would be valid for all your activities and that feature would be implemented for all of them. Basically this sort of thing (Inheritance)can be used any time, when there are some common features which would be applicable to your many classes.
If you are receiving any errors, please do post your LogCat if you require further help.
Hope this helps you.
just giving basic code given by #shobhit puri...
for invoking the action bar back button..add the following code in the onCreate() method along with the onOptionsItemSelected....
protected void onCreate(Bundle savedInstanceState)
{
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}

Categories