Menu Item Not Show In Action Bar In Sherlock? - android

i create action menu from action bar sherlock sample like below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Used to put dark icons on light action bar
//Create the search view
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search for countries…");
menu.add("Save")
.setIcon(R.drawable.abs__ic_menu_share_holo_light)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("Search")
.setIcon( R.drawable.abs__ic_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return true;
}
and my application theme theme is :
<style name="Theme.actor" parent="#style/Theme.Sherlock.Light">
</style>
but menu show in buttom of activity page not in actionbar?
any suggestion?
thanks
Solution:
i change
sherlock.setContentView(R.layout.main_switch);
to
setContentView(R.layout.main_switch);
and its works perfect.

Related

Android 12 | ActionMode menu items are not showing icons but only title in Contextual Action Mode, type_floating

I am using Action mode type_floating in webview where am showing action mode on long click of webview. It is showing action mode menu items correctly but in Android 12, it is just showing titlte names instead of icon. This is my onActionModeStarted method:
#Override
public void onActionModeStarted(ActionMode mode) {
Menu menu = mode.getMenu();
menu.clear();
mode.getMenuInflater().inflate(R.menu.menu_feature,menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mode.invalidateContentRect();
}
setFloatingMenuClickListeners(menu);
super.onActionModeStarted(mode);
}
private void setFloatingMenuClickListeners(Menu menu) {
MenuItem red = menu.findItem(R.id.red_view).setOnMenuItemClickListener(actionModeClick);
note.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
note.setIcon(R.drawable.icon_name);
MenuItem orange = menu.findItem(R.id.orange_view).setOnMenuItemClickListener(actionModeClick);
MenuItem pink = menu.findItem(R.id.pink_view).setOnMenuItemClickListener(actionModeClick);
}
In devices, below Android 12, it is showing only icons which is required. But in Android 12,showing only title.

Automatically open/close keyboard using SearchView in Toolbar

I'm having big problems opening and closing the keyboard using a searchview in my toolbar as actionbar. When I hit the searchbutton it expends as a action and the edit text appears, but it has no focus and they keyboards doesnt open. I have to manually click on the edittext to open the keyboard.
The same for closing the keyboard. When i hit the close searchaction button, they keyboard wont close.
I'm sure that this already worked without using the toolbar.... but I'm not able to fix it with toolbar.
Does anybody have an idea ?
This is my menu :
<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.toolbar.MainActivity" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.widget.SearchView"
android:title="#string/action_search"/>
<item
android:id="#+id/action_contact"
android:icon="#drawable/ic_action_settings"
app:showAsAction="ifRoom"
android:title="#string/action_settings"/>
And this is my onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
// Do not iconify the widget;expand it by default
searchView.setIconifiedByDefault(false);
setSearchIcons();
return super.onCreateOptionsMenu(menu);
}
Thanks for any advice.
Cheers!
EDIT1:
Well obviously after 10 hours of struggle I ask this question and 30min later I find a solution.
It was the app:showAsAction="ifRoom|collapseActionView"
collapseActionView. Deleting it did the trick.
But now I have another problem. When the search view expands the settings button looks very bad. The settings button is half visible/ half cut out.
On API 21 Nexus 4 Android 5 the search view cuts out the settings button.
On a THL 5000 with Android 4.4.2 the search view seems to work fine, but the icons are not white as on the Nexus but grey.
Any idea?
If your searchView is not always in expanded state, then you can try below code:
mSearchView.setIconifiedByDefault(true);
mMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Add code here
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Add code here
return false;
}
});
To hide keyboard after clicking enter/done on keyboard:
#Override public boolean onQueryTextSubmit(String query) {
mSearchView.clearFocus();
return true;
}

Android: Cannot show only text on overflow actionItem

I want to get an item to only show text on the optionsMenu in the action bar.
Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/select_recipients_return_to_compose"
android:showAsAction="ifRoom|withText"
android:orderInCategory="100"
android:title="Finish"/>
</menu>
Things I've tried:
1) True on onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.create_broadcast_options_menu, menu);
optionsMenu = menu;
MenuItem menuItem = menu.getItem(0);
menuItem.setEnabled(false);
// check if our roles recipients are chcked and if
Cursor cursor3 = getCheckedRecipients();
// actually, just checked if they're ticked.
if (cursor3 == null || cursor3.getCount() == 0) {
menuItem.setVisible(false);
} else {
menuItem.setVisible(true);
menuItem.setEnabled(true);
}
return true;
}
2) Setting the text manually
Anything else?
Right now it will show my home icon as the icon but will respond to clicks and behavior I set on onOptionsItemSelected().
android:showAsAction="withText|always"
forces the menuItem to the be a action of your actionbar.
if you want it to be in the overflow use
android:showAsAction="withText|never"

How to make popup menu like this using ActionBarSherlock

I am using ActionBarSherlock in my application and I want to implement popup menu like the below image, having a logo and respective text.
Please help me to achieve this, any help would be appreciable.
Thanks
Here it is.This is actually done in the app which you posted screenshot for.I think you are familiar with ActionbarSherlok.The button for this dropdown menu will be on actionbar.
public boolean onCreateOptionsMenu(Menu menu) {
// Used to put dark icons on light action bar
SubMenu subMenu1 = menu.addSubMenu("");
subMenu1.add("Item1").setIcon(R.drawable.icon).setOnMenuItemClickListener(
new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
return false;
}
});
MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setIcon(R.drawable.abs__ic_menu_moreoverflow_holo_dark);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
Added PopupMenu in ActionBarSherlock.

android-seach option in actionBar without creating custom theme

Is it possible to put a search option on the right of the action bar without having to create a custom action bar ?
Follow this code If you are action bar sherlock, you can implement search menu in action bar
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Search")
.setIcon(R.drawable.search)
.setActionView(R.layout.search_layout)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_COLLAPSE_VIEW);
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
//What do i write here?
return true;
}
In search_layout.xml
place a edittext that's enough.
Normal Android Actionbar search widget Implementation

Categories

Resources