0

I have this code:

ImageButton call = new ImageButton(context);
call.setId(9001+result.index);
call.setBackgroundResource(R.drawable.small_call);
LinearLayout.LayoutParams call_params = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT,
    LinearLayout.LayoutParams.WRAP_CONTENT
);
call.setLayoutParams(call_params);

It renders the button the way I want it, but when I do this:

ImageButton call = new ImageButton(context);
call.setId(9001+result.index);
call.setBackgroundResource(R.drawable.small_call);
LinearLayout.LayoutParams call_params = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT,
    LinearLayout.LayoutParams.WRAP_CONTENT,
    Gravity.CENTER_VERTICAL | Gravity.RIGHT
);
call.setLayoutParams(call_params);

It stretches the image, messing up the aspect ratio, and makes it blurry/pixelated.

So what is the correct way to render the image as in the first code snippet, but place the button where the second code snippet puts it?

Thanks in advance.

Robert Louis Murphy
  • 1,558
  • 1
  • 16
  • 29

1 Answers1

2

Use RelativeLayout or FrameLayout instead of LinearLayout.

See also layout_gravity in LinearLayout.

This is a tutorial: A Visual Guide to Relative Layouts In Android.

Community
  • 1
  • 1
Shaiful
  • 5,643
  • 5
  • 38
  • 41
  • Non of the comments or answers so far allow me to do what I want to do: Align each call button right, center each button vertically in it TableRow, while preventing the button from scaling up or stretching. I looked at the example sited above, and after following it, my buttons weren't aligned right or vertically centered. – Robert Louis Murphy Feb 09 '12 at 16:49