I have an ImageView created in my xml layout. I then access the ImageView in my Java with the following:
ImageView iv1 = (ImageView) findViewById(R.id.iv1);
I’m then accessing the ImageView’s OnClick method. When the ImageView is clicked I would like to change it’s position on the screen. In the XML side I can do this with layout_margin, but I can’t figure out how that is done in the Java side.
Could anyone point me in the right direction? Thanks in advance!
You can set margins programmatically using LayoutParams, please try with following code:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
and set this layout param object to your view by using:
view.setLayoutParams(layoutParams);