First change the root layout(which is parent of your LinearLayout
and ImageView
) to RelativeLayout
in your xml file.
Initialize both views:
LinearLayout linear=(LinearLayout)findViewById(R.id.contentWithBackground);
ImageView image=(ImageView)findViewById(R.id.imageViewDelivered);
Initialize the LayoutParam
of LinearLayout
and ImageView
:
RelativeLayout.LayoutParams linearLp=(RelativeLayout.LayoutParams)linear.getLayoutParams();
RelativeLayout.LayoutParams imageLp=(RelativeLayout.LayoutParams)image.getLayoutParams();
To set the LinearLayout
to left of ImageView
:
linearLp.addRule(RelativeLayout.LEFT_OF,image.getId());
To set the LinearLayout
to right of ImageView
:
linearLp.addRule(RelativeLayout.RIGHT_OF,image.getId());