Search code examples
androidviewoverlayandroid-actionbar-compat

Android - Draw Views Onto SupportActionBar


In Android, is it possible to draw some views onto an existing Support ActionBar, such as ImageViews?


Solution

  • try this sample...

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
    
        ImageView imageView = new ImageView(actionBar.getThemedContext());
        ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
        imageView.setLayoutParams(layoutParams);
        imageView.setImageResource(R.drawable.image1);
        imageView.setScaleType(ImageView.ScaleType.CENTER);
    
        actionBar.setCustomView(imageView);