1

I have no custom bar, I just set a delete icon to the actionbar, but now I need to set OnClickListener to this icon. how can I do that without custom bar is this possible. Also the icon not apears on the left side, can I set it on the rightside?

activity.getSupportActionBar().setIcon(R.drawable.ic_delete);

I use Navigation Drawer, when I use custom bar the toggle icon despairs.

Sam Joos
  • 430
  • 2
  • 11
  • 25

2 Answers2

0
actionBar.setDisplayHomeAsUpEnabled(true);

Then you need to override activity method:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        onIconClicked();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

To create an item in the right side, you need to create custom menu, actually, it is simple.

Here is example how to do this

Community
  • 1
  • 1
Orest Savchak
  • 4,529
  • 1
  • 18
  • 27
0

Looks like you want to set the ActionBar's home button as your delete button. I would suggest not to do it as that is a bad design decision in my opinion. And moreover you also want to show the button on the right hand side which can be done in a much intuitive manner by using a menu.

Please have a look at the official documentaion for adding ActionBar actions here

Basically you need to add an XML menu resource and declare your actions like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_delete"
        android:icon="@drawable/ic_delete"
        android:title="@string/action_delete"
        app:showAsAction="always"/>

</menu>

Then in your Activity override the OnOptionsItemSelected method:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_delete:
            // Do your stuff here
            return true;

        default:
            return super.onOptionsItemSelected(item);

    }
}
Bhaskar Kandiyal
  • 978
  • 1
  • 10
  • 20