Fustigador
Fustigador

Reputation: 6459

How to set a custom back button in ActionBar?

I am creating an ActionBar (SupportActionBar, more precisely) this way:

android.support.v7.app.ActionBar actionBar=getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);
    //actionBar.setDisplayOptions(actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE););
    actionBar.setIcon(R.drawable.ic_drawer);
    actionBar.setTitle(mTitle);

This way, I get a button in my action bar which, once clicked, does what I want it to do. But, it displays an arrow pointing to the left. I would like to display another drawable, so I uncomment the setDisplayOptions line, and the icon I want is displayed. But, the button is no longer clickable.

How could I set my drawable to the button, and maintaining it clickable?

Upvotes: 4

Views: 6840

Answers (2)

Keshav
Keshav

Reputation: 3273

If you want to back arrow button, just use below code:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val ab = actionBar
    ab?.setDisplayHomeAsUpEnabled(true)
    getSupportActionBar()?.setDisplayHomeAsUpEnabled(true);
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
   finish()
   return super.onOptionsItemSelected(item)
}

Upvotes: 0

Maksim Ostrovidov
Maksim Ostrovidov

Reputation: 11058

Use setHomeAsUpIndicator() method instead

actionBar.setHomeAsUpIndicator(R.drawable.ic_drawer);

Upvotes: 7

Related Questions