Android error: cannot find symbol variable menu - java

Im creating menu:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.points, menu);
return true;
}
I have xml file in res/menu/points.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Item" />
</menu>
I get error - Error:(27, 36) error: cannot find symbol variable menu
why ?
Clean Rebuild helps - Thanks

I had this same problem and my issue was with the parameter Menu menu. When i first invoked Menu i didn't hit enter to set it to android.view menu, which would explain why yours is saying it doesn't understand the variable. If your not casting it as a menu type for what you're importing it as then it wont understand it as a standalone variable. I hope this helps, we could possible be working on the same tutorial. Good luck either way!!

Create a menu_main.xml file in menu folder in res folder.res>menu>menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.myapplication.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="action_settings"
app:showAsAction="never" />

Related

Android - How to add a default checked CheckBox to Navigation Drawer

I want to add a check box to Navigation Drawer, and I want one of them to be default checked
I'm using this: app:actionViewClass="android.widget.Switch" as suggested in this answer.
But I couldn't figure out how to make one of them default checked. If I use this property android:checked="true", then the option is checked instead of checkbox (See image).
Does anyone know how to make it default checked (if possible, I want to do it in XML only)?
Here is my activity_drawer.xml.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:showIn="navigation_view">
<group
android:checkableBehavior="single">
<item
android:id="#+id/checkboxX-axis"
android:title="Show x-axis"
android:icon="#drawable/ic_x_axis_black_24dp"
app:actionViewClass="android.widget.CheckBox"
android:checked="true"
/>
<item
android:id="#+id/checkboxY-axis"
android:title="Show y-axis"
android:icon="#drawable/ic_y_axis_black_24dp"
app:actionViewClass="android.widget.CheckBox"
/>
<item
android:id="#+id/checkboxZ-axis"
android:title="Show z-axis"
android:icon="#drawable/ic_z_axis_black_24dp"
app:actionViewClass="android.widget.CheckBox"
/>
</group>
</menu>
You can do this:
MenuItem item = navigation.getMenu().findItem(R.id.checkboxX-axis);
CompoundButton compoundButton = (CompoundButton) item.getActionView();
compoundButton.setChecked(true);
replace navigation with the NavigationView's name.

How would I programatically add items to my menu in in activity_main_drawer.xml

I have this activity_main_drawer.xml file containing my drawer to my main activity:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/cityAdd"
android:icon="#drawable/ic_menu_camera"
android:title="Add City" />
<item
android:id="#+id/cityList"
android:title="Cities">
<menu>
<item android:title="I want to add these items"></item>
</menu>
</item>
</group>
</menu>
I am trying to reference my menu through Java so I could programmatically add items to my drawer while my app is running. This is the method(I think that's what it's called I'm still a noob) :
private void addMenuItemNavMenuDrawer(String added) {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
Menu menu = navigationView.getMenu();
menu.add(added);
}
The problem is the items are not being added to the menu making this code useless.
If any other parts of my code are needed I will be happy to share. Any help is appreciated!
I found that it was due to me not understanding static and non-static variables that I created earlier in my code.

Action Button goes to Overflow of the Action Bar

My menu is defined as:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.opendoors.registration.BusinessProfActivity" >
<item
android:id="#+id/action_done"
android:title="#string/action_done"
android:icon="#drawable/ic_action_accept"
app:showAsAction="ifRoom"/>
</menu>
Despite using ifRoom in showAsAction, my button still goes to the overflow menu.
I have also tried replacing:
xmlns:app="http://schemas.android.com/apk/res-auto"
with
xmlns:compat="http://schemas.android.com/apk/res-auto"
as well as
app:showAsAction="ifRoom"
with
compat:showAsAction="ifRoom"
However, that doesn't work either, even though it was suggested in similar questions. What am I missing?
Maybe the following error might have something to do with this?
06-10 04:50:34.541: E/AndroidRuntime(1697): java.lang.RuntimeException: Unable to
instantiate application android.app.Application: java.lang.IllegalStateException: Unable
to get package info for com.mypackage.app; is package not installed?
Try this:android:showAsAction="ifRoom"
Have you tried to use always instead of ifRoom?
<item
android:id="#+id/action_done"
android:title="#string/action_done"
android:icon="#drawable/ic_action_accept"
app:showAsAction="always"/>
You could try: app:showAsAction="always"

Despite using android-support-v7-appcompat library, Action bar is working wrong with android 2.2

I have used actionbarcompat in programming. Also I've added android-support-v7-appcompat library. Unfortunately action bar display as bellow (Items doesn't appear) :
please say right way.
this is my menu.xml code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ActionBar2="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:orderInCategory="100"
ActionBar2:showAsAction="always"
android:title="Search"/>
<item
android:id="#+id/action_copy"
android:orderInCategory="100"
ActionBar2:showAsAction="always"
android:title="Copy"/>
<item
android:id="#+id/action_share"
android:orderInCategory="100"
ActionBar2:showAsAction="always"
android:title="Share"/>
<item
android:id="#+id/action_share"
android:orderInCategory="100"
ActionBar2:showAsAction="always"
android:title="Hossein"/>
</menu>
Please take a look at the answers in this question (Android Studio - just can't get action bar / overflow to work?). If a hardware button is available no overflow menu will be shown.
This works for me with appcompat v21:
On My onCreateoptionsMenu();
final MenuItem switchButton = menu.findItem(R.id.action_import);
final MenuItem searchItem = menu.findItem(R.id.action_search);
MenuItemCompat.setShowAsAction(searchItem, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
MenuItemCompat.setShowAsAction(switchButton, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);

Can't display an icon in the actionbar

I'm trying to show a map icon downloaded from the official android developers source.
I did everything as it should, but the icon won't show.
Here is my xml file named main_activity_bar:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/mapIcon"
android:icon="#drawable/ic_action_map"
android:title="#string/mapIconTitle"
android:showAsAction="always"
/>
</menu>
Here is the main activity xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.gs.testApp.MainActivity"
tools:ignore="MergeRootFrame" />
and this is what I have in the java class:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater actionMenue = getMenuInflater();
actionMenue.inflate(R.menu.main_activity_bar, menu);
return super.onCreateOptionsMenu(menu);
}
Everything seems to be fine, but the icon wont show on the emulator. Here is a screenshot:
The minimum version is android 3.0
Why the icon is not showing? What Am I missing? I know that it is something really small, but I can't spot it.
Here is how I fixed it - in case that someone is facing the same issue.
I changed
android:showAsAction="always" to app:showAsAction="always" and I also placed the icon order android:orderInCategory="0" and the auto res xmlns:app="http://schemas.android.com/apk/res-auto" so now my xml looks like:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:orderInCategory="0"
android:id="#+id/mapIcon"
android:icon="#drawable/ic_action_map"
android:title="#string/mapIconTitle"
app:showAsAction="always"
/>
</menu>
I dont think an icon is shown in the overflow menu. Try changing your title to "t" or rotate the screen. See if the icon appears this way.

Categories