I'm trying to create an inline menu as part of a layout. In other words, I want a "three dots" menu overflow icon (such as the one in the below example), placed on its own somewhere in my layout, and upon clicking it a standard menu should pop up.
At first I tried embedding a Toolbar from the support library in my layout, and while it does result in the menu overflow icon being shown, it's not ideal for my situation; a toolbar is a wide component intended to include a title, sub-title, icon etc. I only need the menu icon and behavior. Also, I have failed to style the toolbar to my liking, including getting rid of the quite large margin around the menu overflow icon. It would make more sense to use a simpler component for just the menu then trying to make a Toolbar fit into my layout.
I know that Toolbar internally uses an ActionMenuView to implement the menu, so I then tried including an ActionMenuView in my layout, and attach a menu to it from code:
<LinearLayout ...>
<android.support.v7.widget.ActionMenuView
android:id="#+id/menu_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
...
ActionMenuView amv = (ActionMenuView)findViewById(R.id.menu_button);
getMenuInflater().inflate(R.menu.some_menu, amv.getMenu());
amv.setOnMenuItemClickListener(...)
This however only results in an invisible view. When using a Toolbar and writing the equivalent code to attach a menu to it, the menu overflow icon is automatically shown and can be clicked, but when using a stand-alone ActionMenuView I just cannot get it to work.
I even tried to hard-code the width and height to 50dp, and while the view is visible in Android Studio's layout preview window, it's not visible on device (hierarchy viewer shows its height to be zero).
Now, of course I guess I can create an ImageButton with the desired icon and manually implement a menu behavior on it, but surely there must be a simple way to get the existing component working?
Related
Im making an app for android, without actionbar.
What is best practice for showing titles without actionbar?
The best practice is basically never using an action bar. This gives you a complete device screen to play with.
You can use images or different text styles for showing the title. You can also use interesting icons.
To remove action bar -
Go to res > values > styles.xml
Here is an illustration of styles file -
You can use the Toolbar (it's like the action bar, but more flexible). For example, action bar needs to be on top of the screen, but toolbar can have place anywhere in the layout.
You title can be shown as text or a beautiful asset.
If, for some reason, you don't want to use the toolbar, you can think about something like having a scrollView with the title on top of it.
Use a regular LinearLayout with a TextView inside. Style according to your needs, and set the text in the xml, or programmaticaly using TextView's setText() method.
I have an app, which have an action menu item on the right (2).
I need an ulterior menu in the left(1) or a button in the action bar, is that possible?
I attach a image.
enter image description here
You may create a custom toolbar. The standard height of a toolbar is 89dp.
To create a custom toolbar you should make your activity view container RelativeLayout. Then create a custom toolbar view (it may also be RelativeLayout) which height is 89dp. Then add your toolbar to the main container and set alignParentTop to true. So you have a custom flexible toolbar and you can add any view to it.
This way is very comfortable to use any custom views on your toolbar.
I also faced the same situation of customizing action bar. After a lot of searching I got the solution which is Toolbar.
Checkout: https://developer.android.com/training/appbar/setting-up
I think from now on, you should start using Toolbar as the default action bar for your apps. It provides high level of customization and material design features.
I am using the newest AppCompat library and replaced the SupportActionBar with a Toolbar, as suggested by the Android developer's blog. I want to display a second Toolbar at the bottom of the screen, that shows an additional Menu from some xml menu resource, but nothing else.
I have two problems with that:
Although nothing but a Menu is displayed (using Toolbar.inflateMenu()), the Menu occupies only a small space on the right side of the toolbar.
The Overflow Action Menu expands to the bottom of the toolbar and is therefore not shown. It should expand to the top.
Here is a screenshot of the bottom Toolbar:
How can I solve those issues without being forced to create custom views for my Buttons?
I have a similar problem as found on this specific question.
I'm using a Toolbar from the v7 support library, and a custom layout. If I have no options menu, I have a result similar to what OP has on the question linked above:
However if I have an options menu, it shifts the toolbar a little bit to the left (start). I would like to have it over the toolbar.
Is it possible? The only way I could hack it is by adding a negative right (end) margin to the toolbar layout, however this moves the menu outside of the screen…
Initially, the whole Toolbar contained several views, so they had to share the width with the action menu.
Now instead I wrapped the Toolbar and the other views in a container (a FrameLayout), so that they overlap without sharing the width. I just need to be careful with the margin at the top so that the other views don't overlap with action icons.
I want to make a action-bar drop-down-menu that shows items (icons) horizontally. Like this:
This is suppose to be a action-bar withe drop-down menu, where you can choose a color.
So it should show the icons (colors) horizontally and the icons should be clickable. Until now I have only managed to make a vertical list.
If possible I would like to do this in XML.
The ActionBar is not designed for such a use-case. The ActionBar buttons are meant to replace the old options menu that could be triggered with a separate hardware button on older devices (pre HC/ICS). The overflow button (the one with the 3 dots) that you've drawn in your sketch is used when there isn't enough room to show all buttons (the Android framework takes care of this automatically), so those will be grouped in the overflow menu. Overriding this behavior is a bad idea (if even possible).
Instead you should consider another approach: Add one ActionButton to the ActionBar that is meant to handle the color chooser. When this button is clicked the best solution is to show an AlertDialog (you can easily insert your on Views here that show the colors you want) because this will work best even if the button is hidden in the overflow menu.
Alternatively you could trigger a PopupMenu or implement some QuickAction to handle the color chooser (this will probably suck if your button is hidden in the overflow menu and will also lead to a very confusing UI).