1

I have a requirement where i have a icon ( not the application icon ) that needs to be on left hand side of the action bar.

I tried creating custom view but i have no clue on how to detect the click on that icon.

Other option that comes to mind is to have setIcon(mycustomicon) and handle the click events onOptionSelected method; but this method is also not being invoked and not working.

I am using actionbar sherlockholmes library. Is it because of that?

Please help me as i am stuck with this for days .

Thanks in advance

Jigar Pandya
  • 6,004
  • 2
  • 27
  • 45
Preethi
  • 2,112
  • 7
  • 38
  • 54

4 Answers4

6

This may help you

    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            getSupportActionBar().setCustomView(R.layout.actionbar);
            /***THis will be used to creatre a custom action bar click listener **/
            View v =getSupportActionBar().getCustomView();
           /*** sample click is a id of the view i have used in action bar view ***/
            ((Button)v.findViewById(R.id.smaple_click)).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    /*****Add your click function here******/

                }
            });
Rakki s
  • 1,426
  • 1
  • 18
  • 42
5

If you are adding your custom view with getSupportActionBar().setCustomView(resId), you need to get the newly created View object back by calling getSupportActionBar().getCustomView(). Then, you can set your click handler with setOnClickListener as you would normally do with others views.

On the other hand, if you are using one of the overloads of setCustomView that take a View as the first parameter, the first step can be omitted since you already have a View object to work with.

Here is an example for the first case:

getSupportActionBar().setCustomView(R.layout.test);
getSupportActionBar().setDisplayShowCustomEnabled(true);

getSupportActionBar().getCustomView().setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {
        // your code here
    }
});
Tamás Szincsák
  • 1,031
  • 2
  • 12
  • 31
  • i Have more than one view in the action bar I have used switch (view.getId()) { case R.id.menu: Toast.makeText(getApplicationContext(), "Menu", Toast.LENGTH_SHORT).show(); break; case R.id.textView: Toast.makeText(getApplicationContext(), "Header", Toast.LENGTH_SHORT).show(); break; } for this but unable to set click event can u please suggest me where i'm doing wrong – Dilip Nov 07 '13 at 06:17
1

You can make your own "Action", like so

public class CustomAction extends AbstractAction
{
    public interface CustomActionListener
    {
        public void onAction(int drawable);
    }

    private CustomActionListener listener;
    private int drawable;

    public CustomAction(CustomActionListener listener, int drawable)
    {
        super(drawable);

        this.listener = listener;
        this.drawable = drawable;
    }

    @Override
    public void performAction(View view)
    {
        if (listener != null)
        {
            listener.onAction(drawable);
        }
    }
}

Then, add the action to the action bar.

CustomAction menuAction = new CustomAction(this, R.drawable.ic_menu);
ActionBar.addAction(menuAction);

Make sure the activity implements CustomActionListener, in which you can do almost anything.

@Override
public void onAction(int drawable)
{
    // your code goes here...
}

I must admit I'm using a slightly modified version of johannilsson/android-actionbar. But the main idea is to know how to use Action and AbstractAction. Check out the implementation of "BackAction" in the library.

Sarath Vuyyuru
  • 118
  • 1
  • 12
-1

u don need to create a custom view jst add the item in menu layout (having required icon). Then handle onOptionSelected() and make sure are you returning true

user987171
  • 122
  • 5