I have created with Android Studio 1.4 a Blank Activity where I can set menu's items to diplay.
My problem is that when I create a new activity "MainActivity2" I see the Action Bar with its colours but I'm not able to "connect" a menu with its elements. By default "onOptionsItemSelected" and "onCreateOptionsMenu" are not created automatically in this new activity.
Thanks
First activity,
MainActivity2
I found the solution. I saw some tutorials in Internet saying that to change activity I have to use "setContentView(R.layout.activity_main2)". By the way using this the application will override only the design and will never be executed the "onCreateOptionsMenu" where menu items are going to be added. The correct action to execute on the button listener is " startActivity(new Intent(MainActivity.this, Main2Activity.class)) ", so that the code can be executed.
Related
I've used a library downloaded from this link to create a Circular Menu using Circular Layout in an application.
https://android-arsenal.com/details/1/1454
The menu contains 6 items. I create the menu in onCreate and I want to create that menu again in onResume as I want to update some of the menu items.
I want to create a Circular Menu with 6 items. As I have called the menu create function in both, number of menu items have got doubled, which is 12.
Below is a code excerpt which uses Circular Menu.
String[] mItemTexts = new String[] { "Item1", "Item2", "Item3", "Item4", "Item5", "Item6" };
int[] mItemImgs = new int[] { R.drawable.im1,
R.drawable.im2, R.drawable.im3, R.drawable.im4, R.drawable.im5, R.drawable.im1};
mCircleMenuLayout = (CircleMenuLayout) findViewById(R.id.id_menulayout);
mCircleMenuLayout.setMenuItemIconsAndTexts(mItemImgs, mItemTexts);
What can I do to avoid getting the Circular Menu doubled?
Any suggestions or workarounds are much appreciated.
Thank you.
PS:
EDIT
I am using creation of the Circular Menu in onResume in the Activity. I don't finish the Activity with Circular Menu when starting another Activity, thus when I return from the newly started Activity again I find the Circular Menu has got doubled. That's the issue.
Simply call your method only in OnResume and remove form onCreate
as onResume is called after the onCreate
see this link
I want to know if there's any way in Android to programatically know if
public boolean onCreateOptionsMenu(Menu menu)
has been called and returned.
I'm using actionsherlockbar library in a project and the app has to download some content and I'd like to start the download when the action bar is loaded so I can use its integrated progress interface, but I can't find a way since onCreateOptionsMenu is called after onCreate.
To sum up, hat I want to do is:
-load the actionBar
-notice it is loaded
-enable the progress bar
-download
-disable the progress bar
But I don't know how to notice it's loaded.
Thanks in advance for your help.
use Log.d() method to register in the logcat. eg:
in your case:
where you have defined the onCreateOptionsMenu() add this command:
Log.d("tag of your choice", "value you want to check if it has been initialized");
You can see more about this function in eclipse or https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CCwQFjAA&url=http%3A%2F%2Fdeveloper.android.com%2Freference%2Fandroid%2Futil%2FLog.html&ei=3RIfUqaCGcS4rgf19ICQCg&usg=AFQjCNEd4hTMzjI2BnTS2woBfjvrddoQsw&sig2=4JDVD0JjIjx0mEhog9plPQ&bvm=bv.51495398,d.bmk
How can we handle the check button in the CAB menu?
Is the item associated to some internal id like android.R.id.checkbox?
I've read that it could be handled in SherlockActionBar CAB, but could it be the same with the native ActionBar CAB?
Is there any way to detect the interaction of this item? onActionModeFinished() is not sufficient since I'm calling it multiple times since the CAB needs to be present due to previous changes that happened.
Thanks.
Ok, finally found a solution after trying some things.
Place the code you want for the checkbox or onbackpressed in the method below
public void onDestroyActionMode(ActionMode mode)
{
//place the code you want for the checkbox or back icon here. If you don't want
// this code run if other selections are used, then just create a boolean value that
//you earlier on and check the value in this section before implementing the code
}
};
I've got an optionsmenu looking like this right now:
Lets say that if I click item 1, i want two new items added to the menu looking like this:
I'm having problems doing this at runtime(while it's open) since onCreateOptionsMenu is only called once and onPrepareOptionsMenu seems only to be called when the menubutton of the phone is clicked. I just want it to refresh with these new items added.
Thanks for any help!
When you select an option item it causes the system to close the options menu. This happens after onOptionsItemSelected() runs. I'm guessing what you need to have happen is for that entire process to complete then have the options menu programmatically opened again so onPrepareOptionsMenu() is called.
Try this:
In onOptionsItemSelected(), when the user selects your "add two more options" option, set a flag in your activity but don't do anything else.
Override onOptionsMenuClosed(). Check if the flag is set. If it is, then post to be executed by the UI thread on its next pass. It should do nothing but open the options menu again, e.g.
runOnUiThread(new Runnable() {
#Override
public void run() {
openOptionsMenu();
}
});
Override onPrepareOptionsMenu(). Check if the flag is set. If it is, then add your two additional menu items and un-set the flag. You'll need to do some additional work to prevent the "add more items" menu item from continuing to add new items every time its pressed, unless that's the behavior you're looking for.
I'm new to android and I'm trying to figure out if there is a best practice in menu handling.
Well, here is the thing:
I have created a menu.xml file (within res/menu), Main.java handle the menu action with a switch case.
I'm wondering what is the best way to run appropriate task when an action is performed on a menu item:
Use an intent and trigger the corresponding activity
define everything (which could be a lot of code) within the case corresponding to this menu item.
...
startActivity(new Intent(getApplicationContext(),MyOtherActivity.class));
return;
it doesn't have to be more complicated than that.