I have a simple layout: a fixed height TextView with three ImageView objects anchored to the bottom of the view.
How can I programmatically change the height of each of the ImageViews so they vary within some range (10dp, 16dp below the TextView)? Concretely, my activity gets a list of percentages (between 0 and 1) representing how much of that vertical space each of those "bars" should take up.
I've tried the following without success:
// No visible difference
imgView.setMinHeight(newHeight);
// No change to height, but horizontal constrained was messed up
imgView.setLayoutParams(new LayoutParam(imgView.getWidth(), newHeight);
If by "height" you mean the absolute top-to-bottom dimension of the ImageView, you can do the following to double the height of an ImageView. You can set the height to the value that you want.
ImageView iv = (ImageView) findViewById(R.id.imageView);
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) iv.getLayoutParams();
lp.height = lp.height * 2;
iv.setLayoutParams(lp);
See ConstraintLayout.LayoutParams.
But, if by "height" you mean the vertical position of the ImageView, you can do the following to change the ImageView's vertical bias (assuming that you are using ConstraintLaytout):
ConstraintSet cs = new ConstraintSet();
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.layout);
cs.clone(layout);
cs.setVerticalBias(R.id.imageView, 0.25f);
cs.applyTo(layout);
See setVerticalBias.
There are other ways to move an ImageView about the layout, but which you use would depend on how your layout is set up.
Related
For an app i have an activity with various views and I need them all to be square, have the exact same height and width and occupy all together a specified portion of the screen. Therefore, I need to set the width and height programmatically, which seems to work with
View view = findViewById(R.id.nutrition_bar_filled);
LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = newWidth;
view.setLayoutParams(layoutParams);
However, with many views this is pretty tedious, is there a way to set all heights and widths at once as they are all uniform?
Thanks in advance!
In my App. I have a RelativeLayout containing an ImageView.
The RelativeLayout is set to MATCH_PARENT horizontally and WRAP_CONTENT vertically.
The ImageView is set to WRAP_CONTENT both horizontally and vertically. The image Drawable is a 24x24 Vector icon.
Because the icon is too small to be seen, I want to double its size. That's where the issue is:
// Parent RelativeLayout
RelativeLayout titleLayout = new RelativeLayout(MyApplication.getContext());
titleLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//Icon
RelativeLayout.LayoutParams information_icon_layout = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
information_icon_layout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
ImageView information_icon = new ImageView(MyApplication.getContext());
information_icon.setLayoutParams(information_icon_layout);
information_icon.setPadding(
getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_left),
getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_top),
getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_right),
getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_bottom)
);
information_icon.setImageDrawable(MyApplication.getContext().getResources().getDrawable(R.drawable.ic_info_outline_black_24dp));
information_icon.setScaleX(2);
information_icon.setScaleY(2);
information_icon.setAdjustViewBounds(true);
The icon scales as expected, but the parent RelativeLayout does not seem to take into account this change, and still calculates its vertical size using the original size of the Icon (24x24).
As a result, the icon is scaling outside of the parent, and only part of it is visible.
My question: How can I tell the parent RelativeLayout to take into account the scaled size of the icon?
Use scaletype property of an imageview it has main 3 property
center,
centercrop,
centerinside,
And if you want to scratch the image fully it also has property as fit center,fit end,fit start,fitxy.You can use this property in xml as well as java.
here is the link
scaletyper
I have a horizontal scroll view that contains an ImageView followed by a horizontal LinearLayout (with child views). When I receive the ImageView from an API, I want it to be able to match parent in terms of height, then expand in terms of width to maintain aspect ratio. How should I configure the ImageView to make this happen?
Thanks!
Have you tried to simply specify match_parent for height and wrap_content for width? The parent with the same values.
Set the height of the ImageView to match_parent so it matches the height of the parent. Then just calculate the width based on the aspect ratio you're trying to achieve, e.g. -- 16:9 = height/0.5625. -- and set the width to that.
EDIT:
To get the target height, then calculate the target width based on that value, you would first get the height of the parent layout, then calculate the width, then set the LayoutParams of your ImageView, then add the view. Something like this...
RelativeLayout parentLayout = (RelativeLayout) findViewById(R.id.parent_layout);
int height = parentLayout.getHeight();
int targetWidth = (int)Math.round(height / 0.5625); //example for 16:9 ratio
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new ViewGroup.LayoutParams(
targetWidth,
ViewGroup.LayoutParams.MATCH_PARENT
));
parentLayout.addView(imageView);
EDITED
I have a TextView. I am getting the TextView width programmatically and then setting it to the width of my Layout. Say, the width of my TextView is 60, then i apply it to my Layout using myLayout.getLayoutParams().width. Why there is a lag of width for the Layout and for the TextView i.e. the layout shows different width on screen , not equal to that one of my TextView.
In short, 60 width for TextView is different from 60 width of Layout.
I am getting the width of TextView like this :
int textWidth = getTextWidth(text,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, editText.getTextSize(), getResources().getDisplayMetrics()), null);
Why are you stuffing your logic programmatically. Simply use wrap_content in your layout, and it will do all the logic that you are doing in you java class.
Change the width of your layout to android:layout_width = "wrap_content"
Welcome all.
i have a LinearLayout that must show a ImageView. The imageView must fit all the width of the LinearLayout, so i used match_parent for the width, and it must keep the aspect ratio, so i used wrap_content for the height and setAdjustViewBounds to true. But something is not working because the width of the ImageView is correct (it fits the width of the linear layout) but the height of the imageView is wrong (the image is being cutted in the upper and the lower part)
I tryed with all the scale types (fit_center, fit_xy, center_crop, center_inside etc...) and i have problems with all of them (for example, if i use fit_center, the image is does not fit the full width of the ImageView, i can see two black spaces on left and right of the image, also i tried using FIT_CENTER without setAdjustViewBounds and i have the same problem)
HERE YOU HAVE AN SNAPSHOT: http://i.stack.imgur.com/8hrKF.png
this is my code:
LinearLayout onlineHolder = this.holders.get(i);
onlineHolder.setLayoutParams(
new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
onlineHolder.removeAllViews();
ImageView imgv = new ImageView(onlineHolder.getContext());
imgv.setLayoutParams(
new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
imgv.setAdjustViewBounds(true);
imgv.setScaleType(ImageView.ScaleType.CENTER_CROP);
Bitmap b=res.getCachedBitmapWithWidth(this.width); //obtenemos el recurso cacheado con el width correspondiente
imageElement.setBitmap(b);
imgv.setImageBitmap(b);
onlineHolder.addView(imgv);
Thanks
EDIT: it only works if i specify the width and the height of the imageview with absolute numbers. For example, if i put 400 (400 is the 50% of the screen, the layout width) width and 583 height, it works, but if i put MATCH_PARENT for width and WRAP CONTENT for height... it not works... why?
I have this one in XML and it works in the way you want. take a look.
<ImageView
android:id="#+id/attachedImage"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:adjustViewBounds="true" />
But if you want to do it on the runtime, I suggest to create an XML this way and inflate the View on the runTime and add it to your parent layout. That will work for sure.
EDIT : How to inflate the ImageView.
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.your_layout_contains_image_view, null, false);
ImageView imageView = view.findViewById(R.id.attachedImage);