I'm going crazy! I have this source but why it goes an error?
This is the Menu.xml for MainActivity
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:id="#+id/group1">
<item android:id="#+id/item1" android:title="X"></item>
<item android:id="#+id/item2" android:title="Y"></item>
<item android:id="#+id/item3" android:title="Z"></item>
<item android:id="#+id/item4" android:title="Share"></item>
</group>
</menu>
And this is the Java File
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
CODE
}
return true;
case R.id.item2:
CODE
return true;
case R.id.item3:
CODE
return true;
case R.id.item4:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "hXXX");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Check out this site!");
startActivity(Intent.createChooser(intent, "SHARE"));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Why give me an error in this line: case R.id.item4: The error is Multiple markers at this line
- item4 cannot be resolved or is
not a field
- item4 cannot be resolved or is
not a field
I'm guessing your Menu.xml file is saved in 'res/menu/Menu.xml'. If so, try:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.Menu, menu);
return true;
}
Otherwise it may be in a different folder, so if your Menu.xml file is in your layout folder (ie 'res/layout/Menu.xml', try:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.Menu, menu);
return true;
}
Just clean your project. The error shall
be gone.
Related
I am trying to add a simple button to my action bar, but when I try to use the following code, the items are added as items under the overflow menu, not on the actionbar itself. Any help would be appreciated,
Jacob
Here is my Java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_review, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
and my XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/seda"
android:icon="#drawable/redpinreal"
android:title="Hello Worldh"
android:showAsAction="withText|always"/>
</menu>
I also have this enabled:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Part of your code (android:showAsAction, instead of yourapp:showAsAction) is attempting to use the native action bar. Part of your code (getSupportActionBar(), instead of getActionBar()) is attempting to use the appcompat-v7 action bar backport. Choose one and stick with it.
I am trying to create a New button (icon only) next to the 3 dots button in the ActionBar. This is the code I tried, but it adds it as child of the 3 dots button.
main_activity_actions.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/create_game"
android:menuCategory="container"
android:orderInCategory="1"
android:title="#string/new_game"
android:titleCondensed="nieuw spel">
</item>
</menu>
MainActivity.java:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
Also, how can I find out what default icons I can use?
To show icons in the action bar, you can add the line android:showAsAction="always" to your XML item. Still Android would show it only if it judges there is adequate space in the action bar. If you strictly always want to show your button, you can define and set a custom layout for your action bar.
You can download the default icon set for action bar from https://developer.android.com/design/downloads/index.html
try it
/res/menu/activity_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:showAsAction="never"
android:title="#string/menu_settings"/>
<item
android:id="#+id/menu_save"
android:showAsAction="ifRoom"
android:icon="#android:drawable/ic_menu_save"
android:title="#string/menu_guardar"/>
<item
android:id="#+id/menu_new"
android:showAsAction="ifRoom|withText"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/menu_nuevo"/>
</menu>
MainActivit.class
public class MainActivity extends Activity {
//...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
//onClic item
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_new:
Log.i("ActionBar", "Nuevo!");
return true;
case R.id.menu_save:
Log.i("ActionBar", "Guardar!");;
return true;
case R.id.menu_settings:
Log.i("ActionBar", "Settings!");;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And you can download All IconPack here:
https://developer.android.com/design/downloads/index.html
Yes, you can inflate a custom actionbar for adding buttons with images like this:
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) getActionBar()
.getThemedContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View customActionBarView = inflater.inflate(R.layout.actionbar_custom, null);
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setCustomView(customActionBarView,
new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
setContentView(R.layout.activity_main);
}
Where _actionbar_custom.xml_ would be your layout resource, usually a LinearLayout with whatever components you want.
So I have a Search Button in my Action Bar, But I only want that button in the first Activity, but when I open the seconde activity, the Action button is still there. How to get rid of that button in the seconde activity without removing the action bar, because I nee it from my Navigation Drawer.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
...
</menu>
Activity1:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
Activity2:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//I thought removing the INFLATER part would help, but it didn't...
return super.onCreateOptionsMenu(menu);
}
You can try hiding the search menu item in your second activity.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.action_search).setVisible(false);
return true;
}
You can refer to this question with multiple answers.
You can make the Item in your 2nd Activity invisible like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.action_search);
item.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
Another approach would be to load a second menu.xml with no item in it.
if you return false it will not be shown. as android docment saying http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu%28android.view.Menu%29
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
I'm trying to add action buttons to my simple app. I found and used this tutorial https://developer.android.com/training/basics/actionbar/adding-buttons.html
Here is the relevant part of my source (MainActivity.java):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_new_route:
(...)
return true;
case R.id.action_current_orders:
(...)
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Here is menu/main.xml
<item
android:id="#+id/action_new_route"
android:icon="#drawable/ic_route"
android:showAsAction="always"
android:title="New route"/>
<item
android:id="#+id/action_current_orders"
android:icon="#drawable/ic_orders"
android:showAsAction="always"
android:title="Current orders"/>
The problem is with these two lines:
case R.id.action_new_route:
(...)
case R.id.action_current_orders:
These are error messages I get:
action_current_orders cannot be resolved or is not a field
action_new_route cannot be resolved or is not a field
Can you help me solve this problem please?
Thank you.
I am using ActionBarSherlock Tabs with Fragments in my applications:
public class ExampleActivity extends SherlockFragmentActivity{}
I have added an Options Menu in the Action Bar. The problem is while this does show the options menu on the click of virtual button in the action bar, it doesn't do so when the physical button is pressed. I would like the options menu to be displayed when the physical menu button is pressed.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//return super.onCreateOptionsMenu(menu);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_prefs:
Intent i = new Intent(this,ShowSettingsActivity.class);
startActivityForResult(i, requestCode);
return true;
case R.id.menu_faq:
startActivity(new Intent(this, AboutApp.class));
return true;
case R.id.menu_contact:
startActivity(new Intent(this, FeedbackApp.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And in res/menu/options_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:icon="#drawable/ic_settings_dark"
android:orderInCategory="100"
android:showAsAction="always">
<menu
android:id="#+id/e">
<item
android:id="#+id/menu_prefs"
android:title="#string/menu_prefs"/>
<item
android:id="#+id/menu_faq"
android:title="#string/menu_faq"/>
<item
android:id="#+id/menu_contact"
android:title="#string/menu_contact"/>
</menu>
</item>
</menu>
If I remove the sub-menu, it displays the options menu on click of physical button.
(Sorry about what my first answer that was off the point)
I don't think that the <menu> XML element accepts android:id as an attribute. Did you tried removing it ?
Another try :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true; // override the useless returned value by super()
}
Try this code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
Hope this will help you.