views:

37

answers:

1

So Im trying to add an imageview to my current xml design - and its working decently. Now my main problem is that I cant seem to find a way to set the attributes for the image like where it needs to be displayed etc.

RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.board);

ImageView i = new ImageView(this); i.setImageResource(R.drawable.blue_1); i.setAdjustViewBounds(true); mRelativeLayout.addView(i); setContentView(mRelativeLayout);

i tried messing around with setlayoutparams but got absolutely no clue what to do with it.

A: 

You actually have to use the class LayoutParams to do that efficiently and easily :

RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.board);
ImageView i = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(40, 40);
params.leftMargin = 25;
params.topMargin = 25;
i.setImageResource(R.drawable.icon);
i.setAdjustViewBounds(true);
mRelativeLayout.addView(i, params);

This works for me and put my icon in the top left corner of the screen with the specified margin to the sides of the screen. Does that help?

Sephy
awesome, i had set my ieye on it, but couldnt understand the documentations.
Android Noob
You're lucky because I was just like you and understood it this morning. SO I was happy to share my understanding
Sephy
what about stuff such as alignLeft etc? cannot seem to get it to recognise it, also it seems that all my pictures that was in the relativelayout to a start will ge tthe params from the last line.
Android Noob
ah found it :)now its just how to make it a little more pretty.
Android Noob