I am watching a udemy training series on Android. And for many of the videos, when he creates a new project there is a res/menu folder. In it, there are xml files like a menu_main.xml. However, when I launch a new project on Android Studio, there is no res/menu folder at all. Am I missing something? Why do I have to create it manually?
In the video, it has the following in menu_main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" android:showAsAction="never"/>
</menu>
Am I missing something?
The trainer in the video is using a different version of Android Studio than you are, or you did not set up your project in exactly the same manner (e.g., you chose a different activity template).
Why do I have to create it manually?
In the version of Android Studio that you are using, for the activity templates that you chose (e.g., "Blank Activity"), the templates do not employ an action bar, Toolbar, or anything else that involves a menu resource.
Related
I am trying to specify an menu icon for an Action Bar. I'm using Android Studio 1.2. My XML code follows:
<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=".MainActivity">
<item
android:id="#+id/menu_connect"
android:icon="#drawable/ic_settings_bluetooth_white_24dp.png"
android:title="#string/menu_connect"
app:showAsAction="always"
/>
<item
android:id="#+id/menu_settings"
android:title="#string/menu_settings"
app:showAsAction="never"
/>
</menu>
The compiler complains about anything that I put after "android:icon=". Depending on what I've changed last, I get: "No resource found that matches the given name" or "String types not allowed" (among other errors).
I've tried suggestions from several posts. I cleared the Intellij cache and rebuilt everything and nothing changed. One post suggested that perhaps android:icon wasn't supported in earlier API versions so I set minSdkVersion to 23 (my current SDK version) in my module build script. I re-sync'd and again nothing changed.
I had some trouble figuring out how to set up the drawable directories for multiple resolution bitmaps. (Why doesn't Android Studio just create them when creating a new project?) I now have them (drawable-hdpi etc.) set up as siblings to the drawable directory. I think that's right. At least they appear to be correct in the Android directory view. Yeah, I'm a newbie.
Is there anything else that I can try?
Replace:
android:icon="#drawable/ic_settings_bluetooth_white_24dp.png"
with:
android:icon="#drawable/ic_settings_bluetooth_white_24dp"
You do not use file extensions when referring to resources from anywhere inside of Android.
I wonder if I should use the built in version of Google's icons for menu items or if I should download the icon pack instead?
Here's the default XML for my menu. It's using the built-in icons but the visual results looks a bit odd.
<?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:context=".ParkItemActivity">
<item android:id="#+id/action_remove"
android:title="#string/action_remove"
app:showAsAction="ifRoom"
android:icon="#android:drawable/ic_menu_delete" />
<item android:id="#+id/action_save"
android:title="#string/action_save"
app:showAsAction="always"
android:icon="#android:drawable/ic_menu_save" />
</menu>
But if I use the icons from the Material-Design-IconPack, then it looks like this. But is there a recommended way? Remark: Can't find the trash-icon in the new icon-pack. And the delete/remove is a simple minus symbol.
android:icon="#mipmap/ic_remove_white_48dp"
android:icon="#mipmap/ic_save_white_48dp"
Yes you are right, default drawable icons present in the android sdk doesn't scaled up according to the material design. Android sdk by default is not supported with material icons, instead of that you could make use of appcompat-v7 support library, which has few materials icon by default like copy, paste, clear, search, back, menu icons respectively.
To use appcompat-v7 icons in your xml
#drawable/abc_ic_menu_paste_mtrl_am_alpha
Note: Only few icons are listed out in appcompat-v7. You don't have any other options without using Material-Design-IconPackonce if you want to use specific other icons.
I reccomend you to find icons you would like to use and if you use Eclipse, you can navigate: Right-click your project -> New -> Other -> Android Icon Set. Don't forget to check "Action Bar Tab Icons" and you will be able to generate icons correctly scaled for actionbar.
I am following step by step instructions shared on development platform of google http://developer.android.com/training/basics/actionbar/index.html
But some how due to some reason I am not seeing ic_action_search.png on Menu bar in my simulator, though it is imported and copied on drawable-hdpi.
Listing down all options tried till now (using eclipse IDE):
Import image ic_action_search.png
Don't import image but copy it directly on folder
Size of image, I have copied it directly from Android_Design_Icons.zip suggested at https://developer.android.com/design/downloads/index.html
Recompile project by "Clean"
Deleting res in bin folder
Deleting R.java in gen folder
Creating whole project altogether again
Changing option android:showAsAction to "never", "always", "ifRoom"
Placing .png file at right place
Not using .jpg only .png
Using Support library
Using lower case to define file names
Added following to menu tag,
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.actionbar.MainActivity"
Added follwoing to item tag
android:actionViewClass="android.widget.SearchView"
My main_activity_actions.xml looks like,
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="never" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" /></menu>
.... but still I cant see search image getting loaded up in menu bar. Though settings icon shows the action_search value defined in string.xml
I am not using Support Library Setup as of now, and android:minSdkVersion="11"
android:targetSdkVersion="19"
I need to show this, as there is something wrong here and as i go further I still cant load any other image. Please advise
You need to change the showAsAction as always or ifRoom to show the action icons in the menu bar.
If you set it to never , it will not be shown as an action menu. It will only show in PopupMenu
Change
android:showAsAction="never"
to
android:showAsAction="always"
append this line in your menu tag xmlns:yourapp="http://schemas.android.com/apk/res-auto"
and append these two lines in item tag
yourapp:actionViewClass="android.widget.SearchView"
yourapp:showAsAction="always"
i.e. use this layout
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search / will display always -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:actionViewClass="android.widget.SearchView"
yourapp:actionViewClass="android.widget.SearchView"
yourapp:showAsAction="always"
android:showAsAction="always"/>
All, Forgive me I just began to learn development of Android, It is the first time I used the ActionBar in my Android application. Firstly.
I was following the tutorial to add the resource of ActionBar to project. But the IDE alert me the resource can't be found. please help to review my current setup. thanks.
I had downloaded and upziped all the Icons. and copy all the icons under the res/drawable folder.
and I add a xml file named main_activity_actions.xml under the folder res/menu which content shows below.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/Core_Icons/unstyled/hdpi/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
Could someone please tell me why IDE can't find the icon ic_action_search.png? thanks.
Unfortunately the resource folders do not support directory hierarchies (sub-folders), so you will need to move all icons to the parent folder (drawable).
Then reference it like this: android:icon="#drawable/ic_action_search"
https://code.google.com/p/android/issues/detail?id=2018
I am using the actiobarcompat sample in my application and I am trying to implement search for pre 3.0 devices.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_search"
android:orderInCategory="1"
android:title="#string/menu_search"
android:icon="#drawable/ic_home"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
On Honeycomb+ this works fine, the searchview widget appears in the actionbar. What I am trying to do is have a second menu XML so I can fall back to the old search activity way of doing it. However, there is no such thing as menu-v11 folder as the menu folder is essentially menu-v11 because that is the version it started supporting this.
My question is, using the actionbar compatibility sample, is there a way to declaratively add an alternate button for pre-honeycomb?
Can you please be more specific about what you are trying to achieve?
It it's about calling different activities depending on API version, the action bar has nothing to do with that.
You analyse API version in onOptionsItemSelected and act accordingly.
If you want different menu items depending on API version, just create a folder menu-v11 (or menu-v14) and put
version-specific xml-s- there.
BTW, I use com.android.actionbarcompat and it works great for me!