Custom Navigation bar and option button - android

How to set navigation bar in android and how to add option button on navigation bar ?

#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("menu");
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (menuDialog == null) {
menuDialog = new Dialog(this, R.style.Dialog_Fullscreen);
menuDialog.setContentView(menuView);
menuDialog.show();
} else {
menuDialog.show();
}
return false;
}

Related

How to hide menu item from toolbar when click on search icon in android

My toolbar contains search icon and refresh icon,when i click on search icon then search view inflate from onCreateOptionMenu() but the refresh icon does not get hide.If i hide refresh icon in onMenuItemActionExpand() then on back press of search view back icon both menu items search and refresh gets hide and default setting icon gets visible.Below is the code which i have implemented.Please help me!!!
MenuItem.java
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
item_search = menu.findItem(R.id.action_search);
item_refresh = menu.findItem(R.id.action_refresh);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item_search);
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// hide action item
if (menu != null) {
menu.findItem(R.id.action_refresh).setVisible(false);
}
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
// re-show the action button
if (menu != null) {
menu.findItem(R.id.action_refresh).setVisible(true);
}
return false;
}
});
Well you could imitate that yourself by hiding all the other items when the SearchView is expanded:
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (android.widget.SearchView) searchItem.getActionView();
// Detect SearchView icon clicks
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setItemsVisibility(menu, searchItem, false);
}
});
// Detect SearchView close
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
setItemsVisibility(menu, searchItem, true);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
private void setItemsVisibility(Menu menu, MenuItem exception, boolean visible) {
for (int i=0; i<menu.size(); ++i) {
MenuItem item = menu.getItem(i);
if (item != exception) item.setVisible(visible);
}
}
​MenuItem action_search;
MenuItem action_refresh;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
action_search = menu.findItem(R.id. action_search);
action_refresh = menu.findItem(R.id. action_refresh);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
item_search = menu.findItem(R.id.action_search);
item_refresh = menu.findItem(R.id.action_refresh);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item_search);
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// hide action item
if (action_refresh != null) {
action_refresh.setVisible(false);
invalidateOptionsMenu();
}
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
// re-show the action button
if (action_search != null) {
action_search.setVisible(true);
}
if (action_refresh != null) {
action_refresh.setVisible(true);
}
invalidateOptionsMenu();
return false;
}
});
​
It seems that it's a bug from android, you can use the solution here!

Android - remove three dots in SearchView

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;
}

How do you "deflate" menus in android?

I have a contextual action bar, where I have a setting called "DELETE". When I press that button I want the CAB menu to disappear.
class MyActionModeCallBack implements android.view.ActionMode.Callback {
#Override
public boolean onCreateActionMode(android.view.ActionMode actionMode, Menu menu) {
actionMode.getMenuInflater().inflate(R.menu.event_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(android.view.ActionMode actionMode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(android.view.ActionMode actionMode, MenuItem menuItem) {
int id = menuItem.getItemId();
if (id == R.id.delete) {
*******
_root.removeView(view);
// This is where I want to remove the CAB menu
*******
}
return false;
}
#Override
public void onDestroyActionMode(android.view.ActionMode actionMode) {
((RelativeLayout)view).removeAllViews();
view.setBackgroundColor(0xFF00FF00);
view.setTag(R.string.viewSelected, "0");
}
}
I was thinking about calling the onDestroyActionMode() right after the _root.removeView(view);, but I don't know what arguments to pass in.
Any suggestions are appreciated, thanks!
If you are trying to hide the item being selected, you can call
menuItem.setVisible(false);
Alternatively, if you want to remove all items from the menu, you can call
optionsMenu.clear();
in the onActionItemClicked() method.
You can save your menu to a field in your onCreateOptionsMenu method as follows:
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
optionsMenu = menu
}
Call actionMode.finish(). This will invoke the onDestroyActionMode() callback.

contextual action bar padding in Android

I want to use contextual action bar in my app. When I start action mode, There are padding between default actionbar and contextual actionbar as shown in image 2. How can I avoid this padding ?
for start action mode
startSupportActionMode(modeCallBack);
callback method :
private ActionMode.Callback modeCallBack = new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.driver_main_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_send:
Toast.makeText(getApplicationContext(), "Send!", Toast.LENGTH_SHORT).show();
mode.finish();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
};

How to click disabled option menu item in Toolbar android?

I have two option items in a Toolbar. When one item is clicked, that item will be enabled. Then another item must be disabled. But, once the item was disabled I can't fire click event anymore on that item. Is there anyway that I can click on the disable item?
Thank you
I did like this but its not working anymore
MenuItem tureMenuItem;
MenuItem dingMenuItem
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.lore_fragment, menu);
tureMenuItem = menu.findItem(R.id.menu_ture);
dingMenuItem = menu.findItem(R.id.menu_ding);
tureMenuItem.setEnabled(true);
dingMenuItem.setEnabled(false);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_ding:
tureMenuItem.setEnabled(true);
dingMenuItem.setEnabled(false);
break;
case R.id.menu_ture:
tureMenuItem.setEnabled(false);
dingMenuItem.setEnabled(true);
break;
}
return super.onOptionsItemSelected(item);
}
Using setEnabled() will work. manage with your conditions.
you must be doing some action on those menu items right. You can enable the other item at the end of action.since you have not posted I have only option. Using Handler.PostDelayed.
boolean isMenuEnabled;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.action_settings) {
this.isMenuEnabled = true;
tureMenuItem.setEnabled(true);
dingMenuItem.setEnabled(false);
handler.postDelayed(runnable,1000);
return true;
}
else if(item.getItemId()== R.id.action_settings1) {
this.isMenuEnabled = true;
tureMenuItem.setEnabled(false);
dingMenuItem.setEnabled(true);
handler.postDelayed(runnable,1000);
return true;
}
return super.onOptionsItemSelected(item);
}
remove the callbacks
#Override
protected void onDestroy() {
handler.removeCallbacks(runnable);
super.onDestroy();
}
runnable with delay
Runnable runnable = new Runnable() {
#Override
public void run() {
if(isMenuEnabled){
tureMenuItem.setEnabled(true);
dingMenuItem.setEnabled(true);
isMenuEnabled=false;
}
}
};
You need to check below example code to disable and enable menu item vice-versa.
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
if(menu.getItem(0))
{
menu.getItem(0).setEnable(false);
menu.getItem(1).setEnable(true);
}
else if(menu.getItem(1))
{
menu.getItem(1).setEnable(false);
menu.getItem(0).setEnable(true);
}
return super.onPrepareOptionsMenu(menu);
}

Categories

Resources