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);
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)
So I am making an app that holds multiple user-chosen pictures. I have default ImageViews with pre-set heights of 300dp. I want this height to change to wrap_content once an image has been placed into the ImageView. The only way I know to do this is to remove the image from the layout and then re-add it with a new LayoutParams, but this messes up the order of the other views in my layout. Can I change the height without removing it?
Essentially:
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
final float scale = getResources().getDisplayMetrics().density;
LinearLayout.LayoutParams mTestImgParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
(int) Math.ceil(scale * 300)
);
final ImageView createdView = new ImageView(this);
mainLayout.addView(createdView, mTestImgParams);
//onLongClick listener, get picture, set the picture into the imageview, etc.
I somehow want to change the
(int) Math.ceil(scale*300)
to
LinearLayout.LayoutParams.WRAP_CONTENT
without removing and re-adding the ImageView, and only after the image has been placed. Help please.
You could try to get the current layout params and change it.
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) createdView.getLayoutParams();
params.height = LayoutParams.WRAP_CONTENT;
createdView.setLayoutParams(params);
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);
Hi I wanted to change height and width property of image view inside my activity
I tried it in following way but its not working for me ...
View card_view = getLayoutInflater().inflate(R.layout.card_details1,null);
coupon_img = (ImageView) card_view.findViewById(R.id.coupon_image);
// I tried this ////////
coupon_img.getLayoutParams().height = 20;
coupon_img.getLayoutParams().width = 20;
// I also tried this ////
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
coupon_img.setLayoutParams(layoutParams);
// also this one ////
coupon_img.setMaxHeight(10);
But I am not able to change height and width of imageview src. Is there any mistake I am doing?
How to do this?
Need help...
Thank you...
In this piece of code, I am creating a new instance of an ImageView at runtime and setting dimensions to it:
// SET THE IMAGEVIEW DIMENSIONS
int dimens = 120;
float density = activity.getResources().getDisplayMetrics().density;
int finalDimens = (int)(dimens * density);
LinearLayout.LayoutParams imgvwDimens =
new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgAlbumPhoto.setLayoutParams(imgvwDimens);
// SET SCALETYPE
imgAlbumPhoto.setScaleType(ScaleType.CENTER_CROP);
// SET THE MARGIN
int dimensMargin = 5;
float densityMargin = activity.getResources().getDisplayMetrics().density;
int finalDimensMargin = (int)(dimensMargin * densityMargin);
LinearLayout.LayoutParams imgvwMargin =
new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgvwMargin.setMargins
(finalDimensMargin, finalDimensMargin, finalDimensMargin, finalDimensMargin);
This will set the dimensions of the ImageView. They will, however, be in px. Use the code from here if you need dp values: https://stackoverflow.com/a/9563438/450534
UPDATED:
To change the dimensions of an existing ImageView that has already been defined in the XML, use this:
coupon_img.setLayoutParams(new LayoutParams(100, 100));
try some thing like this...
LayoutParams params = new LayoutParams(100, 100);
parantlayout.addView(coupon_img, params);
I think this will help you.
I think you are not adding the changed image to the layout..
LinearLayout ll = (LinearLayout)findViewById(R.layout.yourlinearlayout);
image.setLayoutParams(
new LinearLayout.LayoutParams(
bmp.getWidth(),
bmp.getHeight()));
ll.addView(image);// Then add the image to linear layout
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