How to change HomeAsUp indicator in new AppCompat Toolbar?

后端 未结 6 1787
既然无缘
既然无缘 2020-12-28 15:01

I wanna change default ActionBar homeAsUp indicator (drawable) in my AppCompat Toolbar. How to achieve that? Only default arrow shows up.

styles (same for other API\

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-28 15:22

    i just found out that it actually works toolbar.setNavigationIcon(R.drawable.ic_action_back) and here is the code to make it work.

      @Override
       protected void onCreate(Bundle savedInstanceState) {
    
       .....
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_action_back);
    

    Post the following code in onOptionsItemSelected(MenuItem item)

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       ......
        if (id == android.R.id.home) {
            startActivity(new Intent(this, MainActivity.class));
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    

    Note Mainactivity in this code is the current activity i want my back or home button to go to. I hope this helps

提交回复
热议问题