Let's say that I have a layout that contains
<ImageView
android:id="#+id/my_id"
android:layout_width="123px"
android:layout_height="345px"
android:padding="0dip"
android:scaleType="fitXY" />
which is used in an AppWidget and is set via a RemoteViews. How can I programmatically set the width and height in pixels (overriding the 123 and 345 in the above xml)?
(motivation: this image view shows a dynamic png file that is set at runtime via a file URL. If the layout_width and layout_height above are set to wrap_content, the image is displayed scaled down the the device's density(). Setting the exact width and height in pixels solves the problem but the file size changes and I don't know how to set it programmatically).
You can do it in a different way. Put your ImageView in a LinerLayout and set layout_width and layout_height of ImageView to "fill_parent". Then change LinearLayout's width and height.
I assume you know about app widgets size rules.
Related
There is Frame Layout, where width\height = wrap_content. But it increases to max size when I try to set a background, even if it doesn't contains any elements.How to fix it?
<FrameLayout
xmlns:android="schemas.android.com/apk/res/android";
xmlns:app="schemas.android.com/apk/res-auto";
android:background="#drawable/oblakol"
adroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true" >
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</FrameLayout>
Your background is much bigger than you think. Check size of background!
Wrap content is working but your image size is large and hence you are not getting the expected results.
Try importing image as -> IMAGE ASSET so the size will be auto-Adjusted
or
set size manually!
I hope this will help you ...
wrap_content will put the background of it's original size.
Using large resolutions will cause lagging in your app while using. So, you should resize your background according to your needs : Image Resizer
When you set the dimensions(layout_width or layout_height) of a view to WRAP_CONTENT, you are giving freedom to the view to expand without any limit(as per its content ). Same is with your scenario as you've set the height and width to WRAP_CONTENT for your Frame Layout.
So whatever the space occupied on screen, its due to the large size of your android:background="#drawable/oblakol" drawable image. Try setting the height and width of the Frame Layout to some random values to test it.
android:layout_width="250dp"
android:layout_height="250dp"
I've attached some screenshots for your reference.
android:layout_width and android:layout_height set to WRAP_CONTENT. Actual dimension of the android Image is 800x799(wxh)
android:layout_width and android:layout_height set to WRAP_CONTENT. Actual dimension of the android Image is 256x256(wxh)
android:layout_width and android:layout_height set to 250dp. Actual dimension of the android Image is 800x799(wxh)
Hope it helps.
I'm working on mdpi, and when I add a imageView to the activity it doesnt fill the screen, I tried setting the scaleType to fitXY but the image didnt fill the screen, also I'm using a relativeLayout
You must set width and height parameters as match_parent in your XML layout (instead of wrap_content)
For Android, How can I change the image size in an ImageView in a layout?
My image is jpeg format.
<ImageView
android:id="#+id/imageView1"
android:layout_margin="20dp"
android:src="#drawable/stop"
android:layout_width="50dp"
android:layout_height="50dp"/>
Just change width and height attribute.
You can also set Params using LayoutParams
ImageView yourImageView= (ImageView)findViewById(R.id.imageId);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
//width and height of your Image ,if it is inside Relative change the LinearLayout to RelativeLayout.
yourImageView.setLayoutParams(layoutParams);
There are a few ways to change the size of an image in an imageView in XML.
Scale the image by pulling around the edges of the imageView in the Graphical Layout (this is not really advised since the Graphical Layout sometimes adds undesired properties into the XML).
Apply a weight to the imageView (this requires the parent object to have specified a weightSum), causing the image to scale according to the screen size. This seems to be the most reliable way to gauge how your layout will look on many different sized screens.
Simply adjust the weight and height in the imageView (as others have mentioned).
<ImageView
android:id="#+id/imageViewId"
android:src="#drawable/icon"
android:layout_width="xxdp"
android:layout_height="yydp"/>
where xx is the width in pixels and yy is the height in pixels.
(give integer values as you desire the size to be)
Is there a way to proportionally set the height of an ImageView if the width is set to android:layout_width="fill_parent"?
It appears there is no direct way of adjusting an ImageView without skewing one of it's dimensions. This seems bizarre given the application is expected to support a slew of screen sizes and resolutions.
You can use wrap_content for the height and set the correct scale type (either in code or via the android:scaleType attribute in XML). You are probably looking for CENTER_INSIDE/android:scaleType="centerInside".
See ImageView.ScaleType
I am creating a dynamic table whose rows contains a imageview and a textview. My problem is this imageview is taking full size of original image but I want to change the size of imageview.I have used setLayoutParams which has no effect.
As an alternative I also used textview instead of imageview and set image as textview's background and used setWidth and setHeight but it has the same problem.
Plz Help Me.
Have you tried the scaleType-Attribute?
Check out the setMaxWidth and setMaxHeight-methods of the ImageView-class:
To set an image to be a maximum of 100
x 100 while preserving the original
aspect ratio, do the following: 1) set
adjustViewBounds to true 2) set
maxWidth and maxHeight to 100 3) set
the height and width layout params to
WRAP_CONTENT.
Link
Assuming your image is a drawable resource, you can use ScaleDrawable instead, and use the xml attributes android:scaleHeight and android:scaleWidth to shrink your image.
OK I found the Solution
Resizing an ImageView within a TableLayout programmatically
Try changing your ImageView's layout_width and layout_height inside the layout file to some constant values measured in sp, for example 100sp. This will make all ImageViews look the same.