Android: add actions from Menu to own title bar RelativeLayout - android

In Android project most Activities have ActionBar disabled with style
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
and use own title bar that is based on RelativeLayout via include
<include
android:id="#+id/activity_title_bar"
layout="#layout/view_title_bar" />
There is no intetion of using ActionBar but I would like to find how to implement adding Menu item on title bar like ActionBar does.
Currently those actions are defined in res/menun/common_actions.xml
and visible via options menu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.common_actions, 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_home:
MainActivity.start(this);
return true;
case R.id.action_search:
SearchActivity.start(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
However options menu button is not available on newer devices.
How to implement adding actions to title bar RelativeLayout?
There is no need for all Actionbar logic, just take 2 actions with icons from defined menu and put them on title bar.
Related docs:
http://developer.android.com/guide/topics/ui/menus.html
http://developer.android.com/guide/topics/ui/actionbar.html

You should really just use the Toolbar. It's rather quite simple and you can achieve exactly what you want...
..but to achieve what you want, you would use a PopupMenu and anchor it to an ImageView or ImageButton that looks like the overflow menu.

Related

Overflow Menu icon in Custom Toolbar

I am using a custom toolbar which has a menu icon. Now on clicking this menu icon i want to show the options menu. How can this be done.
I tried adding a onclicklistener to this menu icon
#Override
public void onClick(View v) {
if(v.getId() == R.id.toolbarMenuIcon){
openOptionsMenu();
}
}
This didnt work. Then i added these lines
setSupportActionBar(mBinding.customSelectToolbar.selectionModeToolbar);
in my activitys oncreate() . Also did override
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.selection_mode_menu, menu);
return super.onCreateOptionsMenu(menu);
}
With this i can see the overflow menu when i click the icon. But the problem is that, it adds the default menu icon also to the tool bar and thus my tool bar has two menu now. How can i have only my custom toolbar icon open the options menu
If you want to customize the default overflow menu icon..
Use setOverflowIcon method of your toolbar.
like:
toolbar.setOverflowIcon(ContextCompat.getDrawable(this, R.drawable.your_icon));
I got it working using theme
<style name="OverFlow" parent="Widget.AppCompat.ActionButton.Overflow">
<item name="android:src">#drawable/ic_menu_overflow</item>
<item name="android:tint">...</item>
<item name="android:width">..dp</item>
<item name="android:height">..dp</item>
</style>
<style name="OnArrival.toolbarTheme" parent="Theme.OnArrival.Light">
<item name="actionOverflowButtonStyle">#style/OverFlow</item>
</style>

How do I open a drop menu from a button in action bar

I'm developing an app for android which needs to have a button in the Action Bar(near the overflow) that opens a drop menu as the menu that comes out by clicking on the overflow button. Any ideas? Thank you
What you can do is adding an item to the action bar by filling the associated inflated XML file.
On the res/menu/action_bar.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">
<item
android:id="#+id/new_popup"
android:title="A popup example"
app:showAsAction="always" // Do not forget to put showAsAction to always so that this item will not be grouped with the overflow
android:visible="true">
<menu>
// Your popup items will be inserted programmatically here by adding item.getSubMenu().add(...) (see the code below)
</menu>
</item>
<item>
// The overflow items. They all will be hidden here (you put the showAsAction to never)
...
</item>
</menu>
On your activity you can inflate this menu :
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_bar, menu);
return true;
}
Then, populate the items on the menu and handle the click event on those items:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.new_popup)
{
item.getSubMenu().add(0, itemId, order, "your text").setIcon(your icon).setOnMenuItemClickListener(new OnMenuItemClickListener() {
// Handle this item click event
}
// Add more items!
}
The "plus" button in the action bar needs to open a menu as the one that comes out by clicking the overflow button. Eventually I need both menus.
You can use Dropdown menu navigation in action bar to do that, here is the example also see this example, hope this helps :)

Action View in android l

I have an application with a search widget in the action bar. The application is not creating the search widget as an actionview when I switch the theme to use Material.Light in android L. When debugging I can see that the search view is null.
The menu item:
<item
android:id="#+id/search"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/title"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always" />
The creation of the menu:
Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.homescreen, menu);
return true;
}
The initialization of a variable for the search view:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setOnQueryTextListener(queryListener);
return super.onPrepareOptionsMenu(menu);
}
Any ideas?
Update 1:
Ok so I stopped the app from crashing by replacing app:actionViewClass="android.widget.SearchView" with android:actionViewClass="android.widget.SearchView". But now the search item shows up in the overflow menu rather than the action bar even though it is set to show as action always.
I had this same problem with setting it to show "always" and it ended up always being in the overflow. If you are using the support library make sure you are extending ActionBarActivity, if not, the regular Activity should work.

Unable to show the 3 dotted menu in overflow - SherlockActionBar

I've searched alot around the internet. I've done what I had to, but still I'm unable to show the 3 dotted menu as I want.
Here's the relevant code:
First, my manifest min sdk is set to 9 and my targetSdk is set to 18.
The menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/itemMainAlbum"
android:showAsAction="ifRoom"
android:title="Set as Main Album">
</item>
<item
android:id="#+id/itemImport"
android:showAsAction="ifRoom"
android:title="Import">
</item>
<item
android:id="#+id/itemSettings"
android:showAsAction="ifRoom"
android:title="Settings">
</item>
The Activity's relevent code (extends SherlockActivity):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.gallery_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.itemImport:
startImportActivity();
return true;
case R.id.itemMainAlbum:
setMainAlbum();
return true;
case R.id.itemSettings:
// do s.th.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Here's how my activity looks like:
The menu item in the bottom is shown only when I click on my device's menu button. The 3 dotted menu is just never shown, no matter how many menu items I add.
If you use ActionBarSherlock, you can use this little hack. It works flawlessly for me and shows the menuoverflow on every device I have tested it on.
The "3 dotted menu" will never be shown, as your device has a MENU button. The "3 dotted menu" will only appear on devices that lack a MENU button, to allow such users the ability to access the overflow. You can see what the "3 dotted menu" looks like by setting up an emulator that emulates a device with no MENU button.
You can read more about this in the "Say Goodbye to the MENU Button" Android Developers Blog post.

Menu overflow button on ActionBar/ActionBarSherlock

This app was designed before the deprecation of the hardware menu button, so now it automatically shows a menu overflow button at the bottom of our app. We'd rather it wedge itself to the right of our 4 tab bar items when needed.
If that's not doable, we'd at least like to be able to center the lonely menu overflow button like it appears on the first pic here -> http://www.droid-life.com/2012/05/30/dear-developers-can-we-quit-with-the-menu-button-already-and-adopt-an-action-overflow/
We are using ActionBarSherlock.
You must add it mannually with setAddOptionMenu(true);
and then create it in xml for example:
<item
android:id="#+id/video"
android:title="#string/video"
android:icon="#drawable/video"
android:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/email"
android:title="#string/email"
android:icon="#drawable/email"
android:showAsAction="ifRoom|withText"
/>
Then inflate it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.items, menu);
return super.onCreateOptionsMenu(menu);
}
if you want to split the actionBar in the bottom of the screen you can use
android:uiOptions=”splitActionBarWhenNarrow”
in your activity in the manifest
hope it helps

Categories

Resources