ImageView imageView = = new ImageView(getContext()); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); lp.setMargins(10, 20, 30, 40); imageView.setLayoutParams(lp);