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>
Related
I've created a menu with a single item.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/syncButton"
android:title="Sync"
android:icon="#drawable/ic_sub_menu"
app:showAsAction="never"/>
</menu>
This is used on some of my activities in the toolbar, when clicked it drops down a menu, currently there is only one option but in the future it may be more.
Everything works well except the icon, it's a vector image of the traditional 3 dots colored white. Depending on what showAsAction" is set as it changes color.
Currently showAsAction is set to never so it displays a menu when clicked, this is what I want, but the icon changes to a dark grey. If i set this option to "always" then the icon changes to white but I lose the drop down menu.
How can I keep the drop down menu while keeping my icon white ?
If you just want to change the 3-Dots-Icon for the DropDownMenu, you can change this in your Styles.xml:
In your Theme Style define
<item name="android:actionOverflowButtonStyle">#style/MyTheme.OverFlow</item>
and then define the Overflow with your Icon you want to show instead the 3DotsIcon (in your case the Icon with 3 white dots)
<style name="MyTheme.OverFlow">
<item name="android:src">#drawable/yourNewIcon</item>
</style>
Try this code
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.menu_confirm, menu);
MenuItem action_done = menu.findItem(R.id.action_done);
action_done.setIcon(R.drawable.ic_filter);
menuIconColor(action_done, Color.WHITE);
super.onCreateOptionsMenu(menu, menuInflater);
}
public void menuIconColor(MenuItem menuItem, int color) {
Drawable drawable = menuItem.getIcon();
if (drawable != null) {
drawable.mutate();
drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}
}
I am using the ActionMode via:
#Override
public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_list_context, menu);
this.mActionMode = mode;
return true;
}
And my resource file menu_list_context is:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/discard_button"
android:icon="#drawable/ic_action_discard"
android:title="delete"
android:showAsAction="ifRoom" />
The style for my ActionBar is:
<style name="MyActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar.Solid">
<item name="android:background">#color/background_holo_light</item>
</style>
where <color name="background_holo_light">#dddddd</color>.
With these settings, my ActionBar under ActionMode looks like this:
The icon has the background of my ActionBar (holo light), but the ActionMode background seems white. How can I fix the icon background to have the ActionMode background (including the blue bottom line)?
You could try using the Action Bar Style Generator. This is the only way I know of to get exact ActionBar styles that actually works.
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.
I'm making some application with action bar. But when I try to run my application on my emulator I get only optional menu with items from my activity_main_actions.xml. If anybody help me what I need to do fix my problem and to get Action Bar instead of Optional Menu when I try to rum my application. This is my code:
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_launcher_search"
android:showAsAction="ifRoom"
android:title="#string/action_search"/>
<!-- Location Found -->
<item
android:id="#+id/action_location_found"
android:icon="#drawable/ic_launcher_location_found"
android:showAsAction="ifRoom"
android:title="#string/action_location_found"/>
<!-- Refresh -->
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="ifRoom"
android:title="#string/action_refresh"/>
And My Main Java Class is :
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
}
I think you confused names here. Progress bar is used for reporting/showing progress to user. While the action bar (I think this is the one you want) is a window feature for navigation, actions, etc.
I would suggest reading the following link http://developer.android.com/guide/topics/ui/actionbar.html#Adding
There you will find what is needed to add the action bar.
After that, if you need to add a Progress bar in your action bar, you need to read this tutorial:
Refresh item inside ActionBar using circular ProgressBar
If I define the following items for my action bar:
res/menu/action_menu.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="label"/>
<item android:title="label1"/>
<item android:title="label2"/>
<item android:title="label3"/>
<item android:title="label4"/>
</menu>
In my Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
return true;
}
Is there anyway to allow me define certain items move to action overflow part ? and how to do it?
P.S. Action overflow part is the right-most part of action bar which hide certain items like a popup menu.
It's the other way round. You need to explicitly tell the menu which ones you want in the ActionBar and which not by setting the appropriate flags
E.g.
<item android:id="#+id/refresh"
android:title="#string/refresh"
android:icon="#drawable/reload_button"
android:showAsAction="always"/>
Here android:showAsAction tells how to handle it. Options are
always
ifRoom
never
withText
You can or options together with the pipe symbol as "always|withText"
See the android docs for action bar for more documentation.
To add something to Heiko's answer about the "overflow menu" on the action bar, this only happens if you have items set as ifRoom and there is no room for them to be displayed. On the overflow menu they only appear with a title and no icon.
On Android 4.0, the overflow menu ("3 dot spinner") is only shown on devices that don't have the physical "menu" button. You can test this on an ADV setting the option Hardware Back/Home keys option to "no".