0

I wanted my image buttons to maintain their aspect ratio so I followed https://stackoverflow.com/a/7966838/135135

and set the background to be null, and then set the image button source image to be the image button. The issue is now that the image button is not aligning with the parent as it was anymore. How do I make it align with the parent right edge again?

        ImageButton bt = new ImageButton(this);        
        bt.setImageResource(R.drawable.next_button);
        bt.setBackgroundDrawable(null); 

        bt.setAdjustViewBounds(true);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
        params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);


        bt.setLayoutParams(params);        
        return bt;
Community
  • 1
  • 1
RyanCheu
  • 3,522
  • 5
  • 38
  • 47

1 Answers1

0

I ended up just hard coding the dimensions in for the button in dp. I searched a bunch and could not find anyone with a good solution other than this.

RyanCheu
  • 3,522
  • 5
  • 38
  • 47