I want to provide a image button in place of action bar and on clicking that I want to display list of options and again on clicking any particular option I want to pass Intent.
How can I achieve this?Does anybody know
If so help me out.
Thank you...
In order to display the imagebutton in Actionbar. First add an item in main.xml.Which is Under MENU Folder.
for ex:
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/stopwatch"
android:orderInCategory="50"
android:icon="#drawable/ic_action_alarms"
android:showAsAction="always|withText"
android:title="Stopwatch"/>
</menu>
then add the following code in your mainActivity.java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId())
{
case (R.id.stopwatch):
Toast.makeText(getApplicationContext(), "Stopwatch", Toast.LENGTH_SHORT).show();
Intent i=new Intent(getApplicationContext(), Stopwatch.class);
startActivity(i);
break;
return super.onOptionsItemSelected(item);
}
and Don't forget to create another java class as Stopwatch.java
Go through the following link it will help you
https://developer.android.com/training/basics/actionbar/adding-buttons.html
You can set a custom view on your action bar, here is the link
How to display custom view in ActionBar?
It can be done in two ways
Options 1: Create an layout XML with your image button, and set that layout to action bar.
For example: In you activity copy the following lines.
ActionBar actionBar = getSupportActionBar();
LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.search, null);
actionBar.setCustomView(v);
Now display a PopUp menu when the user clicks a ImageButton.
Option 2:
Instead of setting the view for the ActionBar , you can use sub-Menu's in the action bar. For this you need to create sub-Menu.
for example:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/file"
android:icon="#drawable/ic_new_game"
android:title="#string/file" >
<!-- "file" submenu -->
<menu>
<item android:id="#+id/create_new"
android:title="#string/create_new" />
<item android:id="#+id/open"
android:title="#string/open" />
</menu>
</item>
</menu>
Set an icon for the main `MenuItem` so it looks like an `ImageButton`, when the user click the first MenuItem it displays the its sub-`Menu`
Related
I have NavigationView in my DrawerLayout and let's say it has simple menu
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_settings_black_24dp"
android:title="#string/title_settings"/>
</menu>
Now I set listener for click:
mNavigationVeiw.setOnNavigationItemSelected(this);
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_settings:
SettingsActivity.startActivity(this);
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
return false;
}
The problem is that I don't see ripple effect. When I long press on item, the ripple is showing, but with click it's not working. I think the problem is that I open Activity after click instead of replacing fragments, and my menu should have addition options. How can I fix that?
EDIT
Everything works fine when I set <group android:checkableBehavior="single"> but in my case it's not this behavior, cause I'm launching another activity and it should not check clicked item, because in this new Activity is back button on the top instead of access navigation drawer.
Add itemBackground into NavigationView inside your xml file the following attribute:
app:itemBackground="#drawable/ripple_navigation_selector"
Also, inside the drawable-v21 folder, add ripple_navigation_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/submit_btn_ripple_color" >
<item android:drawable="#color/accentColor" />
<item
android:id="#android:id/mask"
android:drawable="#android:color/white" />
</ripple>
Is it possible to add Action Item in ActionBar, which show drop down menu on click?
paint example:
P.S. ActionBar already contains navigation drawer toggle button, title and overflow menu.
Where do I initialize that button, in which xml?
How do I set such drop down operations to Action Item?
How to set the content of such drop down menu?
And how to get access to specific item click action?
Some working code examples would be great!
Thanks in advance. Appreciate any help.
So I found solution by myself.
You need to inflatean Action Item in onCreateOptionsMenu(Menu menu):
getMenuInflater().inflate(R.menu.global_filters, menu);
global_filters.xml:
<?xml version="1.0" encoding="utf-8"?>
<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=".NavigationActivity">
<item android:id="#+id/action_filters"
android:title="Фильтры"
android:icon="#drawable/ic_filter_white"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
</menu>
...which is that downside arrow:
Then create a PopupMenu. I did it in onOptionsItemSelected:
View menuItemView = findViewById(R.id.action_filters); // SAME ID AS MENU ID
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.popup_filters_user);
popupMenu.show();
and here you set .xml file with items of drop down menu in file popup_filters_user.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/filter_bought_user"
android:title="Купленые"/>
<item
android:id="#+id/filter_price_user"
android:title="Цена"/>
<item
android:id="#+id/filter_author_user"
android:title="Автор"/>
</menu>
and hooray! Here's the result:
If your menu is in a Fragment then you might use the following to get the View
View menuItemView = MainActivity.getInstance().getWindow().findViewById(R.id.action_filters);
I can't show items inside the "Menu Item" when those items are already displayed on the Action bar.
This is my onCreateOptionsMenu method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.opt_menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
This is my opt_menu_main layout:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_act_settings"
android:title="Settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="ifRoom"/>
</menu>
I'm not using support library (my target API is 17).
If I click on the button Menu it is always empty because the item is already displayed up on the ActionBar.
I've tried to add more items but when the items goes up on the ActionBar, they are not displayed inside the options menu.
I've also tried with showAsAction="ifRoom|withText"but doesn't work.
I think this behavior is correct, but is it possible to show the same item both in Menu and Action Bar at the same time?
Thanks
Try this:
app:showAsAction="always"
but don't forget to add this line at the begin of definition:
xmlns:app="http://schemas.android.com/apk/res-auto"
The Id's for menu items must be unique. The best option is to create another menu item with another Id and set its showAsAction to "never" to force it always into the overflow menu. But their is a chance if you are also displaying other primary options it might force both into the overflow in which case you will see two menu items with the same title.
For the moment I solved like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_act_settings"
android:title="Settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="always"/>
<item
android:id="#+id/menu_settings"
android:title="Settings"
android:showAsAction="never"/>
</menu>
So in the onOptionsItemSelected method :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add_schedule:
case R.id.menu_act_add_schedule:
//some code
break;
}
return true;
}
I don't think this is a correct solution, because on some device where there is not enough space to add this and other items up on the ActionBar,
since they are defined as showAsAction="always", they will be overlapped on the ActionBar Title, so it can create some layout problems.
The behavior that I want is to show always the items on the option menu, and show it also in the action bar if there is enough space.
This behavior can not be obtained with showAsAction="always" .
It would be great if someone could find a better solution.
Thanks anyway.
I'm using a normal Action Bar Sherlock Action Bar but noticed that, if I opted to hold down on the item as opposed to clicking on it, a black box would appear. I have two questions:
What is its intended purpose?
How do I get rid of it?
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/refresh_button_actionbar"
android:icon="#drawable/refresh_circle"
android:showAsAction="always">
</item>
</menu>
onOptionsItemSelected:
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.refresh_button_actionbar:
this.refresh();
break;
default:
return false;
}
return true;
}
The black box is somekind of toast indicating the "description" of your button.
<item
android:id="#+id/refresh_button_actionbar"
android:icon="#drawable/refresh_circle"
android:title="Refresh"
android:showAsAction="always">
Add the android:title tag into your menu item and you will see it appear on black box, not sure how to get rid of it tough.
Can you please tell me how can I create an android menu item using android setting icon?
Here is a list of the standard icons. I don't see a "settings" icon. Perhaps you mean "Preferences" (ic_menu_preferences)?
You can set the icon programmatically like this:
menu.add(0, MENU_QUIT, 0, "Quit").setIcon(R.drawable.menu_quit_icon);
You can also set it in your xml layout like this:
<item android:id="#+id/save_button"
android:icon="#android:drawable/ic_menu_save"
android:title="Save Image"/>
Creating Menus in Android
You can see all the icons in the android SDK forder:
_your_install_path_\android-sdk\platforms\android-10\data\res\drawable-hdpi\
and then get a reference to them with:
android.R.drawable.ic_menu_preferences
just like it was your drawable.
Add a new Vector Asset.
File -> New -> Vector Asset.
Click on the icon to change it.
Select the icon you want (e.g. search for "setting").
Adjust other settings.
Use that new Vector Asset in your xml.
android:logo="#drawable/ic_settings_white_24dp"
Party!
If you want to handle the event, just try this on your activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case android.R.drawable.ic_popup_sync:
Toast.makeText(this, "ic_popup_sync selected", Toast.LENGTH_SHORT)
.show();
break;
default:
break;
}
return true;
}
And in your menu folder use something like this:
<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="com.example.test.app.MainActivity"
>
<item android:id="#+id/action_settings1"
android:icon="#drawable/abc_ic_search"
android:title="Find Location"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
<item android:id="#+id/save_button"
android:icon="#android:drawable/ic_menu_save"
android:title="Save Image"/>
<item android:id="#+id/refresh"
android:icon="#android:drawable/ic_popup_sync"
android:title="Refresh"/>
</menu>