I'm a new to Android development and I've been giving Android Studio a spin. I've followed Google's tutorial and I still haven't been able to get the ActionBar up and running either on the emulator or on the real device. I've specified the min version in the manifest file and I've also edited the menu and activity xml files accordingly.
Confusingly, the UIs shown in the activity_foo.xml and menu_foo.xml are different:
activity_foo.xml:
menu_foo.xml:
Even more confusingly, the final app when built shows both the Search and Settings in the hamburger menu though I do not recall seeing it in the GUI previews:
How can I fix this?
Confusingly, the UIs shown in the activity_foo.xml and menu_foo.xml are different
They are supposed to be different. One is showing you a layout file. The other is showing you a menu resource. Their previews are not supposed to necessarily match. After all, an Android app that is bigger than a breadbox will have many layout resources, few of which will be defining the contents of an activity.
For those layouts that do define the contents of an activity, IIRC, you can have the same tools:context=".FooActivity" in the root element of your layout file, and the preview may take that into account.
Even more confusingly, the final app when built shows both the Search and Settings in the hamburger menu though I do not recall seeing it in the GUI previews:
In the preview, the search item is represented by a toolbar-style button (icon is a magnifying glass).
In your menu resource, you have one <item> that has android:showAsAction and one <item> that has app:showAsAction. Either you are using the appcompat-v7 action bar backport, or you are not. That would be determined by things like:
what Java class your activity inherits from (ActionBarActivity or AppCompatActivity for appcompat-v7)
what theme you are using for the activity in your manifest (if it is based on Theme.AppCompat.*, you are using appcompat-v7)
If you are using appcompat-v7, you need to change the android:showAsAction to app:showAsAction. Given the results of your run of the project in the emulator, my guess is that you are using appcompat-v7. If you make the change to the menu resource and run the project again, you may see the search item show up as the magnifying glass icon, as you see in the preview. I say "may" because there may or may not be room to show that toolbar-style icon, depending on screen size and orientation of the device that runs your app — action bar items with ifRoom will show as toolbar-style buttons if there is room or will fall into the overflow menu if not.
Related
I've run into a very peculiar problem with Toolbars. The way my team currently uses the Toolbar is via a wrapper class, let's call it WrapToolbar.
So WrapToolbar will internally setup a Toolbar, and exposes some common Toolbar functions. Things like setTitle, getTitle, etc are available.
This WrapToolbar also sets up a default icon. This way all usages of the WrapToolbar will have this icon available for the screens that need it.
The problem with this approach is that for screens that require additional icons, have to add them via a Menu XML and the onCreateOptionsMenu function.
When we do that, the default icon setup with WrapToolbar gets pushed to the left, and the new icons from the Menu XML get added at the end.
This has worked fine for us until recently when requirements changed, and now are asking for the default icon to be at the end all the time.
I'm currently wrestling with the idea of doing a custom wrapper around Toolbar that exposes the ability to add icons, get the title, set the title, etc. This way we can put the icons in any order, and have ultimate flexibility in how our Toolbar looks.
I'm looking for some guidance on what that might look like. I also want to get some feedback on whether my initial idea to solve for this is flawed in some way I haven't foreseen.
Menus have a built in ordering based on the android:menuCategory attribute - you can use android:menuCategory="secondary" to push a menu item (like your default icon) to the end of the list - all MenuItems without a menuCategory will appear to the left of it.
Is there a way to completely remove the Android options menu in XE5 Delphi? I've been testing my application on a HTC One and because my phone doesn't have a menu button and my application doesn't have an options button, android automatically adds a options menu. Since this button is added by Android there is no way to add items to the menu.
I've already tried to change the minimal SDK version, but this makes the application very unstable and makes it crash when the orientation is changed:
<uses-sdk android:minSdkVersion="14" />
Is there an other way to remove the options menu? Now there is just a large options menu in the bottom of my screen that has no menu items.
Ok, you're right in that this empty action overflow menu is added because of your lack of hardware menu button.
On a Nexus 7 it's not such an issue as the empty overflow menu is added at the end of the other soft buttons.
On a HTC One, however, you can configure the Home button to act as a menu button, which removes the action overflow button.
It's not correct to say you can't add items to the menu. My Android session at CodeRage 8 shows how to add menu items. However, I'll grant you, it's a bit of a faff.
In order to remove it, the docs say you should set the targetSdkVersion attribute (not minSdkVersion) in your Android manifest to 14 (see this blog post for details). However having tried this it causes a crash if you don't prevent rotation in the RTM version of XE5, as you saw with your tests. This issue is sat in QC, logged some weeks back, hopefully to receive a fix in the near future.
However you should consider restricting the rotation as one course of action...... This is easy enough.
you must change in manifest android:configChanges="orientation|keyboardHidden">
to:
android:configChanges="orientation|keyboardHidden|screenSize">
then you can set minsdk, maxsdk, targetsdk as you want, and application will not crash
I have made several apps before using eclipse. I (think) always started out with file>new>project>application project and eclipse set up a project in which the main activity was comprised of a black screen with nothing in it - until I used the "graphical layout" editor to start adding some buttons/text etc.
But in more recent versions of eclipse it appears that however hard I try, I always seem to ens up with some components already on my activities screen. It appears that some "style" or "theme" (whatever they are) has been selected in the process and my activity starts with a fat bar across the top with my application's icon on the left of it. I have attempted to make edits to various xml files to remove the unwanted icon/bar, but so far to no avail.
What do I need to do to get a properly blank application so that I can be fully in charge of every pixel on the screen?
In your graphical layout preview select AppTheme select theme select Theme.NoTitleBar.FullScreen
You should see a preview with no title bar
Add this in manifest for your activity
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
Using SherlockActionBar I wrote a custom actionProviderClass (a spinner) for one of the action bar items.
I decided I don't need it and deleted all the files for it as well as the actionProviderClass attribute in the menu file. I even removed the spinner's values from strings.xml.
However, if I switch to horizontal orientation, the damn thing still shows up. I tried a fresh emulator as well as a device and it still happens. I also cleaned the project in Eclipse.
I guess something is being cached somewhere, anyone have any ideas?
User error here.
SherlockActionBar will collapse tabs into a spinner sometimes even though there is space for views
Are there open source versions of the standard Options, Home, Back, and Search icons that appear on every phone?
Check out this page, it offers a convenient way to browse the built-in drawables, most of which you can use just by referencing them in your layout.
See for example, the item ic_menu_home. You can probably use it in a layout like so
<android:Whatever
android:icon="#drawable/android:ic_menu_home"/>
Although, as the author of the page mentions, you sometimes have to copy items from the SDK resources folder if the above method throws an error.
Note also that while this method isn't guaranteed to match the icon you see, it will match what the user will see for that logical name. For example, I used this technique to add the "Preferences" icon to an options menu, and the emulator had a different icon than what was shown there. When you want a "standard" icon, this is good, although if you really wanted an exact image you could copy it into your app's res folder.