Reputation: 2061
I'm new to Android. In my app I have created an ImageView programmatically in my FrameLayoutProgramatically
class but when I use setLayoutParams
of this Imageview there it's showing errors. Why am I getting these errors?
My FrameLayoutProgramatically
class:
public class FrameLayoutProgramatically extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Initializing imageView
ImageView imageview = new ImageView(this);
imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview.setImageResource(R.drawable.nature);
imageview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
Upvotes: 2
Views: 7138
Reputation: 3353
You need to specify which LayoutParams you need to use, it must be based on parent layout which can be anything LinearLayout.LayoutParams or RelativeLayout.LayoutParams or FrameLayout.LayoutParams.
RelativeLayout imageLayout = new RelativeLayout(this);
ImageView imageview = new ImageView(this);
imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview.setImageResource(R.drawable.nature);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageLayout.addView(iv, lp);
or
imageview.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
imageLayout.addView(iv);
Upvotes: 4