I have a bunch of images with different sizes. Each of them should be presented on the top part of the screen and must take the space in height equal to 60% of screen height. Width of the image will be dependent on it's height to save initial proportions. I tried to use weightsum property in layout and weight property in ImageView, but I don't know what to put in the height property of my image view. If it is "wrap_content", every image resizes my ImageView and all mark-up crushes.
Any advices?
If you want, you can set the image's dimensions by code.
Just set the width with the weight_sum method and then do something like:
WindowManager manager = (WindowManager) context.getSystemService(Activity.WINDOW_SERVICE);
int screenHeight = manager.getDefaultDisplay().getHeight();
YOUR_VIEW.getLayoutParams().height = (int) (screenWidth * 0.6);
Please mind that you can do so only AFTER your ImageView has been drawn on screen - so calling it within the onCreate() method will not work.
You can either call it delayed (postDelayed) or set a layout listener to one of your view.
Hope this helps.
Related
I'm working with an ImageView and I want to resize it programmatically passing (for example) from a full-screen ImageView to a 50x50. Is there a way to do that?
Different from the one you suggested me to see because I don't need to fit the image in the ImageView but to resize the dimension of the ImageView.
My suggestion use a relative layout and put image inside that has with weight and able to auto resize which depends on the weight. The best way to auto resize even your screen rotates.
I didn't remember which one layout has weight format
You can use LayoutParams to set height width programmatically -
ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
layoutParams.width = 30;
layoutParams.height = 30;
imageView.setLayoutParams(layoutParams);
ConstraintLayout provides a mechanism for fixing the aspect ratio of a child view. Select the child view that we want to control, and then set the ratio value, we can manually put the ratio that we want to set.
Using constraintLayout makes UI shrink or expand according to screen size without distorting the UI. This is the most important advantage of using ConstraintLayout in UI.
I have a ScrollView with a LinearLayout with 3 elements inside. I would like that the first element has a height of 1/3 the height of the device height and the other 2 with wrap_content, is this possible to do in xml or how would you do this? Using weight alone it does not seem possible because the LinearLayout inside the ScrollView could be longer than the device's height.
According to Android docs, android:layout_weight:
Indicates how much of the extra space in the LinearLayout is allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
This implies that the weight is not dependent on the screen height, but rather only on its parent view, so you can't achieve that from xml. To achieve this I would compute the height of the screen and then resize the view:
Display screen = getWindowManager().getDefaultDisplay();
Point size = new Point();
screen.getSize(size);
int screenHeight = size.y;
myView.setLayoutParams(new LayoutParams(myView.getWidth(), screenHeight / 3));
You can give the first linear layout weight=0.333 with height=0 and the other two layout wrap_content
In my app, I download images from my web server (each having a different resolution) but I would like to show these images on a fragment screen consistently.
Ideally, I would like the image to have width="match_parent" and height to take 1/3 of the screen EXACTLY. Furthermore, the image should be shown in a as it's part of a content layout with other controls that could possibly grow larger than the screen height (hence the need for scrolling).
I have tried putting the image and the rest of the content in a LinearLayout and then setting the weight to 1 (with max weight being 3), but since the image and the other contents are in a scrollview, it doesn't quite seem to work. the image is either too large or too small (depending on the orientation and the resolution of the image) and my 1/3rd settings don't seem to be respected.
Is there any way to do this other than fixing the height to a pre-determined (dp) value? I would like to avoid that unless there's no other choice.
Many thanks,
You could always set the height for each of the images programatically to 1/3 the height of the screen, like so
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
ImageView img = (ImageView) findViewById(R.id.image);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) img.getLayoutParams();
params.height = (int)height / 3;
img.setLayoutParams(params);
Is there a layout, that would allow me to make it in absolute values, but when it would be on larger / smaller screen, it would stretch and adjusted those values, to fit onto the screen but preserve the same look?
Relative layout still keens on exact values.
For example, I have a button and I want it to have it the width of 1/3 of the screen of every device.
Setting manualy in code the width of an element to (for example) screenWidth/3 works. Yet I don't think it's clean. But this technique works.
Find device dimensions at runtime and set width of button at runtime.
Display mDisplay = getWindowManager().getDefaultDisplay();
int deviceWidth = mDisplay.getWidth();
int deviceHeight = mDisplay.getHeight();
button.getLayoutParams().width = deviceWidth / 3;
Give the values in dpi of your layout and view and they will adjust themselves on any screen
I have one question. I want to set one image to image button. I have one button image of size 90 x 48. I want to set this image on Image button.
Problems:
I need to set image on background parameter or src parameter?
If i set image on background parameter its not showing its actual size(smaller than actual size) if i have set the Layout width = wrap content and Layout Height= wrap content. If i give Layout width = 98dip and Layout Height= 48dip then its showing the same size. Is it a coorect way?
thanks
set image as background Parameter sometimes src makes problem
if you are set width and height as wrap_content then android will check the screen size and try to adjust to your screen if screen size is big then android displays your image as its actual height and width and if screen size is less then it will compress your image
so best way is to set height and width is as wrap_content if you have to use portrait and landscape mode both
Yes.
If you use Layout width = wrap content and Layout Height= wrap content, image will be as actual size. Maybe just the density of your monitor is less than the density of your phone?
Remember dip != pixels.