I would like to rename menu on click of an item. I tried the following but it didn't change.
I tried item.setTitle("LandScape"); or item.setTitle("Portrait");
Declare the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
Do something on click on the menu item:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
boolean result = true;
switch(item.getItemId())
{
case R.id.oscillation_mode:
{
if(getScreenOrientation() == 1)
{
Log.e("Orientation","ABC"+getScreenOrientation());
item.setTitle("LandScape");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else
{
item.setTitle("Portrait");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
break;
}
The text is the same. The default it is Portrait.
when you call setRequestedOrientation() your Activity is destroyed and recreated, so any changes you make to in memory objects will be dropped. You should make these changes when the menu is shown instead
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem oscillation = menu.findItem(R.id.oscillation_mode);
if (getScreenOrientation() == 1){
item.setTitle("LandScape");
}else{
item.setTitle("Portrait");
}
return true;
}
Related
I am working on Android application in which I want to refresh my menu at run time. For example if in the starting my menu is like menu1.xml then after getting any result for example 'Yes' from the server I want to refresh it with menu2.xml.
I want to use invalidate function for this but I am not able to make my menu items to be refreshed. My code is given below:
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
if (showAcceptButton==true) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu1, menu);
return true;
}if (Constants.showAcceptedButton = true) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu2, menu);
return true;
}
if(showNormalMenu==true){
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu3, menu);
return true;
}
return true;
}
At onCreate for the first time it loads the menu but on run time I want to load another menu items.
Once try as follows
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onPrepareOptionsMenu(menu);
}
It will give existing menu object and you can add or delete menu items from it.
UPDATE:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.clear();
if (showAcceptButton==true) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu1, menu);
return true;
}if (Constants.showAcceptedButton = true) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu2, menu);
return true;
}
if(showNormalMenu==true){
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu3, menu);
return true;
}
return super.onPrepareOptionsMenu(menu);
}
Hope this will helps you.
In onCreateOptionsMenu I'm setting an action view with:
MenuItem item = ..
item.setActionView(action_view)
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
and it works ok. But I want to move this to my xml menu and I can't seem to make it show. It only shows up the title. I tried many versions, like:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourApp="http://schemas.android.com/apk/lib/myPackage.myClass" >
<item
android:id="#+id/item"
yourApp:actionViewClass="action_view"
android:showAsAction="always" or youtApp:showAsAction="always"
android:title="title"
</item>
The problem must be it getting to the action_view variable, but I can't seem to figure it out
Did you inflate the menubar?
#Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_name, menu);
mMenu = menu;
if(mMenu != null){
MenuItem item = mMenu.findItem(R.id.item_id);
if(item != null){
item.setVisible(false);
}
}
super.onCreateOptionsMenu(menu, inflater);
}
Also do you have setHasOptionsMenu(true); #onCreate() function?
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item:
// Write your action_view here
break;
case R.id.your_other_item_id:
//Your other Menu item logic
break;
}
return super.onOptionsItemSelected(item);
}
I am creating a menu where one of the items is used the lock an object. When this item is clicked, the menu should be recreated with a button to unlock the item. I created two menus for this. This is working fine. I read that in Android version >= 11 the onPrepareOptionsMenu is no longer called when displaying the menu and I have to call invalidateOptionsMenu(). So I changed the build target (both in the Manifest and in properties) to 11 and ran the app on an AVD of 4.0.3. The program is still working fine, but I thought it shouldn't anymore, and I should check
if (Build.VERSION.SDK_INT >= 11)
{
invalidateOptionsMenu();
}
This is my code:
public class MainActivity3 extends Activity{
boolean locked;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locked = false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.changing_menu1, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
MenuInflater inflater = getMenuInflater();
if (locked) {
inflater.inflate(R.menu.changing_menu2, menu);
}
else {
inflater.inflate(R.menu.changing_menu1, menu);
}
return super.onPrepareOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Menu1:
break;
case R.id.Menu2 :
break;
case R.id.Menu3 :
locked = !locked;
break;
}
return true;
}
}
So the Menu is still refreshed/recreated in 4.0.
Did I misunderstand something about the usage of invalidateOptionsMenu();?
invalidateOptionsMenu() was added to give us the ability to force onCreateOptionsMenu() to be called again. onPrepareOptionsMenu() is still called every time you call the menu.
What you are trying to achieve above is a good example of when to use invalidateOptionsMenu() but because of backwards compatibility you will need to do both:
if (Build.VERSION.SDK_INT >= 11) {
invalidateOptionsMenu();
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
if (Build.VERSION.SDK_INT >= 11) {
selectMenu(menu);
}
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (Build.VERSION.SDK_INT < 11) {
selectMenu(menu);
}
return true;
}
private void selectMenu(Menu menu) {
menu.clear();
MenuInflater inflater = getMenuInflater();
if (locked) {
inflater.inflate(R.menu.changing_menu2, menu);
}
else {
inflater.inflate(R.menu.changing_menu1, menu);
}
}
In my app i have an optionsmenu. It has 2 buttons. Depending on a boolean value i would like to show/hide one of the buttons. I've got the following code but it doesn't hide the button. How can i do this?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menushowmoredetails, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(displayRotaDetails.equalsIgnoreCase("false")){
if(item.getItemId() == R.id.moredetails)
item.setVisible(false);
}
switch (item.getItemId()) {
case R.id.back:
onBackPressed();
return true;
case R.id.moredetails:
You have to use the onPrepareOptionMenu method like this:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
// add your conditions here and change 0 with the R.id.moredetails item postion.
if(displayRotaDetails.equalsIgnoreCase("false")){
menu.getItem(1).setVisible(false);
}
}
package com.guess.phone;
import android.app.Activity;
public class Menu extends Activity {
// Setting Up the MenuInflater
public boolean onCreateOptionsMenu(Menu Menu) {
super.onCreateOptionsMenu((android.view.Menu) Menu);
MenuInflater awesome = getMenuInflater();
awesome.inflate(R.menu.main_menu,(android.view.Menu) Menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menuSweet:
startActivity(new Intent("com.guess.phone.SWEET"));
return true;
case R.id.menuToast:
return true;
}
return false;
}
}
Use below OnCreateOptionsMenu instead
public boolean OnCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}