I want to add ImageView to FrameLayout with Gravity or margins. but FramLayout and ImageView has no method about that(Actually, I can't found that). Reason that selects Framelayout is to put ImageView on ImageView.
Help me plz. It is emergency for me to find solution.
thx.
Below is my code which help understanding my question.
FrameLayout imageFrame = new FrameLayout(mContext);
imageFrame.setLayoutParams(new GridView.LayoutParams(158, 158));
ImageView frame = new ImageView(mContext);
frame = new ImageView(mContext);
frame.setLayoutParams(new LayoutParams(158, 158));
frame.setScaleType(ImageView.ScaleType.CENTER_CROP);
frame.setImageResource(R.drawable.image_frame_n);
ImageView image = new ImageView(mContext);
image.setLayoutParams(new LayoutParams(148, 148));
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
image.setImageResource(mThumbIds[position]);
// image is needed to have a margin or gravity to be positioned at center of imageFrame
imageFrame.addView(image);
imageFrame.addView(frame);
Try:
image.setLayoutParams(new FrameLayout.LayoutParams(width, height, gravity));
More info:
http://developer.android.com/reference/android/widget/FrameLayout.LayoutParams.html
Related
I have created an ImageView by code and I want it to have a max size of 24x24dp. But the setMaxHeight/Width aren't working. The image I use is a svg Drawable and it's size is 40x40dp.
I have also tried to use imageView.setMinimumHeight/Width and
imageView.setLayoutParams(new ViewGroup.LayoutParams((int) getTypedValueInDP(24), (int) getTypedValueInDP(24)));
without any effect!
Here I am creating the ImageView:
ImageView imageView = new ImageView(context);
imageView.setMaxHeight(getTypedValueInDP(20));
imageView.setMaxWidth(getTypedValueInDP(20));
imageView.setImageDrawable(context.getResources().getDrawable(drawable));
imageView.setPadding(left, 0, left, 0);
and I'm using it with a RelativeLayout also made in code like this:
RelativeLayout toastLayout = new RelativeLayout(context);
toastLayout.setBackground(getBLLShape());
toastLayout.addView(getImageView());
You need to specify the LayoutParams for the ImageView before adding to the ViewGroup.
ImageView imageView = new ImageView(this);
imageView.setMaxHeight(getTypedValueInDP(20));
imageView.setMaxWidth(getTypedValueInDP(20));
imageView.setAdjustViewBounds(true);
imageView.setImageDrawable(context.getResources().getDrawable(drawable));
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(layoutParams);
RelativeLayout toastLayout = new RelativeLayout(this);
toastLayout.addView(imageView);
The padding seems to be messing up with the setMaxWidth(). Make sure that the padding you give at both sides must not be equal to or greater than setMaxWidth argument. In your code, you are giving padding to left and right side. So, change:
imageView.setMaxWidth(getTypedValueInDP(20));
to
imageView.setMaxWidth(2* left + getTypedValueInDP(20));
If your image and imageview have same aspect ratio. Did you try
imageview.setScaleType(ImageView.ScaleType.CENTER_INSIDE)
I'm currently trying to set the height of an ImageView, placed as ListView header, but i'm always getting an NullPointerExpection in imageHeaderView.getLayoutParams. Here's my code:
ImageView imageHeaderView = new ImageView(this);
imageHeaderView.setImageBitmap(BitmapFactory.decodeResource(getResources(),
R.drawable.deckblatt));
imageHeaderView.setScaleType(ScaleType.CENTER_CROP);
imageHeaderView.getLayoutParams().height = 50;
myList.addHeaderView(imageHeaderView);
Is it not possible to get/set the layoutParams, when used in a ListView header?
What else can i do to set the height of my ImageView?
Best regards
try this--->
ImageView imageHeaderView = new ImageView(this);
imageHeaderView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.deckblatt));
imageHeaderView.setScaleType(ScaleType.CENTER_CROP);
imageHeaderView.setLayoutParams(new AbsListView.LayoutParams(100, 100));
myList.addHeaderView(imageHeaderView);
I want to add one imageView, TextView in a Relative Layout with background image through programmatically.I spedified height and width for the relative layout but it is not fitting to the specified width and height. where am going wrong ploesae help
Thanks in advance
here is my Code:
RelativeLayout.LayoutParams lp_topheader = new RelativeLayout.LayoutParams(800,45);
relative_topheader = new RelativeLayout(this);
relative_topheader.setLayoutParams(lp_topheader);
relative_topheader.setId(1);
Resources resources_topheader = getResources();
Drawable drawable_topheader = resources_topheader.getDrawable(R.drawable.headerbar_m);
relative_topheader.setBackgroundDrawable(drawable_topheader);
setContentView(relative_topheader);
RelativeLayout.LayoutParams lp_banner = new RelativeLayout.LayoutParams(385, 206);
relative_banner = new RelativeLayout(this);
relative_banner.setId(2);
relative_banner.setLayoutParams(lp_banner);
lp_banner.setMargins(40, 40, 0, 0);
lp_banner.addRule(RelativeLayout.BELOW,1);
ImageView iv = new ImageView(this);
iv.setScaleType(ScaleType.FIT_XY);
iv.setLayoutParams(lp_banner);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.banner_image);
iv.setImageBitmap(bitmap);
setContentView(iv, lp_banner);
The root view (that is, the view that you set as the content view) always fill the entire area of the window. If you want it to take only a certain part, add it to another Layout that will take the entire window.
Try this instead of the last line:
LinearLayout ll = new LinearLayout(this);
ll.addView(iv, lp_banner);
setContentView(ll);
I want to put some images in an area at the application start.
As the number of images is not a fixed amount, so I have to create images dynamically.
I want to set the position/margin of every images while they're created for good balance.
I have tried follows, but there is no efficacy.
・After Created, use imageview.layout().
・Use LayoutParams.margins().
・Use AsyncTask to set margins.
・activity.runOnUiThread().
This is the code:
// After access server, if needed then add a imageview to the 'c_box'
// c_box: the parent that imageview to add.
FrameLayout c_box = (FrameLayout) findViewById(R.id.c_box);
MarginLayoutParams mlp = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageView img1 = new ImageView(this);
img1.setImageResource(R.drawable.c_file);
img1.setLayoutParams(params);
img1.setLongClickable(true);
img1.setId(img_id);
c_box.addView(img1);
img1.setOnTouchListener(listener);
**// it's possible to set the position by layout or margin?
img1.layout(100, 100, 200, 200);**
I don't know where to call the invalidate() method.
// After access server, if needed then add a imageview to the 'c_box'
// c_box: the parent that imageview to add.
FrameLayout c_box = (FrameLayout) findViewById(R.id.c_box);
MarginLayoutParams mlp = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mlp.setMargins(100, 100, 200, 200);
ImageView img1 = new ImageView(this);
img1.setImageResource(R.drawable.c_file);
img1.setLayoutParams(mlp);
img1.setLongClickable(true);
img1.setId(img_id);
c_box.addView(img1);
img1.setOnTouchListener(listener);
Finally I found the point:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.NO_GRAVITY);
the 3rd param(Gravity.NO_GRAVITY) that I missed. use it you can position views at anywhere with setting width/height.
You could use MarginLayoutParams
MarginLayoutParams mlp = (MarginLayoutParams) yourImageViewHere.getLayoutParams();
mlp.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);//all in pixels
yourImageViewHere.setLayoutParams(mlp);
To set your margins dynamically.
So I'm 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.
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?