My problem is, that I have got a TabActivity, which has 4 Tabs right now. The first Tab is a special Details-Tab, where the user could modify some data.
The problem is, that if I add a OptionsMenu for the Activity, that the OptionsMenu is appearing on every Tab.
I tried to check the current mTabHost.getCurrentTabTag() in the onCreateOptionsMenu but that changed nothing.
So, how to do that?
(The following code, which still shows the OptionsMenu on every Tab)
public boolean onCreateOptionsMenu(Menu menu) {
if(mTabHost.getCurrentTabTag()==getString(R.string.tab_details)) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, EDIT_ID, 0, R.string.menu_edit).setIcon(R.drawable.edit);
return result;
}
return true;
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case EDIT_ID: {
Toast.makeText(this, "o.O", Toast.LENGTH_LONG).show();
}
}
return super.onMenuItemSelected(featureId, item);
}
I see that you are comparing strings (tags in fact) with ==, usually, this is nicer to do that with .equals() method. Maybe it will solve your issue.
To dynamically update an option menu (from the documentation) :
If you want to change the Options
Menu each time it opens, you must
override the onPrepareOptionsMenu()
Related
i have three menu items "enable, diable, exit". what i want to do is, when the R.id.menu_enable_bt is chosen, i want to disable it using
menu.findItem(R.id.menu_enable_bt).setVisible(false);
but i can not call
menu.findItem(R.id.menu_enable_bt).setVisible(false);
from inside the onOptionsItemSelected(..) method.
how can i change the visibility of a menu item inside onOptionsItemSelected(..)
CODE:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
Log.w(TAG, SubTag.msg("onCreateOptionsMenu"));
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
if (this.mBTAdapter.isEnabled()) {
menu.findItem(R.id.menu_enable_bt).setVisible(false);
menu.findItem(R.id.menu_disable_bt).setVisible(true);
} else {
menu.findItem(R.id.menu_enable_bt).setVisible(true);
menu.findItem(R.id.menu_disable_bt).setVisible(false);
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
Log.w(TAG, SubTag.msg("onOptionsItemSelected"));
switch (item.getItemId()) {
case R.id.menu_enable_bt:
Log.d(TAG, SubTag.bullet("onOptionsItemSelected", "menu_enable_bt"));
//menu.findItem(R.id.menu_enable_bt).setVisible(false); **how to do this**
this.mATEnableBT = new ATEnableBT();
this.mATEnableBT.execute();
break;
case R.id.menu_disable_bt:
Log.d(TAG, SubTag.bullet("onOptionsItemSelected", "menu_disable_bt"));
break;
case R.id.menu_exit:
Log.d(TAG,SubTag.bullet("onOptionsItemSelected", "menu_exit"));
finish();
break;
}
return super.onOptionsItemSelected(item);
}
you can use invalidateOptionsMenu() which will forse onCreateOptionsMenu to be called again.
From the documentation
Declare that the options menu has changed, so should be recreated. The
onCreateOptionsMenu(Menu) method will be called the next time it needs
to be displayed.
There you can check your conditions and undertake the needed actions
See the javadoc for onPrepareOptionsMenu. This is your opportunity to make changes to a menu just before it becomes visible to the user. So you should maintain some member variables describing what should be visible and use that to modify the menu items here.
I have the below code and I can't understand why my code doesn't show up menu. If the activity containing data, condition_true = true otherwise condition_true = false. I can clearly see the control going to menutag and the condition is true too. Even I am getting Inflated string too in log. But still menu doesn't show up. I have seen lot of posts on these like link 1, link2 etc., but that didn't solve my problem. Can someone please help me on this issue?
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear(); //Clear view of previous menu
MenuInflater inflater = getSupportMenuInflater();
Log.d("menutag", condition_true);
if(condition_true)
{
inflater.inflate(R.layout.menu, menu);
Log.d("menutag","Inflated");
}
return super.onPrepareOptionsMenu(menu); // replaced this with true, but no use.
}
You have to add the options attributes in xml.. Otherwise you can do it programmatically like this:
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.yourmenuxmlfilename, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId){
case R.id.item1:
// what you want to do with first button
break;
case .....
break;
}
return true;
}
Maybe you're overriding onKeyDown and it always returns true? This can cause the problem, because it will catch the menu button pressed event too.
I'm not trying to change the main Icon , just a menu item's icon.
The Icon is essentially displays whether I am recording at that moment. I change the icon when it's tapped using
item.setIcon(R.drawable.recordstart);
In this method.
public boolean onOptionsItemSelected(MenuItem item) {
...
} else if (item.getItemId() == R.id.ab_menu_VRecord) {
if(recording)
{
item.setIcon(R.drawable.recordstop);
}else{
item.setIcon(R.drawable.recordstart);
}
}
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
Anyone know how I can do this outside this method.
Example:
class {
public MenuItem example;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbar, menu);
example = menu.findItem(R.id.ab_menu_exampleview);
return true;
}
}
Then throughout your class you can use
example.setIcon("Your Image");
Not 100% sure where you are wanting to change the icon, but you can certainly cache or create a class member variable and point it at the the MenuItem in your Activity or Fragment for example. After they click it or you inflate it, assign it to the member variable and when you need to change it, you've got a reference or "cached" pointer to it to change the icon.
I think that is a UI change so you might have to be sure that you only call that on the UI thread.
if(!item.isChecked()){
item.setChecked(true);
item.setIcon(R.drawable.icon1);
}else{
item.setChecked(false);
item.setIcon(R.drawable.icon2);
}
is this?
I am trying to start an activity from an options menu, but my app keeps crashing. The only error that I receive is an ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord,Intent) error in the debug window in Eclipse.
Below is the the code that I am using at the moment, but keep in mind I have tried multiple options, all of which end in the same misery, at the same piece of code - the startActivity statement (discovered by using breakpoints, since I'm not sure how to see the stack trace in the LogCat window, as described in my previous question Android/Eclipse: assistance with LogCat).
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.changescheme, menu);
menu.findItem(R.id.changeScheme).setIntent(new Intent(this, ColourActivity.class));
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
this.closeOptionsMenu();
startActivity(item.getIntent());
return true;
}
And here is the changescheme.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/changeScheme" android:title="Change Colour Scheme" android:icon="#android:drawable/ic_menu_edit"></item>
</menu>
I have also tried using a switch(item.getItemId()) statement in the onOptionsItemSelected block as opposed to the menu.findItem in the onCreateOptionsMenu block, but still no luck.
I have defined the activity in my Manifest file. I can also start the activity from a regular button, and the first time the app opens on a device, the activity is started immediately after my splash screen, and I have had no problems with either of these methods.
To me this indicates that there is nothing wrong with the ColourActivity class or its associated layout file, but there is a problem with the implementation from the options menu.
I have also implemented this same method as shown above (in code) in a different app and had no problems, so I'm am really at a loss here.
Intent you are activating should point to some target component, which is not in your case, instead you should do following:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
this.closeOptionsMenu();
Intent intent = new Intent(ActivityA.this, ColourActivity.class);
/*Here ActivityA is current Activity and ColourActivity is the target Activity.*/
startActivity(intent);
return true;
}
Try with this,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflator = getMenuInflater();
inflator.inflate(R.menu.changescheme, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.changeScheme:
Log.d("ChangeScheme", "Selected : ChangeScheme Option");
startActivity(new Intent(MainAcitivity.this, ColourActivity.class));
return true;
caseR.id.help:
Log.d("HelpMenu", "Selected : Help Option");
//Here put your code
return true;
}
}
Check this:
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.changescheme, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.changeScheme:
//start activity here
break;
case R.id.help:
//start activity here
break;
}
return true;
}
Hi adam your code seem to be perfectly fine while i am testing on my emulator, please check whether you have added the class name "ColourActivity" to your manifest file.
<activity android:name="ColourActivity"></activity>
I have solved the problem now.
It turns out the problem was not at all on the ListActivity class, it was in fact on the ColourActivity class.
I was attempting to parse a few colours in onCreate, but I had forgotten to include the # in one of the RGB colour strings, hence the crash!
Thanks heaps for everyone's help, Adam.
I am trying to use the onPrepareOptionsMenu and when I press the menu, it does nothing!! Where am I going wrong?
Also, am I using the finish (); command right? When the user presses exit(menuX) I want them to exit the application completely, not the activity.
Thanks! :)
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater getItnow = getMenuInflater();
getItnow.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onPrepareOptionsMenu(MenuItem item){
switch (item.getItemId()){
case R.id.menuA:
startActivity(new Intent("com.abc.example.A"));
return true;
case R.id.menuB:
startActivity(new Intent("com.abc.example.B"));
return true;
case R.id.menuC:
startActivity(new Intent("com.abc.example.C"));
return true;
case R.id.menuD:
startActivity(new Intent("com.abc.example.D"));
case R.id.menuX:
finish();
}
return false;
}
}
First off all I would rather use onOptionsItemSelected then onPrepareOptionsMenu. Because as I understand you just want to press it. It is quite the same only first line is changed.
The second thing - app exit. Well, if this menu is in your main and first activity, then when you call finish() then this activity is destroyed (not exactly but the thing is that you don't see it anymore), so if app has no more activity everything is destroyed.
More advanced method for that is using instruction android.os.Process.killProcess(android.os.Process.myPid()); and what it is doing is simply sending kill signal for whole process. As far as your app won`t get bigger, I mean you will have only your activities in this process, than it is fine. Hope it will help.
When I create an options menu, I usually use these two methods:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, 1, 0, "option text");
.....
}
and
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId() == 1)
...
}
Also, with your switch statement, you are forgetting to put in break; for each option (which I notice mostly because I am notorious for doing the same).