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)
Related
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.
I have a ImageView and its src is a animation-list when I try to to set it margins programmatically it also resize the imageview very very small.
ImageView hand = (ImageView)rlRoot.findViewById(R.id.ivHandAnimation);
MarginLayoutParams marginsParams = new MarginLayoutParams(hand.getLayoutParams());
marginsParams.setMargins(this.getLeft(), rlRoot.getMeasuredHeight() - 20, 0, 0);
RelativeLayout.LayoutParams lp = new LayoutParams(marginsParams);
//lp.height = rlRoot.findViewById(R.id.btnHole_14).getMeasuredHeight();
hand.setLayoutParams(lp);
if (hand != null) {
hand.setVisibility(View.VISIBLE);
AnimationDrawable frameAnimation = (AnimationDrawable)hand.getDrawable();
frameAnimation.setCallback(hand);
frameAnimation.setVisible(true, true);
}
I have tried to set lp.height but it is not working. And I also create LayoutParams with following code.
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Thank you.
The issue is in rlRoot.getMeasuredHeight() - 20. rlRoot.getMeasuredHeight() inside onCreate method returns you 0. so rlRoot.getMeasuredHeight() -20 means you are reducing height by 20 pixel.
I forgot to answer this question. In my case when ImageView resized it keeps its first width and height when I set the margins hand.setLayoutParams(lp); using android:adjustViewBounds property fix the problem for me.
friends,
i want to set android:layout_centerVertical="true" property of layout
through java code of an image.
can any one guide me how to achieve this. here is my code.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.height = (int)totalHeight;
img.setLayoutParams(params);
i have tried using setScaleType(ScaleType.FIT_CENTER) but no use.
any help would be appriciated.
Try this Code..
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
img.setLayoutParams(params);
Correct me if I'm wrong but it sounds like you are trying to set the alignment (or positional orientation) of an image inside of a layout? To do that you need to set the gravity property of the layout containing the View you align.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(params);
imageView.setImageBitmap(bitmap);
layout.setGravity(Gravity.CENTER_VERTICAL | Gravity.TOP);
layout.addView(imageView);
In this example I'm programmatically adding an image to a RelativeLayout in my layout resource, adding an ImageView, and aligning it so it will be placed at the top, vertical center position.
In the new SDK there's no Gravity.VERTICAL_CENTER, but maybe you could set this with
layout.setGravity(Gravity.CENTER).
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