How to check if the back button of the ActionBarSherlock was clicked? - android

I'm using the library ActionBarSherlock, and put a item with the property android:showAsAction="ifRoom|collapseActionView". How to check if the back button of the ActionBarSherlock was clicked? thanks!

you have to override
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
}
return true;
}
from the doc:
his hook is called whenever an item in your options menu is selected.
The default implementation simply returns false to have the normal
processing happen (calling the item's Runnable or sending a message to
its Handler as appropriate). You can use this method for any items for
which you would like to do processing without those other facilities.

the answer above works (Thanks). But for my code, this solution works best ...
#Override
public boolean onOptionsItemSelected(
com.actionbarsherlock.view.MenuItem item) {
item.setOnActionExpandListener(new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// running changes ...
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// running changes ...
return true;
}
});
return super.onOptionsItemSelected(item);
};

Related

Setting visibility of a view when a menu item is selected and when we are done with the menu item

I have implemented a menu item to search a list view. I need to make a view invisible when the menu item is selected. This is easily done with this code in my fragment:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_search:
addButton.setVisibility(View.INVISIBLE);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I can't figure out how to set the visibility back when I am done with the search (I am using SearchView). I tried to use onOptionsMenuClosed (Menu menu) but that is not being called for some reason.
Thanks in advance
Try using setOnActionExpandListener():
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu, menu);
menu.findItem(R.id.action_search).setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
addButton.setVisibility(View.VISIBLE);
return false; // change to true if `false` wont work for your case
}
});
return super.onCreateOptionsMenu(menu);
}
onMenuItemActionCollapse() will be called when SearchView is collapsed or closed.

simulate onOptionsItemSelected inside method call

I have an ImageView inside a xml layout. The ImageView has an onClick method.
android:onClick="onHomeClicked"
When the user clicks my image, I want that to in turn call onOptionsItemSelected inside the activity. How would I do that?
The following code gives null for homeMenuItem:
MenuItem homeMenuItem;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
homeMenuItem = menu.findItem(android.R.id.home);
super.onPrepareOptionsMenu(menu);
return true;
}
public void onHomeClicked(View view) {
onOptionsItemSelected(homeMenuItem);
}
You have no MenuItem, whatever logic onOptionsItemSelected() would contain would fail (assuming there are multiple options).
Since you seem to have a need for shared code, move that piece of logic to its own method, then call that method from both onHomeClicked() and onOptionsItemSelected().
Eg
private void mySharedMethod (){
//implementation
}
public void onHomeClicked (View v){
mySharedMethod();
}
public boolean onOptionsItemSelected (MenuItem item){
switch (item.getItemId ()){
case android.R.id.home:
mySharedMethod();
return true;
default:
return super.onOptionsItemSelected (item);
}
}
Possible you have to declare method and call it from onOptionsItemSelected and from onHomeClicked
onOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.btn:
youNewMethod();
break;
}
}
public void onHomeClicked() {
youNewMethod();
}
private void youNewMethod(){
// write your code here
}

Don't repeat menu code

I'd like to have only one menu for all my activities. I don't want to repeat my menu code (below) in all my activities.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.referent, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
System.out.println("set");
return true;
case R.id.action_alert:
System.out.println("alert");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I read some topics, but I found just one solution witch is to extends a parent class who declare the menu. I can't use this solution because all my activities are not extending Activity, I have also have FragmentActivity and ListActivity.
Is there a solution to have the same menu on each activity writing a minimum of code on each activity?
Depending on what the menu handling code needs access to from the current activity, you could create a class whose only responsability is to handle the selected menu items. Possibly even with just a static method that receives the MenuItem.
For example, modify activities such that the onOptionsItemSelected is:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean handled = MenuHandler.onOptionsItemSelected(item);
if (!handled) {
handled = super.onOptionsItemSelected(item);
}
return handled;
}
and create the MenuHandler class:
public class MenuHandler {
public static boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId()) {
case 1: //R.id.action_settings:
System.out.println("set");
return true;
case 2: //R.id.action_alert:
System.out.println("alert");
return true;
default:
return false; //allow default processing
}
}
}
all what you need is to extend from a main class
public abstract class main extends activity(){
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// put your common menu code
super.onOptionsItemSelected(item);
}
}
public class HelloActivity extends main{
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
}
}

SearchView: can't capture the close event

I use the ActionBarShellock 4.2. I think the OnCloseListener() would be called when I click the closebutton but no response when i did it.
mSearchView.setOnCloseListener(new OnCloseListener() {
#Override
public boolean onClose() {
Toast.makeText(mContext, "OnCloseListener", 1000).show();
return false;
}
});
I've tried to call the getChildAt(index) to get the closebutton. Then I think it's unsafe cause the SearchView is not my own code. So, how can I capture the close event? Did I do in the wrong way?
thanks in advance.
Just get your menuItem, then setOnActionExpandListener. Then override unimplents methods.
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//do something on close
// you must return true
return true;
}
Hack done
good luck

How to implement a menuitem click listener inside a fragment in android

I have MenuItems in the ActionBar and I am using Fragments inside ViewPager. Now I would like to handle onMenuItemClickListener event inside my fragment. It works fine inside Main Activity. But not inside Fragments. And also it doesn't fetch any error.
Here is the methods that I tried. Both works fine inside Activity.
First method:
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.grid_view);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Log.v("test","dfsfdsfasd");
return true;
}
});
return true;
}
Second Method:
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.grid_view:
{
Log.v("Log:","grid_view item pressed");
return true;
}
case R.id.list_view:
{
Log.v("Log:","list_view item pressed");
return true;
}
default:
return true;
}
}
Any help on how to achieve this will be appreciated.
Solved by using onPrepareOptionsMenu method.

Categories

Resources