android-seach option in actionBar without creating custom theme - android

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

Related

Multiselection mode doesn't end automatically when I click Delete

I'm working on a "small" example to learn about multiselect mode for a ListView. I set the mode on my list:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener(this));
}
Inflate the menu:
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = activity.getMenuInflater();
inflater.inflate(R.menu.context, menu);
return true;
}
And handle the menu event:
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch(item.getItemId()) {
case R.id.delete_menu:
activity.deleteSelectedWords();
return true;
}
return false;
}
Is there something else I need to do to end the action mode and return to the normal action bar?
Call clearChoices() on the ListView, as that should exit the action mode. Do this after you do your activity.deleteSelectedWords() bit. See this sample project for a complete working implementation.
Or, call finish() on the ActionMode itself.

Selecting overflow menu item on TextView CustomActionModeCallback

I am trying to present a custom action bar while long pressing a textview. My menu has more than 5 items which causes some of the items to be present under the overflow menu.
When I press the overflow icon, the action bar gets destroyed and I am not able to choose any item inside the overflow.
ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.add_rule_menu, menu);
return true;
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if (!mOptionsList.contains(item.getItemId()))
item.setVisible(false);
}
return false;
}
// Clicking on overflow button does not trigger this method at all.
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
// Rest of the code
}
}
public void onDestroyActionMode(ActionMode mode) {}
};
textView.setCustomSelectionActionModeCallback(mActionModeCallback);
I filed an issue about this years ago, which has never been resolved.
A cheesy workaround is to use nested action modes. By this, I mean you have an item in an action mode that finishes the current mode and starts a new one, to provide a "drill-down menu" effect. I use this in my recently-resuscitated RichEditText widget, which offers an action mode for formatting text. I add a "format" item to the default action mode via setCustomSelectionActionModeCallback(). Tapping on "format" opens another action mode that offers options like bold and italic, along with further drill-downs to get to thinks like font changes.

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.

Menu Item Not Show In Action Bar In Sherlock?

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.

How to start an action mode in android 3.0

We have this default feature in android 3.0 that when we long press somewhere (say in a webview) action mode is started and as a result the action bar changes with many default features like copy, find etc.
Now what I want is that I want to start this action mode but not by long pressing on the web view. I want that when I load a webview I want this action mode automatically started without any long press. Is it possible ? Is there any method by which we can achieve this?
In case you still need it or other people are looking for it
startActionMode(mContentSelectionActionModeCallback);
And as callback use:
private ActionMode.Callback mContentSelectionActionModeCallback =
new ActionMode.Callback() {
public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
{
//Here you can inflate a menu
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.name_menu, menu);
return true;
}
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem)
{
return false;
}
public void onDestroyActionMode(ActionMode actionMode) {
awesomeAdapter.overviewFragment.stopEdit();
}
};

Categories

Resources