I want to add a clickable icon on the default title bar looks like
edited with
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem menuItem = menu.add("title is needed");
menuItem.setIcon(R.drawable.call);
return true;
}
Looks like
Override method onCreateOptionsMenu in your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem menuItem = menu.add("title if needed");
menuItem.setIcon(R.drawable.YOUR_ICON_HERE);
// the rest of the code here...
}
In fact if you are planning to use this action bar on old platforms as well (2.x-3.x), you better consider using ActionBarSherlock
Check this code,
public boolean onCreateOptionsMenu(Menu menu) {
// Used to put dark icons on light action bar
menu.add("Search")
.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
Intent search = new Intent(MainActivity.this,
SearchActivity.class);
startActivityForResult(search, 0);
overridePendingTransition( R.anim.righttoleft, R.anim.stable );
return false;
}
}).setIcon(R.drawable.ic_search)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
Related
I have a navigation drawer and I would like to add a view (Button, etc) to the menu. I don't want to use XML, therefore I am not using an inflator. I tried the setActionView method based on these examples at https://www.codota.com/android/methods/android.view.MenuItem/setActionView but I am getting an empty menu. Is what I am trying to do is possible? Here is my relevant part of code:
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add("search");
SearchView sv = new SearchView(this);
item.setActionView(sv);
return true;
}
private static final int MENU_ITEM_ITEM1 = 1;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, MENU_ITEM_ITEM1, Menu.NONE, "Item name");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ITEM_ITEM1:
//your action
return true;
default:
return false;
}
}
How can I remove the three dots to the right of my SearchView ?
set false in onPrepareOptionsMenu
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
return false;
}
Maybe this helps you
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity, menu);
MenuItem searchMI = (MenuItem) menu.findItem(R.id.menu_search);
searchMI.setOnActionExpandListener(new OnActionExpandListener(){
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
// Hide menu icon
return true;
}
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
// Show menu icon
return true;
}
});
return true;
}
You can show or hide icons like this:
menu.findItem(R.id.yourActionId).setVisible(true);
menu.findItem(R.id.yourActionId).setVisible(false);
You need to hide all others menu items one by one...
#Override
public boolean onMenuItemActionExpand (MenuItem item){
menu.getItem(MENU_MAIN_REFRESH).setVisible(false);
menu.getItem(MENU_MAIN_SETTINGS).setVisible(false);
menu.getItem(MENU_MAIN_EXIT).setVisible(false);
return true;
}
I have a option Menu in my toolbar in app.but I want to Is unseen Some places.
What solution do you recommend to it my friends?
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//codes...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.optionmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.item1) {
return true;
}
return super.onOptionsItemSelected(item);
}
TO:
Just remove onCreateOptionsMenu and onOptionsItemSelected method from activity.
try : Open menu xml using ctrl + click on R.menu.optionmenu in your code. Delete all "item" in xml, if you want to add menu items afterwards.
or
try : remove onCreateOptionsMenu(Menu menu) and onPrepareOptionsMenu(Menu menu) from Activity if you don't want to use action bar menus at all.
You can change this code and try again :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if("Your condition")
{
getMenuInflater().inflate(R.menu.optionmenu, menu);
}
return true;
}
I am displaying menu action bar at the bottom of the screen. when user click/touch any of the menu item, i want to highlight it (i.e. the way button click highlighting happens). i tried onClickListener and ontouchListener but it doesn't highlight.
can someone tell me which porperty/method i have set.
Here is code i am using.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.landing_page_layout);
ActionBar actionBar = getActionBar();
actionBar.show();
// business logic }
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_items, menu);
item1 = menu.findItem(R.id.menu_option1);
item1.getActionView().setOnTouchListener(new OnTouchListener() {
// logic when user touch menu option1 touch
}});
Thanks
Chintan
Check this section in the documentation: http://developer.android.com/guide/topics/ui/menus.html#options-menu
To set the menu up you do this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
Where R.menu.menu points to your res/menu/menu.xml file. This will load the elements from that file
The options menu is listened to in the same way as regular View's with OnClickListeners and such. Instead you onOptionsItemSelected override like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.item1:
// Do something
return true;
case R.id.item2:
// Do something else
return true;
default:
return super.onOptionsItemSelected(item);
}
}
How do I implement an onMenuItemClickListener?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.gameoptions, menu);
menu.findItem(R.id.menu_item).setIntent(new Intent(this, QMenuActivity.class));
menu.findItem(R.id.back_item).setOnMenuItemClickListener;
return true;
}
I want the back_item once clicked on to call a function within the page, how do I do it?
I'm assuming you want to access a non-static function in your activity. Without too much information from you, try something like what's below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.back_item);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
YourActivity.this.someFunctionInYourActivity();
return true;
}
});
return true;
}
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener()
change the code to include MenuItem after new.