Change the actionbar homeAsUpIndicator Programmatically

后端 未结 12 1623
甜味超标
甜味超标 2020-12-09 23:08

I used the following hack to change the homeAsupIndicator programmatically.

int upId = Resources.getSystem().getIdentifier(\"up\", \"id\", \"android\");
if (         


        
12条回答
  •  感动是毒
    2020-12-09 23:21

    The question was to change dynamically the Up Home Indicator, although this answer was accepting and it is about Themes and Styles. I found a way to do this programmatically, according to Adneal's answer which gives me the clue and specially the right way to do. I used the below snippet code and it works well on (tested) devices with APIs mentioned here.

    For lower APIs, I use R.id.up which is not available on higher API. That's why, I retrieve this id by a little workaround which is getting the parent of home button (android.R.id.home) and its first child (android.R.id.up):

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // get the parent view of home (app icon) imageview
        ViewGroup home = (ViewGroup) findViewById(android.R.id.home).getParent();
        // get the first child (up imageview)
        ( (ImageView) home.getChildAt(0) )
            // change the icon according to your needs
            .setImageResource(R.drawable.custom_icon_up));
    } else {
        // get the up imageview directly with R.id.up
        ( (ImageView) findViewById(R.id.up) )
            .setImageResource(R.drawable.custom_icon_up));
    } 
    

    Note: If you don't use the SDK condition, you will get some NullPointerException.

提交回复
热议问题