0
  I am using this code to add image to action bar.

ActionBar actionBar = getSupportActionBar();

    actionBar.setDisplayOptions(actionBar.getDisplayOptions()
            | ActionBar.DISPLAY_SHOW_CUSTOM);
    ImageView imageView = new ImageView(actionBar.getThemedContext());
    imageView.setScaleType(ImageView.ScaleType.CENTER);
    imageView.setImageResource(R.drawable.logo_actionbar);
    ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
                    | Gravity.CENTER_VERTICAL);
    layoutParams.rightMargin = 0;
    imageView.setLayoutParams(layoutParams);
    actionBar.setCustomView(imageView);

But stil I am getting image in centre.Can you please tell me where I am wrong?Thanks in advance.

Stormbuster
  • 76
  • 2
  • 12
  • what do you want to do? also, this will probably help: http://stackoverflow.com/questions/15518414/how-can-i-implement-custom-action-bar-with-custom-buttons-in-android – Jim Nov 02 '14 at 15:32
  • I don't want to create any XML file because I have 12 different activities which would need different text and images on action bar.Is there any way to do in java.The above code is working but it is displaying image in center,but I want it to be in right of action bar. – Stormbuster Nov 02 '14 at 15:36

1 Answers1

0

Inflate a LinearLayout or other layout and add the ImageView to the layout.

The problem here is that the ImageView is only occupying the space required for the image. The LinearLayout can fill the ActionBar with the ImageView on the right.

Jim
  • 10,172
  • 1
  • 27
  • 36