0

how to set the alignment of views in the relative layout using java code in android

I know how to do in XML

android:layout_alignLeft="@id/button1"
android:layout_alignParentLeft="@id/button2"

but, I want to know how to write using java code? My Problem is to I want to align one TextView to right and ImageView to left inside a RelativeLayout. How to do that?

1 Answers1

0
Button btn = (Button)findviewbyid(R.id.your_button_id)

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.RIGHT_OF, view.getId());

bbtn.setLayoutParams(params);

And then do the same for LEFT_OF

Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26