Add Icon to the left of the title in the action bar in android

后端 未结 7 1773
旧时难觅i
旧时难觅i 2021-02-07 01:07

I want to add a clickable icon/button to the left of the title in the action bar . How to do it? Following is the code with which I have added search and setting icon to the act

7条回答
  •  暖寄归人
    2021-02-07 01:47

    Recommended way

    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_settings_24dp);// set drawable icon
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    

    Handle icon click event

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        switch (item.getItemId()) {
            case android.R.id.home:
                Toast.makeText(this, "click..!!", Toast.LENGTH_SHORT).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
    
        }
    }
    

提交回复
热议问题