I am using ActionBarSherlock:
There are 3 menu items, of which only 1 has an ActionLayout.
default_menu.xml
<item
android:id="@+id/searchIcon"
android:icon="@drawable/search_icon"
android:actionLayout="@layout/search_icon_actionview"
android:showAsAction="always|collapseActionView"
android:title="Search"/>
<item
android:id="@+id/newIcon"
android:icon="@drawable/new_icon"
android:orderInCategory="0"
android:showAsAction="ifRoom|collapseActionView"
android:title="New"/>
<item
android:id="@+id/notificationIcon"
android:icon="@drawable/notification_icon"
android:orderInCategory="0"
android:showAsAction="ifRoom|collapseActionView"
android:title="Notifications"/>
if i am using the above code. Only 1st 2 icons are visible. And if i make 2nd and 3rd as Always they remain even when the actionLayout is opened when clicked on Search.
I want to show all the 3 Menu Items initially and when search_icon is clicked hide every Item.
I also have a doubt about my implementation, whether i am doing i the right way.
In the Activity's(which has a viewPager) first Fragment:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.searchIcon:
mEtSearchbar.clearFocus();
(new Handler()).postDelayed(new Runnable() {
public void run() {
mEtSearchbar.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
mEtSearchbar.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
}
}, 100);
return true;
case R.id.newIcon:
return true;
case R.id.notificationIcon:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
inflater.inflate(R.menu.default_menu, menu);
mSearchbar = (MenuItem) menu.findItem(R.id.searchIcon);
View actionview = mSearchbar.getActionView();
mEtSearchbar = ((EditText) actionview.findViewById(R.id.search_editText));
ImageView searchImage = ((ImageView) actionview.findViewById(R.id.search_image));
searchImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String s = mEtSearchbar.getText().toString();
Toast.makeText(getSherlockActivity(), "Searching for: " + s, Toast.LENGTH_SHORT)
.show();
}
});
super.onCreateOptionsMenu(menu, inflater);
}
Call invalidateOptionsMenu() when you want to hide the option.
To hide or display buttons on a toolbar, press the down arrow on the right of the desired toolbar and then press the Add or Remove Buttons item.
How do I hide a menu item? If you want to change the visibility of your menu items on the go you just need to set a member variable in your activity to remember that you want to hide the menu and call invalidateOptionsMenu() and hide the items in your overridden onCreateOptionsMenu() method.
onCreateOptionsMenu() is called by the Android runtime when it need to create the option menu.
This is what i ended up using.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.search);
mSearchView = (SearchView) searchItem.getActionView();
setupSearchView(searchItem);
mSearchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// hide action item
if (menu != null) {
menu.findItem(R.id.notifications).setVisible(false);
menu.findItem(R.id.share).setVisible(false);
}
}
});
mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
adapter.getFilter().filter("");
// re-show the action button
if (menu != null) {
menu.findItem(R.id.notifications).setVisible(true);
menu.findItem(R.id.share).setVisible(true);
}
return false;
}
});
return true;
}
private void setupSearchView(MenuItem searchItem) {
//code
}
I think you could set something like this on the searchView :
searchCommand.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
I am not sure but try this once.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.map_menu, menu);
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if (item.getItemId() == R.id.menu_more) {
itemChooser = item.getActionView();
if (itemChooser != null) {
itemChooser.setOnClickListener(this);
}
}
}
return super.onCreateOptionsMenu(menu);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With