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.
Related
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);
}
When controlling a MenuItem, I have been doing this:
Menu menu;
(...)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
this.menu = menu;
return true;
}
(...)
public void handleSearch(View view) {
Button button = (Button) view;
if(menu.findItem(R.id.action_search).isVisible()) {
button.setText(R.string.button_search_show);
menu.findItem(R.id.action_search).setVisible(false);
} else {
button.setText(R.string.button_search_hide);
menu.findItem(R.id.action_search).setVisible(true);
}
}
"this" being referencing the menu created in onCreateOptionsMenu with a Menu that any method in the class can use. The handleSearch method controls MenuItems by using findItem twice. This doesn't feel very conventional or efficient (a very scientific observation, I might add). Is there a more conventional or efficient way to do this?
You can save MenuItem in a variable instead of using findItem twice.
MenuItem myMenuitem = menu.findItem(R.id.action_search);
There is a method that handles actionBar's menu itself.
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return (true);
case R.id.menu_home:
Intent i = new Intent(this, HomeActivity.class);
startActivity(i);
return (true);
}
return (super.onOptionsItemSelected(item));
}
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;
}
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);
}
I am creating am android application which has 4 different activities all having a common menu. To show menu in all activities generally I need to add this code in every files.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
Is there anyway that I can add this code in one single file & include to all activities?
Thanks in advance.
Create one Main Activity write your menu code in that activity and then extend other activities with Main Activity..
public MainActivity extends Activity
{
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return false;
}
}
And some TempActivity
public TempActivity extend MainActivity
{
/......
}
abstract class MenuHavingActivity extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
}
and then have your other activities extend MenuHavingActivity
edit: yeah, what user370305 said